/** * @todo fix error (hack) */ function testIsRoot_Positive() { //force tree init $node = $this->_createDocument('root', $parent_node = null); $root = lmbActiveRecord::findOne('lmbCmsDocument', array('sort' => 'id')); $this->assertTrue($root->isRoot()); }
function _defineRelations() { parent::_defineRelations(); $this->_hasOne('has_one_relation', $has_one = array('field' => 'child_id', 'class' => 'ChildClass')); $this->relations['has_one_relation'] = $has_one; $this->_hasOne('other_has_one_relation', $other_has_one = array('field' => 'other_child_id', 'class' => 'OtherChildClass')); $this->relations['other_has_one_relation'] = $other_has_one; $this->_hasMany('has_many_relation', $has_many = array('field' => 'parent_id', 'class' => 'ManyChildClass')); $this->relations['has_many_relation'] = $has_many; $this->_hasMany('other_has_many_relation', $other_has_many = array('field' => 'other_parent_id', 'class' => 'OtherManyChildClass')); $this->relations['other_has_many_relation'] = $other_has_many; $this->_hasManyToMany('has_many_to_many_relation', $many_to_many = array('field' => 'my_id', 'foreign_field' => 'important_id', 'class' => 'ImportantClass', 'table_name' => 'me2importand_class')); $this->relations['has_many_to_many_relation'] = $many_to_many; $this->_hasManyToMany('other_has_many_to_many_relation', $other_many_to_many = array('field' => 'my_id', 'foreign_field' => 'other_important_id', 'class' => 'OtherImportantClass', 'table_name' => 'me2other_importand_class')); $this->relations['other_has_many_to_many_relation'] = $other_many_to_many; $this->_belongsTo('belongs_to_relation', $belongs_to = array('field' => 'my_id', 'class' => 'ParentClass')); $this->relations['belongs_to_relation'] = $belongs_to; $this->_belongsTo('other_belongs_to_relation', $other_belongs_to = array('field' => 'my_id', 'class' => 'OtherParentClass')); $this->relations['other_belongs_to_relation'] = $other_belongs_to; $this->_manyBelongsTo('many_belongs_to_relation', $many_belongs_to = array('field' => 'parent_id', 'class' => 'ParentClass')); $this->relations['many_belongs_to_relation'] = $many_belongs_to; $this->_manyBelongsTo('other_many_belongs_to_relation', $other_many_belongs_to = array('field' => 'parent_id', 'class' => 'OtherParentClass')); $this->relations['other_many_belongs_to_relation'] = $other_many_belongs_to; $this->_composedOf('value_object', $value_object = array('field' => 'date_start', 'class' => 'lmbDateTime', 'getter' => 'getStamp')); $this->relations['value_object'] = $value_object; $this->_composedOf('other_value_object', $other_value_object = array('field' => 'date_end', 'class' => 'lmbDateTime', 'getter' => 'getStamp')); $this->relations['other_value_object'] = $other_value_object; }
function doDelete() { if ($this->request->hasPost() && $this->request->get('delete')) { foreach ($this->request->getArray('ids') as $id) { $node = lmbActiveRecord::findById('lmbCmsNode', $id); $node->destroy(); } $this->closePopup(); } }
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 = new lmbSQLFieldCriteria($this->field_name, $value); if ($this->text_block->getId()) { $criteria->addAnd('id <> ' . $this->text_block->getId()); } if (lmbActiveRecord::findOne('lmbCmsTextBlock', $criteria)) { $this->error('Тектовый блок со значением поля {Field} уже существует'); } }
function testSaveLoadValueObjects() { $lesson = new LessonForBCTest(); $lesson->setDateStart(new TestingValueObject($v1 = time())); $lesson->setDateEnd(new TestingValueObject($v2 = time() + 100)); $lesson->save(); $lesson2 = lmbActiveRecord::findById('LessonForBCTest', $lesson->getId()); $this->assertEqual($lesson2->getDateStart()->getValue(), $v1); $this->assertEqual($lesson2->getDateEnd()->getValue(), $v2); }
function doApprove() { if (!($user = lmbActiveRecord::findFirst('lmbCmsUser', array('generated_password = ?', $this->request->get('id'))))) { return $this->flashAndRedirect('Вы прошли по неверной ссылке! Убедитесь, что она соответствует ссылке в отправленном вам письме', '/cms_user/forgot_password'); } $user->setHashedPassword($user->getGeneratedPassword()); $user->setGeneratedPassword(''); $user->saveSkipValidation(); $this->flashAndRedirect('Новый пароль активирован', '/cms_user/login'); }
function check($value) { $criteria = new lmbSQLFieldCriteria($this->field_name, $value); if (!$this->user->isNew()) { $criteria->addAnd('id <> ' . $this->user->getId()); } if (lmbActiveRecord::findOne('lmbCmsUser', $criteria)) { $this->error('Пользователь со значением поля {Field} уже существует'); } }
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 doDelete() { $id = $this->request->get('id'); if (!($this->item = lmbActiveRecord::findById($this->_object_class_name, $id, false))) { return $this->flashErrorAndRedirect('Пользователь не найден', '/admin_user'); } if ($this->item->getId() == $this->toolkit->getCmsUser()->getId()) { return $this->flashErrorAndRedirect('Запрещено удалять свою учетную запись', '/admin_user'); } $this->item->destroy(); $this->flash('Пользователь удален'); $this->redirect('/admin_user'); }
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; }
function getUser() { if (is_object($this->user)) { return $this->user; } if ($this->_isValidSession()) { $this->user = lmbActiveRecord::findById('lmbCmsUser', $this->user_id); $this->user->setLoggedIn($this->is_logged_in); } else { $this->user = new lmbCmsUser(); } return $this->user; }
function login($login, $password) { $criteria = new lmbSQLFieldCriteria('login', $login); $user = lmbActiveRecord::findFirst('lmbCmsUser', array('criteria' => $criteria)); if ($user && $user->isPasswordCorrect($password)) { $this->import($user); $this->setIsNew(false); $this->setLoggedIn(true); return true; } else { $this->setLoggedIn(false); return false; } }
function testCreate_ManyToMany() { $this->_createModelAndIncludeThem('user', 'User'); $this->_createModelAndIncludeThem('group', 'Group'); $user = new User(); $user->setFirstName($user_first_name = 'Vasya'); $user->save(); $group = new Group(); $group->setTitle($group_title = 'Moderasti'); $group->getUsers()->add($user); $group->save(); $loaded_user = lmbActiveRecord::findById('User', $user->getId()); $this->assertEqual($loaded_user->getFirstName(), $user_first_name); $this->assertEqual($loaded_user->getGroups()->at(0)->getId(), $group->getId()); $loaded_group = lmbActiveRecord::findById('Group', $group->getId()); $this->assertEqual($loaded_group->getUsers()->at(0)->getId(), $user->getId()); }
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; }
/** * @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 createObjectFromRecord($record, $default_class_name, $conn, $lazy_attributes = null, $use_proxy = false) { if ($path = $record->get(lmbActiveRecord::getInheritanceField())) { $class_name = lmbActiveRecord::getInheritanceClass($record); if (!class_exists($class_name)) { throw new lmbException("Class '{$class_name}' not found"); } } else { $class_name = is_object($default_class_name) ? get_class($default_class_name) : $default_class_name; } if ($use_proxy || lmb_env_get('LIMB_ACTIVE_RECORD_FORCE_PROXY', false)) { $proxy_class = $class_name . '_ARProxy'; lmbARProxy::generate($proxy_class, $class_name); return new $proxy_class($record, $class_name, $conn, $lazy_attributes); } $object = new $class_name(null, $conn); if (is_array($lazy_attributes)) { $object->setLazyAttributes($lazy_attributes); } $object->loadFromRecord($record); return $object; }
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); }
/** * Allows to override default single table inheritance column name * @param string */ static function setInheritanceField($field) { return self::$_inheritance_field = $field; }
function testCustomLazyFieldsInFindFirst() { $object = new TestOneTableObject(); $object->setAnnotation($annotation = "Annotation"); $object->setContent($content = "Content"); $object->save(); $object2 = lmbActiveRecord::findFirst('TestOneTableObject', array('fields' => array('annotation'))); $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 testDeleteAlsoRemovesManyToManyRecords() { $user1 = $this->creator->initUser(); $user2 = $this->creator->initUser(); $group1 = $this->creator->initGroup(); $group2 = $this->creator->initGroup(); $user1->addToGroups($group1); $user1->addToGroups($group2); $user1->save(); $user2->addToGroups($group1); $user2->addToGroups($group2); $user2->save(); $user3 = lmbActiveRecord::findById('UserForTest', $user1->getId()); $user3->destroy(); $this->assertEqual($this->db->count('user_for_test2group_for_test'), 2); $user4 = lmbActiveRecord::findById('UserForTest', $user2->getId()); $groups = $user4->getGroups(); $this->assertEqual($groups->at(0)->getTitle(), $group1->getTitle()); $this->assertEqual($groups->at(1)->getTitle(), $group2->getTitle()); $this->assertEqual($groups->count(), 2); }
function _createValidator() { if ($this->_test_validator) { return $this->_test_validator; } return parent::_createValidator(); }
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 testFindFirstWithNonSavedOwner_TODO() { $l1 = $this->_createLecture(); $l2 = $this->_createLecture(); $course = $this->_createCourse(array($l1, $l2)); try { $lecture = $course->getLectures()->findFirst(lmbActiveRecord::getDefaultConnection()->quoteIdentifier("id") . "=" . $l1->getId() . " OR " . lmbActiveRecord::getDefaultConnection()->quoteIdentifier("id") . "=" . $l2->getId()); $this->assertTrue(false); } catch (lmbException $e) { } }
function testChildRemovalWithRequiredObjectInParentRelationDefinitionThrowsValidationException() { $number = $this->creator->initSocialSecurity(); $person = new PersonForTestWithRequiredSocialSecurity(); $person->setName('Jim'); $person->setSocialSecurity($number); $number->setPerson($person); $person->save(); try { $number->destroy(); $this->assertTrue(false); } catch (lmbValidationException $e) { $this->assertTrue(true); } $number2 = lmbActiveRecord::findFirst('SocialSecurityForTest'); $this->assertNotNull($number2, 'Removal should not be finished'); $this->assertEqual($number2->getId(), $number->getId()); }
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 testFindFirstWithNonSavedOwner_TODO() { $g1 = $this->_initGroup(); $g2 = $this->_initGroup(); $user = $this->_initUser(array($g1, $g2)); try { $group = $user->getGroups()->findFirst(lmbActiveRecord::getDefaultConnection()->quoteIdentifier("group_id") . "=" . $g1->getId() . " OR " . lmbActiveRecord::getDefaultConnection()->quoteIdentifier("group_id") . "=" . $g2->getId()); $this->assertTrue(false); } catch (lmbException $e) { } }
function testUpdateWhileNoDirtyFields() { $object = new TestOneTableObject(); $object->setAnnotation($initial_annotation = 'some annotation'); $object->setContent($initial_content = 'some content'); $object->save(); $object->setAnnotation('some other annotation'); $object->setContent('some other content'); $object->resetPropertyDirtiness('content'); $object->resetPropertyDirtiness('annotation'); $object->save(); $loaded_object = lmbActiveRecord::findById('TestOneTableObject', $object->getId()); $this->assertEqual($loaded_object->getAnnotation(), $initial_annotation); $this->assertEqual($loaded_object->getContent(), $initial_content); }