Example #1
0
 /**
  * Returns one object from collection
  *
  * @return mixed|null
  */
 public function findOne()
 {
     if (!$this->hasStatement('select')) {
         $this->select('*');
     }
     $this->limit(1);
     $result = null;
     $sql = $this->compile();
     $tbl = $this->getStatement('table');
     $cache_key = OrmCache::genKey($sql, $this->values_accum);
     if (($result_cached = OrmCache::getCachedQuery($cache_key)) !== false && $this->use_cache) {
         return $result_cached;
     }
     if (($result_cached = OrmCache::getFromInternalCache($cache_key)) !== false) {
         return $result_cached;
     }
     if ($row = Db::getRow($sql, $this->values_accum)) {
         $result = Orm::collection($this->entity_name)->create($row);
     }
     if ($this->use_cache) {
         OrmCache::cacheQuery($cache_key, $tbl['sql'], $result);
         return $result;
     }
     OrmCache::storeInternal($cache_key, $tbl['sql'], $result);
     return $result;
 }
Example #2
0
 protected function loadAll()
 {
     if (is_null($this->items)) {
         $this->items = Orm::collection($this->source)->order('sort')->findAll();
     }
 }
Example #3
0
 /**
  * @param $key
  * @param $value
  * @return mixed
  */
 public function __set($key, $value)
 {
     if (isset($this->_has_one[$key]) && isset($this->_has_one[$key]['entity']) && is_object($value) && is_a($value, Orm::ENTITY_BASE_CLASS)) {
         $entity_info = $value->info();
         // creating field name
         $field = isset($this->_has_one[$key]['foreign_key']) ? $this->_has_one[$key]['foreign_key'] : $this->_table . '_id';
         if ($entity_info['entity'] == $this->_has_one[$key]['entity']) {
             /* @todo remove this \|/ condition! 
              */
             //only update the field if it's different from $value
             //if(!$value->fieldExists($field) || $value[$field] != $this->getId()) {
             $this->addJob(self::QUEUE_POST_STORE, self::QUEUE_TYPE_SET_VAL, array('object_left' => $value, 'field_left' => $field, 'object_right' => $this, 'field_right' => $this->_primary_key));
             $this->addJob(self::QUEUE_POST_STORE, self::QUEUE_TYPE_EXEC, array('object' => $value, 'method' => 'store'));
             //}
         }
     } elseif (isset($this->_belongs_to[$key]) && isset($this->_belongs_to[$key]['entity'])) {
         // getting entity info
         $entity_info = Orm::entityInfo($this->_belongs_to[$key]['entity']);
         // creating field name
         $field = isset($this->_belongs_to[$key]['foreign_key']) ? $this->_belongs_to[$key]['foreign_key'] : $entity_info['table'] . '_id';
         // going on if the value is an object based on Entity class
         if ($entity_info['entity'] == $this->_belongs_to[$key]['entity'] && is_object($value) && is_a($value, Orm::ENTITY_BASE_CLASS)) {
             // just setting the id if the object is already loaded
             if ($value->loaded()) {
                 $this->setFieldValue($field, $value->getId());
             } else {
                 // we got a new object - adding the job to save it and set the realtion after
                 $this->addJob(self::QUEUE_PRE_STORE, self::QUEUE_TYPE_EXEC, array('object' => $value, 'method' => 'store'));
                 $this->addJob(self::QUEUE_PRE_STORE, self::QUEUE_TYPE_SET_VAL, array('object_left' => $this, 'field_left' => $field, 'object_right' => $value, 'field_right' => $entity_info['primary_key']));
             }
         } else {
             // we got a numeric ID, just setting it
             $this->setFieldValue($field, $value);
         }
     } elseif (isset($this->_has_many[$key]) && isset($this->_has_many[$key]['entity'])) {
         if (!is_array($value)) {
             return;
         }
         // creating field name
         $field = isset($this->_has_many[$key]['foreign_key']) ? $this->_has_many[$key]['foreign_key'] : $this->_table . '_id';
         foreach ($value as $num => $object) {
             $entity_info = null;
             // create fake object if we got array of IDs as $value
             if (is_numeric($object)) {
                 $entity_info = Orm::entityInfo($this->_has_many[$key]['entity']);
                 $object = Orm::collection($this->_has_many[$key]['entity'])->create(array($entity_info['primary_key'] => $object));
             }
             if ($entity_info['entity'] == $this->_has_many[$key]['entity']) {
                 //only update the field if it's different from $value
                 if (!isset($object[$field]) || $object[$field] != $this->getId()) {
                     $this->addJob(self::QUEUE_POST_STORE, self::QUEUE_TYPE_SET_VAL, array('object_left' => $object, 'field_left' => $field, 'object_right' => $this, 'field_right' => $this->_primary_key));
                     $this->addJob(self::QUEUE_POST_STORE, self::QUEUE_TYPE_EXEC, array('object' => $object, 'method' => 'store'));
                 }
             }
         }
     } elseif (isset($this->_has_many_through[$key]) && isset($this->_has_many_through[$key]['entity'])) {
         $join_table = isset($this->_has_many_through[$key]['join_table']) ? $this->_has_many_through[$key]['join_table'] : null;
         $base_table_key = isset($this->_has_many_through[$key]['base_table_key']) ? $this->_has_many_through[$key]['base_table_key'] : null;
         $associated_table_key = isset($this->_has_many_through[$key]['associated_table_key']) ? $this->_has_many_through[$key]['associated_table_key'] : null;
         $join_table_fields = isset($this->_has_many_through[$key]['join_table_fields']) ? $this->_has_many_through[$key]['join_table_fields'] : null;
         // assume we have an array of objects (or just IDs)
         if (is_array($value)) {
             $related_objects = array();
             $entity_info = null;
             foreach ($value as $object) {
                 // create fake object if we got array of IDs as $value
                 if (is_numeric($object)) {
                     $entity_info = Orm::entityInfo($this->_has_many_through[$key]['entity']);
                     $object = Orm::collection($this->_has_many_through[$key]['entity'])->create(array($entity_info['primary_key'] => $object));
                 }
                 // can't go on if it's not an Entity
                 if (!is_a($object, Orm::ENTITY_BASE_CLASS)) {
                     continue;
                 }
                 // getting entity info
                 if (!$entity_info) {
                     $entity_info = $object->info();
                 }
                 // and finally, if entity name is the same as defined in class, setting the relation
                 if ($entity_info['entity'] == $this->_has_many_through[$key]['entity']) {
                     $this->addJob(self::QUEUE_POST_STORE, self::QUEUE_TYPE_EXEC, array('object' => $this, 'method' => 'attachThrough', 'params' => array($object, $join_table, $base_table_key, $associated_table_key, $join_table_fields)));
                     $related_objects[] = $object;
                 }
             }
             // removing old relations
             $this->addJob(self::QUEUE_POST_STORE, self::QUEUE_TYPE_EXEC, array('object' => $this, 'method' => 'clearUnrelated', 'params' => array($this->_has_many_through[$key]['entity'], $related_objects, $join_table, $base_table_key, $associated_table_key)));
         } elseif (empty($value)) {
             // clear all relationships
             $this->addJob(self::QUEUE_PRE_STORE, self::QUEUE_TYPE_EXEC, array('object' => $this, 'method' => 'clearRelations', 'params' => array($this->_has_many_through[$key]['entity'], $join_table, $base_table_key)));
         }
     } else {
         $this->setFieldValue($key, $value);
     }
 }
Example #4
0
 public function getAnonUser()
 {
     return Orm::collection('User')->where('username = ?', array($this->anon_name))->findOne();
 }