コード例 #1
0
 /**
  * Method that sets current model table associations from csv file.
  *
  * @param array $config The configuration for the Table.
  * @return void
  */
 protected function _setAssociationsFromCsv(array $config)
 {
     $csvData = $this->_csvData(true);
     $csvObjData = $this->_csvDataToCsvObj($csvData);
     $csvFilteredData = $this->_csvDataFilter($csvObjData, $this->__assocIdentifiers);
     //setting up files associations for FileStorage relation
     foreach ($csvObjData as $csvModule => $fields) {
         foreach ($fields as $csvObjField) {
             if (in_array($csvObjField->getType(), ['files', 'images'])) {
                 $fieldName = $csvObjField->getName();
                 $assocName = CsvMigrationsUtils::createAssociationName('Burzum/FileStorage.FileStorage', $fieldName);
                 if ($csvModule == $config['table']) {
                     $this->hasMany($assocName, ['className' => 'Burzum/FileStorage.FileStorage', 'foreignKey' => 'foreign_key', 'conditions' => ['model' => $this->table(), 'model_field' => $fieldName]]);
                 }
             }
         }
     }
     foreach ($csvFilteredData as $csvModule => $fields) {
         foreach ($fields as $csvObjField) {
             $assoccsvModule = $csvObjField->getAssocCsvModule();
             $fieldName = $csvObjField->getName();
             if ($config['table'] === $csvModule) {
                 $assocName = CsvMigrationsUtils::createAssociationName($assoccsvModule, $fieldName);
                 //Belongs to association of the curren running module.
                 $this->belongsTo($assocName, ['className' => $assoccsvModule, 'foreignKey' => $fieldName]);
             } elseif ($config['table'] === $assoccsvModule) {
                 //Foreignkey found in other module
                 $assocName = CsvMigrationsUtils::createAssociationName($csvModule, $fieldName);
                 $this->hasMany($assocName, ['className' => $csvModule, 'foreignKey' => $fieldName]);
             }
         }
     }
 }
コード例 #2
0
 /**
  * linkFilesToEntity method
  *
  * Using AJAX upload, we're dealing with created entity,
  * and stored FileStorage files, upon saving the entity,
  * the items should be linked with 'foreign_key' field.
  *
  * @param Cake\ORM\Entity $entity of the record
  * @param Cake\ORM\Table $tableInstance of the entity
  * @param array $data of this->request->data containing ids.
  * @return mixed $result of saved/updated file entities.
  */
 public function linkFilesToEntity($entity, $tableInstance, $data = [], $options = [])
 {
     $result = [];
     $uploadFields = [];
     if (!method_exists($tableInstance, 'getFieldsDefinitions')) {
         return $result;
     }
     foreach ($tableInstance->getFieldsDefinitions() as $field => $fieldInfo) {
         if (in_array($fieldInfo['type'], ['files', 'images'])) {
             array_push($uploadFields, $fieldInfo);
         }
     }
     if (empty($uploadFields)) {
         return $result;
     }
     foreach ($uploadFields as $field) {
         $savedIds = [];
         $savedIdsField = $field['name'] . '_ids';
         // @NOTE: in case of AJAX/API calls we don't have data[Table][field]
         // notation, only data[field].
         if (isset($data[$tableInstance->alias()][$savedIdsField])) {
             $savedIds = $data[$tableInstance->alias()][$savedIdsField];
         } else {
             $savedIds = $data[$savedIdsField];
         }
         if (empty($savedIds)) {
             continue;
         }
         $savedIds = array_values(array_filter($savedIds));
         $assocName = CsvMigrationsUtils::createAssociationName('Burzum/FileStorage.FileStorage', $field['name']);
         foreach ($savedIds as $fileId) {
             $record = $this->_table->{$assocName}->get($fileId);
             $record->foreign_key = $entity->id;
             $result[] = $this->_table->{$assocName}->save($record);
         }
     }
     return $result;
 }
コード例 #3
0
 /**
  * @dataProvider associationNameProvider
  */
 public function testCreateAssociationName($expected, $module, $foreignKey)
 {
     $this->assertEquals($expected, CsvMigrationsUtils::createAssociationName($module, $foreignKey));
 }