Exemple #1
0
 /**
  *
  */
 public function findAll(\Gacela\DataSource\Query\Query $query, \Gacela\DataSource\Resource $resource, array $inherits, array $dependents)
 {
     if (!count($query->from) > 0) {
         $query->from($resource->getName(), array_keys($resource->getFields()));
     }
     return $this->query($resource, $query);
 }
Exemple #2
0
 /**
  * @param \Gacela\DataSource\Resource $resource
  * @param array $changed
  * @param \stdClass $new
  * @param array $old
  * @return array|bool
  * @throws \Exception
  */
 protected function _saveRecord($resource, $changed, $new, $old)
 {
     $fields = $resource->getFields();
     $data = array_intersect_key($new, $fields, array_flip($changed));
     foreach ($data as $key => $val) {
         $data[$key] = $this->_gacela()->getField($fields[$key]->type)->transform($fields[$key], $val);
     }
     if (empty($data)) {
         return true;
     }
     $test = array_merge($new, $old);
     $primary = $this->_primaryKey($resource->getPrimaryKey(), (object) $test);
     $fields = $resource->getFields();
     $update = true;
     if (is_null($primary)) {
         $update = false;
     } elseif ($fields[key($primary)]->sequenced === false) {
         $update = $this->_source()->find($primary, $resource);
     }
     // Insert the record
     if ($update === false) {
         $rs = $this->_source()->insert($resource->getName(), $data);
         if ($rs === false) {
             return $rs;
         }
         $fields = $resource->getFields();
         if (count($resource->getPrimaryKey()) == 1 && $fields[current($resource->getPrimaryKey())]->sequenced === true) {
             $new[current($resource->getPrimaryKey())] = $rs;
             $changed[] = current($resource->getPrimaryKey());
         }
         // Update the existing record
     } else {
         $primary = $this->_primaryKey($resource->getPrimaryKey(), $test);
         if (is_null($primary)) {
             throw new \Exception('Oops! primary key is null');
         }
         $where = $this->_gacela()->autoload('Criteria');
         $where = new $where();
         foreach ($primary as $k => $v) {
             $where->equals($k, $v);
         }
         if ($this->_source()->update($resource->getName(), $data, $this->_source()->getQuery($where)) === false) {
             return false;
         }
     }
     return array($changed, $new);
 }
Exemple #3
0
 /**
  * @param array $primary
  * @param Resource $resource
  * @param array $inherits
  * @param array $dependents
  * @return
  */
 public function find(array $primary, \Gacela\DataSource\Resource $resource, array $inherits = array(), array $dependents = array())
 {
     $crit = new \Gacela\Criteria();
     foreach ($primary as $key => $val) {
         $crit->equals($resource->getName() . '.' . $key, $val);
     }
     return $this->query($resource, $this->_buildFinder($this->getQuery($crit), $resource, $inherits, $dependents))->fetchObject();
 }