Esempio n. 1
0
 /**
  * Z $findBy prevede do $where v dibi formatu, $findBy vyprazdni.
  * @param BaseDibiCollection
  * @param DibiMapper
  * @param IDatabaseConventional
  * @param array reference
  * @param array reference
  * @param string
  * @param string
  * @see DataSourceCollection::getDataSource
  * @see DibiCollection::__toString
  * @see DibiCollection::join
  */
 public static function dibiProcess(BaseDibiCollection $collection, DibiMapper $mapper, IDatabaseConventional $conventional, array &$where, array &$findBy, $tableAlias, $prefix = NULL)
 {
     foreach ($findBy as $tmp) {
         foreach ($tmp as $key => $value) {
             if ($prefix) {
                 $key = $prefix . '->' . $key;
             }
             if ($join = $mapper->getJoinInfo($key)) {
                 $collection->join($key);
                 $key = $join->key;
             } else {
                 $key = $conventional->formatEntityToStorage(array($key => NULL));
                 $key = $tableAlias . key($key);
             }
             if ($value instanceof IEntityCollection) {
                 try {
                     $value = $value->fetchPairs(NULL, 'id');
                 } catch (EntityNotPersistedException $e) {
                     $value = $value->fetchAll();
                 }
             }
             if ($value instanceof IEntity) {
                 $where[] = array('%n = %s', $key, isset($value->id) ? $value->id : NULL);
                 // `= NULL` never be true
             } else {
                 if (is_array($value)) {
                     $tmp = array();
                     foreach ($value as $v) {
                         if ($v instanceof IEntity) {
                             if (!isset($v->id)) {
                                 continue;
                             }
                             $v = $v->id;
                         }
                         $tmp[] = $v;
                     }
                     $where[] = array('%n IN %in', $key, array_unique($tmp));
                 } else {
                     if ($value === NULL) {
                         $where[] = array('%n IS NULL', $key);
                     } else {
                         if ($value instanceof DateTime) {
                             $where[] = array('%n = %t', $key, $value);
                         } else {
                             $where[] = array('%n = %s', $key, $value);
                         }
                     }
                 }
             }
         }
     }
     $findBy = array();
 }
Esempio n. 2
0
 /**
  * @param IEntity
  * @param scalar|NULL id
  * @param string update|insert
  * @return array
  */
 protected function toArray(IEntity $entity, $id, $operation)
 {
     $values = $entity->toArray();
     if ($id !== NULL) {
         $values['id'] = $id;
     }
     $params = array('id' => isset($values['id'])) + (array) $this->params;
     $params += array_fill_keys(array_keys($values), true);
     if ($this->whichParams !== NULL) {
         $params = array('id' => $params['id']) + array_fill_keys($this->whichParams, true);
     }
     if ($this->whichParamsNot !== NULL) {
         $tmp = array_fill_keys($this->whichParamsNot, false);
         unset($tmp['id']);
         $params = $tmp + $params;
     }
     $arguments = array('params' => $params, 'values' => $values, 'operation' => $operation);
     $this->events->fireEvent(Events::SERIALIZE_BEFORE, $entity, $arguments);
     $params = $arguments['params'];
     $values = $arguments['values'];
     $result = array();
     foreach ($params as $key => $do) {
         if (array_key_exists($key, $values)) {
             $value = $values[$key];
         } else {
             // pokusi se precist, muze existovat getter, jinak vyhodi exception
             $value = $entity->{$key};
         }
         if ($do === false or $operation === 'update' and $entity->hasParam($key) and !$entity->isChanged($key)) {
             continue;
         }
         if ($do !== true) {
             $value = Callback::create($do)->invoke($value, $entity);
         }
         $result[$key] = $this->scalarizeValue($value, $key, $entity);
         if ($value instanceof IRelationship and $result[$key] === NULL) {
             unset($result[$key]);
         }
     }
     $arguments = array('values' => $result, 'operation' => $operation);
     $this->events->fireEvent(Events::SERIALIZE_AFTER, $entity, $arguments);
     $result = $arguments['values'];
     $result = $this->conventional->formatEntityToStorage($result);
     $primaryKey = $this->conventional->getPrimaryKey();
     if ($primaryKey !== $this->primaryKey and array_key_exists($primaryKey, $result)) {
         $id = $result[$primaryKey];
         unset($result[$primaryKey]);
         $result = array($this->primaryKey => $id) + $result;
     }
     $arguments = array('values' => $result, 'operation' => $operation);
     $this->events->fireEvent(Events::SERIALIZE_CONVENTIONAL, $entity, $arguments);
     $result = $arguments['values'];
     return $result;
 }
Esempio n. 3
0
 /**
  * @param string
  * @return string
  */
 protected final function getConnventionalKey($key)
 {
     $tmp = $this->conventional->formatEntityToStorage(array($key => NULL));
     return key($tmp);
 }
Esempio n. 4
0
 /**
  * @param array(string $key, bool $wasFormated)
  * @param IDatabaseConventional
  * @param string|NULL
  * @return string
  */
 private function format(array $key, IDatabaseConventional $conventional, $default = NULL)
 {
     list($key, $wasFormated) = $key;
     if ($key === NULL) {
         $key = $default;
     }
     if (!$wasFormated) {
         $tmp = $conventional->formatEntityToStorage(array($key => NULL));
         $key = key($tmp);
     }
     return $key;
 }