function find($magic_params = array())
 {
     if ($this->is_owner_new) {
         throw new lmbException('Not implemented for in memory collection');
     }
     if (is_string($magic_params) || is_object($magic_params)) {
         $magic_params = array('criteria' => lmbSQLCriteria::objectify($magic_params));
     }
     if (isset($this->default_params['criteria'])) {
         if (isset($magic_params['criteria'])) {
             $magic_params['criteria']->addAnd($this->default_params['criteria']);
         } else {
             $magic_params['criteria'] = $this->default_params['criteria'];
         }
     }
     if (!isset($magic_params['sort']) && isset($this->default_params['sort'])) {
         $magic_params['sort'] = $this->default_params['sort'];
     }
     if (!isset($magic_params['join'])) {
         $magic_params['join'] = $this->join_relations;
     }
     if (!isset($magic_params['attach'])) {
         $magic_params['attach'] = $this->attach_relations;
     }
     $query = $this->_createARQuery($magic_params);
     $rs = $query->fetch();
     return $this->_applyDecorators($rs);
 }
 static function findRoot($class_name = '')
 {
     if (!$class_name) {
         $class_name = self::_getCallingClass();
     }
     return lmbActiveRecord::findOne($class_name, lmbSQLCriteria::equal('parent_id', 0));
 }
 function doDisplay()
 {
     if (!($id = $this->request->getInteger('id'))) {
         $this->is_root = true;
         $criteria = new lmbSQLCriteria('parent_id > 0');
         $criteria->addAnd(new lmbSQLCriteria('level = 1'));
         $this->item = lmbCmsDocument::findRoot();
     } else {
         $this->is_root = false;
         if (!($this->item = $this->_getObjectByRequestedId())) {
             return $this->forwardTo404();
         }
         $criteria = new lmbSQLCriteria('parent_id = ' . $this->item->getId());
     }
     $this->items = lmbActiveRecord::find($this->_object_class_name, array('criteria' => $criteria, 'sort' => array('priority' => 'ASC')));
     $this->_applySortParams();
 }
 function check($value)
 {
     $criteria = lmbSQLCriteria::equal($this->field_name, $value)->addAnd('parent_id = ' . ($this->parent_id ? $this->parent_id : $this->node->getParent()->getId()));
     if (!$this->node->isNew()) {
         $criteria->addAnd('id <> ' . $this->node->getId());
     }
     if (lmbActiveRecord::findOne(get_class($this->node), $criteria)) {
         $this->error($this->error_message);
     }
 }
 function check($value)
 {
     $criteria = lmbSQLCriteria::equal($this->field_name, $value);
     if (!$this->object->isNew()) {
         $criteria->addAnd(new lmbSQLFieldCriteria('id', $this->object->getId(), lmbSQLFieldCriteria::NOT_EQUAL));
     }
     $records = lmbActiveRecord::find($this->class, $criteria);
     if ($records->count()) {
         $this->error('Значение в поле {Field} уже занято');
     }
 }
 /**
  * @param string $uri
  * @return lmbCmsDocument
  */
 static function findByUri($uri)
 {
     $identifiers = explode('/', rtrim($uri, '/'));
     $criteria = new lmbSQLCriteria('level = 0');
     $level = 0;
     foreach ($identifiers as $identifier) {
         $identifier_criteria = lmbSQLCriteria::equal('identifier', $identifier);
         $identifier_criteria->addAnd(lmbSQLCriteria::equal('level', $level));
         $criteria->addOr($identifier_criteria);
         $level++;
     }
     $documents = lmbActiveRecord::find('lmbCmsDocument', $criteria);
     $parent_id = 0;
     foreach ($identifiers as $identifier) {
         if (!($document = self::_getNodeByParentIdAndIdentifier($documents, $parent_id, $identifier))) {
             return false;
         }
         $parent_id = $document->getId();
     }
     return $document;
 }
 static function findOneByIdentifier($identifier)
 {
     if ($block = lmbActiveRecord::findOne('lmbCmsTextBlock', lmbSQLCriteria::equal('identifier', $identifier))) {
         return $block;
     }
     if (!($default_content = lmbToolkit::instance()->getConf('text_blocks')->get($identifier))) {
         return null;
     }
     $block = new lmbCmsTextBlock();
     $block->import($default_content);
     $block->setIdentifier($identifier);
     return $block;
 }
