function testJustFoundObjectIsNotDirty() { $object = new TestOneTableObject(); $object->setContent('test'); $object->save(); $object2 = lmbActiveRecord::find('TestOneTableObject', $object->getId()); $this->assertFalse($object2->isDirty()); }
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} уже занято'); } }
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(); }
protected function _getBlocks() { $blocks = lmbCollection::toFlatArray(lmbActiveRecord::find('lmbCmsTextBlock'), 'identifier'); $result = array(); foreach ($this->toolkit->getConf('text_blocks') as $identifier => $default_properties) { if (isset($blocks[$identifier])) { $item = $blocks[$identifier]; $item['exists'] = true; } else { $item = new lmbCmsTextBlock(); $item->import($default_properties); $item->setIdentifier($identifier); $item['exists'] = false; } $result[$identifier] = $item; } return $result; }
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(); }
/** * @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; }
function testCustomLazyFieldsInFind() { $object = new TestOneTableObject(); $object->setAnnotation($annotation = "Annotation"); $object->setContent($content = "Content"); $object->save(); $rs = lmbActiveRecord::find('TestOneTableObject', array('fields' => array('annotation'))); $object2 = $rs->at(0); $fields = $object2->exportRaw(); //checking which props were actually loaded $this->assertEqual($fields, array('id' => $object->getId(), 'annotation' => $annotation)); //lazy loading in action $this->assertEqual($object2->getAnnotation(), $annotation); $this->assertEqual($object2->getContent(), $content); }
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 doDisplay() { $this->items = lmbActiveRecord::find($this->_object_class_name); }
function testSwapNullableRelations() { $course1 = new CourseWithNullableLectures(); $lectA = new LectureIndependentFromCourse(); $lectA->setTitle("Lecture A"); $lectB = new LectureIndependentFromCourse(); $lectB->setTitle("Lecture B"); $course1->setLectures(array($lectA, $lectB)); $course1->setTitle("Course 1"); $course2 = new CourseWithNullableLectures(); $lectC = new LectureIndependentFromCourse(); $lectC->setTitle("Lecture C"); $lectD = new LectureIndependentFromCourse(); $lectD->setTitle("Lecture D"); $course2->setLectures(array($lectC, $lectD)); $course2->setTitle("Course 2"); $course1->save(); $course2->save(); $c1 = $course1->export(); $c2 = $course2->export(); $c1['lectures'] = $course2->getLectures()->getIds(); $c2['lectures'] = $course1->getLectures()->getIds(); try { $course1->import($c1); $course1->save(); $course2 = new CourseWithNullableLectures($course2->getId()); $course2->import($c2); $c2 = $course2->save(); } catch (lmbARException $e) { } $this->assertEqual(lmbActiveRecord::find("LectureForTest")->count(), 4); }
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); }
function testUpdateRawWithCriteria() { $object1 = $this->creator->createOneTableObject(); $object2 = $this->creator->createOneTableObject(); lmbActiveRecord::updateRaw('TestOneTableObject', array('content' => 'blah'), lmbActiveRecord::getDefaultConnection()->quoteIdentifier("id") . '=' . $object2->getId()); $rs = lmbActiveRecord::find('TestOneTableObject'); $rs->rewind(); $this->assertEqual($rs->current()->getContent(), $object1->getContent()); $rs->next(); $this->assertEqual($rs->current()->getContent(), 'blah'); $rs->next(); $this->assertFalse($rs->valid()); }
} echo "{$counter}) lmbActiveRecord fetching, attr access: " . (microtime(true) - $mark) . "\n"; } if (++$counter == $test || $test == null) { $mark = microtime(true); for ($i = 0; $i < 1000; $i++) { $rs = lmbActiveRecord::find('Foo', array('proxy' => true)); foreach ($rs as $obj) { $foo = $obj->bar; } } echo "{$counter}) lmbActiveRecord fetching(proxied), attr access: " . (microtime(true) - $mark) . "\n"; } if (++$counter == $test || $test == null) { $mark = microtime(true); $rs = lmbActiveRecord::find('Foo'); for ($i = 0; $i < 1000; $i++) { foreach ($rs as $obj) { $foo = $obj->bar; } } echo "{$counter}) lmbActiveRecord fetching(out of loop), attr access: " . (microtime(true) - $mark) . "\n"; } if (++$counter == $test || $test == null) { $mark = microtime(true); for ($i = 0; $i < 1000; $i++) { $rs = Foo::find(); foreach ($rs as $obj) { $foo = $obj->get('bar'); } }
function doDisplay() { $this->items = lmbActiveRecord::find($this->_object_class_name); $this->_applySortParams(); }