/** * This function saves the data of an entity to its class. * This only prepares the database store, but does not store it in database * To store them in database use persist and flush from doctrine * * @access protected * @param object $entity object of the class we want to save * @param Doctrine\ORM\Mapping\ClassMetadata $entityClassMetadata MetaData for the entity * @param array $entityData array with data to save to class * @param string $associatedTo the class which is oneToManyAssociated if it exists */ protected function savePropertiesToClass($entity, $entityClassMetadata, $entityData = array(), $associatedTo = '') { // if entityData is not set, we use $_POST as default, because the data are normally submitted over post if (empty($entityData)) { $entityData = $_POST; } $cx = \Cx\Core\Core\Controller\Cx::instanciate(); $em = $cx->getDb()->getEntityManager(); $primaryKeyName = $entityClassMetadata->getSingleIdentifierFieldName(); //get primary key name $entityColumnNames = $entityClassMetadata->getColumnNames(); //get the names of all fields //If the view is sortable, get the 'sortBy' field name and store it to the variable $sortByFieldName = isset($this->options['functions']['sortBy']) && isset($this->options['functions']['sortBy']['field']) && !empty($this->options['functions']['sortBy']['field']) ? key($this->options['functions']['sortBy']['field']) : ''; //check the 'sortBy' field is self-healing or not $isSortSelfHealing = isset($this->options['fields']) && isset($this->options['fields'][$sortByFieldName]) && isset($this->options['fields'][$sortByFieldName]['showDetail']) && !$this->options['fields'][$sortByFieldName]['showDetail'] ? true : false; // Foreach possible attribute in the database we try to find the matching entry in the $entityData array and add it // as property to the object foreach ($entityColumnNames as $column) { $name = $entityClassMetadata->getFieldName($column); $fieldSetMethodName = 'set' . preg_replace('/_([a-z])/', '\\1', ucfirst($name)); $fieldGetMethodName = 'get' . preg_replace('/_([a-z])/', '\\1', ucfirst($name)); if (isset($this->options['fields']) && isset($this->options['fields'][$name]) && isset($this->options['fields'][$name]['storecallback'])) { $storecallback = $this->options['fields'][$name]['storecallback']; $postedValue = null; if (isset($entityData[$name])) { $postedValue = contrexx_input2raw($entityData[$name]); } /* We use json to do the storecallback. The 'else if' is for backwards compatibility so you can declare * the function directly without using json. This is not recommended and not working over session */ if (is_array($storecallback) && isset($storecallback['adapter']) && isset($storecallback['method'])) { $json = new \Cx\Core\Json\JsonData(); $jsonResult = $json->data($storecallback['adapter'], $storecallback['method'], array('postedValue' => $postedValue)); if ($jsonResult['status'] == 'success') { $entityData[$name] = $jsonResult["data"]; } } else { if (is_callable($storecallback)) { $entityData[$name] = $storecallback($postedValue); } } } if (isset($entityData[$name]) && $name != $primaryKeyName) { $fieldDefinition = $entityClassMetadata->getFieldMapping($name); if ($fieldDefinition['type'] == 'datetime') { $newValue = new \DateTime($entityData[$name]); } elseif ($fieldDefinition['type'] == 'array') { $newValue = unserialize($entityData[$name]); // verify that the value is actually an array -> prevent to store other php data if (!is_array($newValue)) { $newValue = array(); } } else { $newValue = contrexx_input2raw($entityData[$name]); } // set the value as property of the current object, so it is ready to be stored in the database $entity->{$fieldSetMethodName}($newValue); } //While adding a new entity, if the view is sortable and 'sortBy' field is disabled in the edit view //then the new entity sort order gets automatically adjusted. if ($isSortSelfHealing && !empty($sortByFieldName) && $sortByFieldName === $name && !$entity->{$fieldGetMethodName}()) { $qb = $em->createQueryBuilder(); $qb->select('e')->from(get_class($entity), 'e')->orderBy('e.' . $name, 'DESC')->setMaxResults(1); $result = $qb->getQuery()->getResult(); $newValue = isset($result[0]) ? $result[0]->{$fieldGetMethodName}() + 1 : 1; // set the value as property of the current object, // so it is ready to be stored in the database $entity->{$fieldSetMethodName}($newValue); } } // save singleValuedAssociations foreach ($entityClassMetadata->getAssociationMappings() as $associationMapping) { // we're only interested in single valued associations here, so skip others if (!$entityClassMetadata->isSingleValuedAssociation($associationMapping['fieldName'])) { continue; } // we're only interested if there's a target entity other than $associatedTo, so skip others if ($associationMapping['targetEntity'] == '' || $associatedTo == $associationMapping['targetEntity']) { continue; } // save it: // case a) was open in form directly $firstOffset = str_replace('\\', '_', strtolower($associationMapping['sourceEntity'])); $secondOffset = $associationMapping['fieldName']; if (isset($entityData[$secondOffset])) { $this->storeSingleValuedAssociation($associationMapping['targetEntity'], array($associationMapping['joinColumns'][0]['referencedColumnName'] => $entityData[$secondOffset]), $entity, 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $associationMapping['fieldName'])))); continue; } // base b) was open in a modal form foreach ($_POST[$firstOffset] as $foreignEntityDataEncoded) { $foreignEntityData = array(); parse_str($foreignEntityDataEncoded, $foreignEntityData); if (!isset($foreignEntityData[$secondOffset])) { // todo: remove entity! continue; } // todo: add/save entity $this->storeSingleValuedAssociation($associationMapping['targetEntity'], array($associationMapping['joinColumns'][0]['referencedColumnName'] => $foreignEntityData[$secondOffset]), $entity, 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $associationMapping['fieldName'])))); } } }