Exemple #8
0
 /**
  * @param string|object $class_name_or_obj
  * @param array $params
  * @param lmbDbConnection $conn
  * @param string $sql
  * @return lmbARQuery
  */
 static function create($class_name_or_obj, $params = array(), $conn = null, $sql = '')
 {
     if (!$conn) {
         $conn = lmbToolkit::instance()->getDefaultDbConnection();
     }
     if (!is_object($class_name_or_obj)) {
         $class_name_or_obj = new $class_name_or_obj();
     }
     $query = new lmbARQuery($class_name_or_obj, $conn, $sql, $params);
     if (isset($params['criteria']) && $params['criteria']) {
         $criteria = lmbSQLCriteria::objectify($params['criteria']);
     } else {
         $criteria = lmbSQLCriteria::create();
     }
     $has_class_criteria = false;
     if (isset($params['class'])) {
         $filter_object = new $params['class']();
         $criteria = $filter_object->addClassCriteria($criteria);
         $has_class_criteria = true;
     }
     if (!$has_class_criteria) {
         $criteria = $class_name_or_obj->addClassCriteria($criteria);
     }
     $query->where($criteria);
     $sort_params = isset($params['sort']) && $params['sort'] ? $params['sort'] : $class_name_or_obj->getDefaultSortParams();
     $query->order($sort_params);
     if (isset($params['group']) && $params['group']) {
         $query->group($params['group']);
     }
     $join = isset($params['join']) && $params['join'] ? $params['join'] : array();
     if (!is_array($join)) {
         $join = explode(',', $join);
     }
     foreach ($join as $relation_name => $params_or_relation_name) {
         if (is_numeric($relation_name)) {
             $query->eagerJoin(trim($params_or_relation_name));
         } else {
             $query->eagerJoin(trim($relation_name), $params_or_relation_name);
         }
     }
     $attach = isset($params['attach']) && $params['attach'] ? $params['attach'] : array();
     if (!is_array($attach)) {
         $attach = explode(',', $attach);
     }
     foreach ($attach as $relation_name => $params_or_relation_name) {
         if (is_numeric($relation_name)) {
             $query->eagerAttach(trim($params_or_relation_name));
         } else {
             $query->eagerAttach(trim($relation_name), $params_or_relation_name);
         }
     }
     return $query;
 }
 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 addCriteria($criteria)
 {
     $this->_criterias[] = lmbSQLCriteria::objectify($criteria);
     $this->_registerHint('where');
     return $this;
 }
 function testFindWithKind()
 {
     $valid_object1 = new BarFooOneTableTestObject();
     $valid_object1->setTitle('title1');
     $valid_object1->save();
     $valid_object2 = new BarFooOneTableTestObject();
     $valid_object2->setTitle('title2');
     $valid_object2->save();
     $wrong_class_object = new FooOneTableTestObject();
     $wrong_class_object->setTitle('title1');
     $wrong_class_object->save();
     $wrong_title_object = new FooOneTableTestObject();
     $wrong_title_object->setTitle('wrong_title');
     $wrong_title_object->save();
     $criteria = new lmbSQLCriteria();
     $criteria->add(lmbSQLCriteria::equal('title', 'title1'));
     $criteria->addOr(lmbSQLCriteria::equal('title', 'title2'));
     $records = lmbActiveRecord::find('BarFooOneTableTestObject', $criteria)->sort(array('id'))->getArray();
     $this->assertEqual(count($records), 2);
     $this->assertEqual($records[0]->title, $valid_object1->title);
     $this->assertEqual($records[1]->title, $valid_object2->title);
 }
 private function _getKeyCriteria($resolved_key, $column = 'key')
 {
     return lmbSQLCriteria::equal($column, $resolved_key);
 }
