function rewind()
 {
     foreach ($this->attach_relations as $relation_name => $params) {
         if (!in_array($relation_name, array_keys($this->loaded_attaches))) {
             $relation_type = $this->base_object->getRelationType($relation_name);
             $relation_info = $this->base_object->getRelationInfo($relation_name);
             $relation_class = $relation_info['class'];
             $relation_object = new $relation_class(null, $this->conn);
             switch ($relation_type) {
                 case lmbActiveRecord::HAS_ONE:
                 case lmbActiveRecord::MANY_BELONGS_TO:
                     $ids = lmbArrayHelper::getColumnValues($this->prefix . $relation_info['field'], $this->iterator);
                     if (!count($ids)) {
                         $this->loaded_attaches[$relation_name] = array();
                     } else {
                         $attached_objects = lmbActiveRecord::findByIds($relation_class, $ids, $params, $this->conn);
                         $this->loaded_attaches[$relation_name] = lmbCollection::toFlatArray($attached_objects, $key_field = $relation_object->getPrimaryKeyName(), $export_each = false);
                     }
                     break;
                 case lmbActiveRecord::BELONGS_TO:
                     $ids = lmbArrayHelper::getColumnValues($this->prefix . $this->base_object->getPrimaryKeyName(), $this->iterator);
                     if (!count($ids)) {
                         $this->loaded_attaches[$relation_name] = array();
                     } else {
                         $criteria = lmbSQLCriteria::in($relation_info['field'], $ids);
                         $params['criteria'] = isset($params['criteria']) ? $params['criteria']->addAnd($criteria) : $criteria;
                         $attached_objects = lmbActiveRecord::find($relation_class, $params, $this->conn);
                         $this->loaded_attaches[$relation_name] = lmbCollection::toFlatArray($attached_objects, $key_field = $relation_info['field'], $export_each = false);
                     }
                     break;
                 case lmbActiveRecord::HAS_MANY:
                     if (!isset($params['sort'])) {
                         $params['sort'] = $relation_object->getDefaultSortParams();
                     }
                     $params['sort'] = array($relation_info['field'] => 'ASC') + $params['sort'];
                     $query = lmbAROneToManyCollection::createFullARQueryForRelation($relation_info, $this->conn, $params);
                     $ids = lmbArrayHelper::getColumnValues($this->prefix . $this->base_object->getPrimaryKeyName(), $this->iterator);
                     $this->loaded_attaches[$relation_name] = array();
                     if (!count($ids)) {
                         break;
                     }
                     $query->addCriteria(lmbSQLCriteria::in($relation_info['field'], $ids));
                     $attached_objects = $query->fetch();
                     foreach ($attached_objects as $attached_object) {
                         $this->loaded_attaches[$relation_name][$attached_object->get($relation_info['field'])][] = $attached_object;
                     }
                     break;
                 case lmbActiveRecord::HAS_MANY_TO_MANY:
                     if (!isset($params['sort'])) {
                         $params['sort'] = $relation_object->getDefaultSortParams();
                     }
                     $params['sort'] = array($relation_info['field'] => 'ASC') + $params['sort'];
                     $query = lmbARManyToManyCollection::createFullARQueryForRelation($relation_info, $this->conn, $params);
                     $query->addField($relation_info['table'] . '.' . $relation_info['field'], "link__id");
                     $ids = lmbArrayHelper::getColumnValues($this->prefix . $this->base_object->getPrimaryKeyName(), $this->iterator);
                     $this->loaded_attaches[$relation_name] = array();
                     if (!count($ids)) {
                         break;
                     }
                     $query->addCriteria(lmbSQLCriteria::in($relation_info['field'], $ids));
                     $attached_objects = $query->fetch();
                     foreach ($attached_objects as $attached_object) {
                         $this->loaded_attaches[$relation_name][$attached_object->get("link__id")][] = $attached_object;
                     }
                     break;
             }
         }
     }
     parent::rewind();
 }
 function _createDataSet()
 {
     if (!$this->class_path) {
         throw new lmbException('Class path is not defined!');
     }
     $class_path = new lmbClassPath($this->class_path);
     $class_path->import();
     $class_name = $class_path->getClassName();
     if (is_null($this->record_id) && is_null($this->record_ids)) {
         if (!$this->find) {
             return lmbActiveRecord::find($class_name);
         } else {
             $method = 'find' . lmb_camel_case($this->find);
             $callback = array($class_name, $method);
             if (!is_callable($callback)) {
                 throw new lmbException('Active record of class "' . $class_name . '" does not support method "' . $method . '"');
             }
             return call_user_func_array($callback, $this->find_params);
         }
     }
     if ($this->record_id) {
         try {
             if ($this->find) {
                 $method = 'find' . lmb_camel_case($this->find);
                 $callback = array($class_name, $method);
                 if (!is_callable($callback)) {
                     throw new lmbException('Active record of class "' . $class_name . '" does not support method "' . $method . '"');
                 }
                 $record = call_user_func_array($callback, array($this->record_id));
             } else {
                 $record = lmbActiveRecord::findById($class_name, $this->record_id);
             }
         } catch (lmbARNotFoundException $e) {
             $record = array();
         }
         return $this->_singleItemCollection($record);
     } elseif ($this->record_ids) {
         return lmbActiveRecord::findByIds($class_name, $this->record_ids);
     }
     return new lmbCollection();
 }
Exemplo n.º 3
0
 function testFindByIdsReturnEmptyIteratorIfNoIds()
 {
     $object1 = $this->creator->createOneTableObject();
     $object2 = $this->creator->createOneTableObject();
     $rs = lmbActiveRecord::findByIds('TestOneTableObject', array());
     $rs->rewind();
     $this->assertFalse($rs->valid());
     //testing convenient alias
     $rs = TestOneTableObject::findByIds(array());
     $rs->rewind();
     $this->assertFalse($rs->valid());
 }
 function doDelete()
 {
     if (!$this->request->hasPost()) {
         $ids = $this->request->get('ids');
     } else {
         $ids = $this->request->getPost('ids');
     }
     if (!is_array($ids)) {
         $ids = array($ids);
     }
     $this->items = lmbActiveRecord::findByIds($this->_object_class_name, $ids);
     if (!$this->request->hasPost()) {
         return;
     }
     $this->_onBeforeDelete();
     foreach ($this->items as $item) {
         $item->destroy();
     }
     $this->_onAfterDelete();
     $this->_endDialog();
 }