/**
  * Run before a model is deleted, used to do a soft delete when needed.
  *
  * @param object $model Model about to be deleted
  * @param boolean $cascade If true records that depend on this record will also be deleted
  * @return boolean Set to true to continue with delete, false otherwise
  * @access public
  */
 function beforeDelete(Model $model, $cascade = true)
 {
     if ($this->__settings[$model->alias]['delete'] && $model->hasField($this->__settings[$model->alias]['field'])) {
         $attributes = $this->__settings[$model->alias];
         $id = $model->id;
         $data = array($model->alias => array($attributes['field'] => 1));
         if (isset($attributes['field_date']) && $model->hasField($attributes['field_date'])) {
             $data[$model->alias][$attributes['field_date']] = $model->getDatasource()->expression('NOW()');
         }
         foreach (array_merge(array_keys($data[$model->alias]), array('field', 'field_date', 'find', 'delete')) as $field) {
             unset($attributes[$field]);
         }
         if (!empty($attributes)) {
             $data[$model->alias] = array_merge($data[$model->alias], $attributes);
         }
         $model->id = $id;
         $deleted = $model->save($data, false, array_keys($data[$model->alias]));
         if ($deleted && $cascade) {
             $model->_deleteDependent($id, $cascade);
             $model->_deleteLinks($id);
         }
         return false;
     }
     return true;
 }
Example #2
0
 /**
  * Interact with the user to get a custom SQL condition and use that to extract data
  * to build a fixture.
  *
  * @param string $modelName name of the model to take records from.
  * @param string $useTable Name of table to use.
  * @return array Array of records.
  */
 protected function _getRecordsFromTable($modelName, $useTable = null)
 {
     if ($this->interactive) {
         $condition = null;
         $prompt = __d('cake_console', "Please provide a SQL fragment to use as conditions\nExample: WHERE 1=1");
         while (!$condition) {
             $condition = $this->in($prompt, null, 'WHERE 1=1');
         }
         $prompt = __d('cake_console', "How many records do you want to import?");
         $recordCount = $this->in($prompt, null, 10);
     } else {
         $condition = 'WHERE 1=1';
         $recordCount = isset($this->params['count']) ? $this->params['count'] : 10;
     }
     $modelObject = new Model(array('name' => $modelName, 'table' => $useTable, 'ds' => $this->connection));
     $records = $modelObject->find('all', array('conditions' => $condition, 'recursive' => -1, 'limit' => $recordCount));
     $db = $modelObject->getDatasource();
     $schema = $modelObject->schema(true);
     $out = array();
     foreach ($records as $record) {
         $row = array();
         foreach ($record[$modelObject->alias] as $field => $value) {
             if ($schema[$field]['type'] === 'boolean') {
                 $value = (int) (bool) $value;
             }
             $row[$field] = $value;
         }
         $out[] = $row;
     }
     return $out;
 }
Example #3
0
 /**
  * Load data files
  *
  * Options:
  *   `class` - Class to load. Default to load all classes in directory
  *   `extract` - Path to identify an entry for `Hash::extract()`
  *
  * @param string $path Path to directory containing data files
  * @param array $options Options array
  * @return bool True if loading was successful
  * @throws CakeException
  */
 public function load($path, $options = array())
 {
     if (!is_dir($path)) {
         throw new CakeException('Argument not a directory: ' . $path);
     }
     $options = Hash::merge(array('ds' => 'default'), $options);
     $dataObjects = App::objects('class', $path);
     if (isset($options['class']) && in_array($options['class'], $dataObjects)) {
         $dataObjects = array($options['class']);
     }
     foreach ($dataObjects as $data) {
         if (!class_exists($data)) {
             include $path . DS . $data . '.php';
         }
         $classVars = get_class_vars($data);
         $modelAlias = substr($data, 0, -4);
         $table = $classVars['table'];
         $records = $classVars['records'];
         $uniqueKeys = null;
         if (!empty($options['extract'])) {
             $records = Hash::extract($records, $options['extract']);
         }
         $Model = new Model(array('name' => $modelAlias, 'table' => $table, 'ds' => $options['ds']));
         if (!empty($classVars['uniqueFields'])) {
             $uniqueKeys = array_flip((array) $classVars['uniqueFields']);
             foreach ((array) $classVars['uniqueFields'] as $field) {
                 if (!$Model->hasField($classVars['uniqueFields'])) {
                     throw new UnexpectedException("{$field} is not found in table {$table}");
                 }
             }
         }
         if (is_array($records) && count($records) > 0) {
             $i = 0;
             foreach ($records as $record) {
                 if (isset($uniqueKeys)) {
                     $conditions = array_intersect_key($record, $uniqueKeys);
                     $count = $Model->find('count', compact('conditions'));
                     if ($count > 0) {
                         continue;
                     }
                 }
                 $Model->create($record);
                 $saved = $Model->save();
                 if (!$saved) {
                     CakeLog::error(sprintf('Error loading row #%s for table `%s`', $i + 1, $table));
                     return false;
                 }
                 $i++;
             }
             $Model->getDatasource()->resetSequence($Model->useTable, $Model->primaryKey);
         }
         ClassRegistry::removeObject($modelAlias);
     }
     return true;
 }
 /**
  * Configuration method.
  *
  * @param object $Model Model object
  * @param array $config Config array
  * @access public
  * @return boolean
  */
 public function setup(Model $Model, $config = array())
 {
     $this->dbo = $Model->getDatasource();
     return true;
 }