Exemple #13
0
 function delete($table, $criteria = null)
 {
     $query = new lmbDeleteQuery($table, $this->conn);
     if ($criteria) {
         $query->addCriteria(lmbSQLCriteria::objectify($criteria));
     }
     $this->stmt = $query->getStatement($this->conn);
     $this->stmt->execute();
     return $this;
 }
 function addHaving($criteria)
 {
     $this->_having[] = lmbSQLCriteria::objectify($criteria);
     $this->_registerHint('having');
     return $this;
 }
 function addOr($criteria)
 {
     $this->criteria[] = lmbSQLCriteria::objectify($criteria);
     $this->conjunctions[] = self::_OR_;
     return $this;
 }
 function testFetchNested_AttachProperty_In_JoinProperty_In_Attach()
 {
     $course1 = $this->creator->createCourse();
     $course2 = $this->creator->createCourse();
     $alt_course1 = $this->creator->createCourse();
     $alt_course2 = $this->creator->createCourse();
     $lecture1 = $this->creator->createLecture($course1, $alt_course2);
     $lecture2 = $this->creator->createLecture($course2, $alt_course1);
     $lecture3 = $this->creator->createLecture($course1, $alt_course2);
     $lecture4 = $this->creator->createLecture($course1, $alt_course1);
     $lecture5 = $this->creator->createLecture($alt_course2);
     $lecture6 = $this->creator->createLecture($alt_course1);
     $lecture7 = $this->creator->createLecture($alt_course2);
     $lecture8 = $this->creator->createLecture($alt_course1);
     $this->conn->resetStats();
     $query = lmbARQuery::create('CourseForTest', array(), $this->conn);
     $query->where(lmbSQLCriteria::in('id', array($course1->getId(), $course2->getId())));
     $arr = $query->eagerAttach('lectures', array('join' => array('alt_course' => array('attach' => 'lectures'))))->fetch()->getArray();
     $this->assertEqual($this->conn->countQueries(), 3);
     //make sure we really eager fetching
     $this->db->delete('lecture_for_test');
     $this->db->delete('course_for_test');
     $this->conn->resetStats();
     $this->assertIsA($arr[0], 'CourseForTest');
     $this->assertEqual($arr[0]->getTitle(), $course1->getTitle());
     $lectures = $arr[0]->getLectures()->getArray();
     $this->assertEqual(count($lectures), 3);
     $this->assertEqual($lectures[0]->getId(), $lecture1->getId());
     $this->assertEqual($lectures[0]->getAltCourse()->getTitle(), $alt_course2->getTitle());
     $alt_course_lectures = $lectures[0]->getAltCourse()->getLectures();
     $this->assertEqual($alt_course_lectures[0]->getId(), $lecture5->getId());
     $this->assertEqual($alt_course_lectures[1]->getId(), $lecture7->getId());
     $this->assertEqual($lectures[1]->getId(), $lecture3->getId());
     $this->assertEqual($lectures[1]->getAltCourse()->getTitle(), $alt_course2->getTitle());
     $alt_course_lectures = $lectures[1]->getAltCourse()->getLectures();
     $this->assertEqual($alt_course_lectures[0]->getId(), $lecture5->getId());
     $this->assertEqual($alt_course_lectures[1]->getId(), $lecture7->getId());
     $this->assertEqual($lectures[2]->getId(), $lecture4->getId());
     $this->assertEqual($lectures[2]->getAltCourse()->getTitle(), $alt_course1->getTitle());
     $alt_course_lectures = $lectures[2]->getAltCourse()->getLectures();
     $this->assertEqual($alt_course_lectures[0]->getId(), $lecture6->getId());
     $this->assertEqual($alt_course_lectures[1]->getId(), $lecture8->getId());
     $this->assertIsA($arr[1], 'CourseForTest');
     $this->assertEqual($arr[1]->getTitle(), $course2->getTitle());
     $lectures = $arr[1]->getLectures()->getArray();
     $this->assertEqual(count($lectures), 1);
     $this->assertEqual($lectures[0]->getId(), $lecture2->getId());
     $this->assertEqual($lectures[0]->getAltCourse()->getTitle(), $alt_course1->getTitle());
     $alt_course_lectures = $lectures[0]->getAltCourse()->getLectures();
     $this->assertEqual($alt_course_lectures[0]->getId(), $lecture6->getId());
     $this->assertEqual($alt_course_lectures[1]->getId(), $lecture8->getId());
     $this->assertEqual($this->conn->countQueries(), 0);
 }
 function testLess()
 {
     $criteria = lmbSQLCriteria::less('id', 12);
     $this->assertEqual($criteria, new lmbSQLFieldCriteria('id', 12, lmbSQLFieldCriteria::LESS));
 }
Exemple #18
0
 protected function _getInheritanceCriteria()
 {
     return lmbSQLCriteria::like($this->getInheritanceField(), $this->_getInheritancePath() . "%");
 }
 function remove($object)
 {
     $table = new lmbTableGateway($this->relation_info['table'], $this->conn);
     $criteria = new lmbSQLCriteria();
     $criteria->addAnd(lmbSQLCriteria::equal($this->relation_info['field'], $this->owner->getId()));
     $criteria->addAnd(lmbSQLCriteria::equal($this->relation_info['foreign_field'], $object->getId()));
     $table->delete($criteria);
     $this->reset();
 }