Exemplo n.º 1
0
 /**
  * @return lmbActiveRecord
  */
 protected function _getObjectByRequestedId($throw_exception = false)
 {
     if (!($id = $this->request->getInteger('id'))) {
         return false;
     }
     if (!($item = lmbActiveRecord::findById($this->_object_class_name, $id, $throw_exception))) {
         return false;
     }
     return $item;
 }
 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);
 }
Exemplo n.º 3
0
 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();
     }
 }
Exemplo n.º 4
0
 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');
 }
Exemplo n.º 5
0
 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 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 testGetChildren()
 {
     $object1 = $this->_createDocument('root', $parent_node = null);
     $object2 = $this->_createDocument('folder1', $object1);
     $object3 = $this->_createDocument('child1', $object2);
     $object4 = $this->_createDocument('child2', $object2);
     $object5 = lmbActiveRecord::findById('lmbCmsDocument', $object2->getId());
     $children = $object5->getKids();
     $children->rewind();
     $this->assertTrue($children->valid());
     $child1 = $children->current();
     $this->assertEqual($child1->title, $object3->title);
     $children->next();
     $this->assertTrue($children->valid());
     $child2 = $children->current();
     $this->assertEqual($child2->title, $object4->title);
 }
 function testRemoveAllWithExistingOwner()
 {
     $l1 = $this->_createLecture();
     $l2 = $this->_createLecture();
     $course = $this->_createCourseAndSave(array($l1, $l2));
     $collection = new lmbAROneToManyCollection('lectures', $course);
     $collection->removeAll();
     $course2 = lmbActiveRecord::findById('CourseForTest', $course->getId());
     $collection = new lmbAROneToManyCollection('lectures', $course2);
     $this->assertEqual(sizeof($collection->getArray()), 0);
 }
 function testParentDeleteAllDeletesChildren()
 {
     $person = $this->creator->initPerson();
     $number = $this->creator->initSocialSecurity();
     $person->setSocialSecurity($number);
     $person_id = $person->save();
     $number_id = $number->getId();
     //this one should stay
     $untouched_number = $this->creator->initSocialSecurity();
     $untouched_number->save();
     lmbActiveRecord::delete('PersonForTest');
     $this->assertNull(lmbActiveRecord::findFirst('SocialSecurityForTest', array('criteria' => lmbActiveRecord::getDefaultConnection()->quoteIdentifier("id") . '= ' . $number_id)));
     $this->assertNull(lmbActiveRecord::findFirst('PersonForTest', array('criteria' => lmbActiveRecord::getDefaultConnection()->quoteIdentifier("id") . '= ' . $person_id)));
     $number2 = lmbActiveRecord::findById('SocialSecurityForTest', $untouched_number->getId());
     $this->assertEqual($number2->getCode(), $untouched_number->getCode());
 }
 function testSetFlushesPreviousCollectionInDatabaseToo()
 {
     $course = $this->_initCourse();
     $l1 = new LectureForTest();
     $l1->setTitle('Physics');
     $l2 = new LectureForTest();
     $l2->setTitle('Math');
     $course->addToLectures($l1);
     $course->addToLectures($l2);
     $course->save();
     $course2 = lmbActiveRecord::findById('CourseForTest', $course->getId());
     $l3 = new LectureForTest();
     $l3->setTitle('Math');
     $course2->setLectures(array($l3));
     $course2->save();
     $course3 = lmbActiveRecord::findById('CourseForTest', $course->getId());
     $lectures = $course3->getLectures();
     $this->assertEqual($lectures->count(), 1);
     $this->assertEqual($lectures->at(0)->getTitle(), $l3->getTitle());
 }
 function testDoNotSettingARPrimaryKeyOnAggregatedObjects()
 {
     $image = new ImageForAggregateTest();
     $image->setExtension($extension = 'jpg');
     $photo = new PhotoForTest();
     $photo->setImage($image);
     $photo->save();
     $this->assertNotEqual($photo->getImage()->getPhotoId(), $photo->getId());
     $photo2 = lmbActiveRecord::findById('PhotoForTest', $photo->getId());
     $this->assertEqual($photo2->getImage()->getPhotoId(), $photo2->getId());
     $photo2->getImage()->setExtension($other_extension = 'png');
     $photo2->getImage()->setPhotoId($other_photo_id = $photo2->getId() + 10);
     // we try set AR primary key
     $photo2->save();
     $photo3 = lmbActiveRecord::findById('PhotoForTest', $photo2->getId());
     $this->assertEqual($photo3->getImage()->getExtension(), $other_extension);
     $this->assertNotEqual($photo3->getImage()->getPhotoId(), $other_photo_id);
     // affect setting AR primary key
     $this->assertEqual($photo3->getImage()->getPhotoId(), $photo3->getId());
     // AR primary key not updated
 }
 function testRemoveAllWithExistingOwner()
 {
     $group1 = $this->_initGroup();
     $group2 = $this->_initGroup();
     $user = $this->_createUserAndSave(array($group1, $group2));
     $collection = new lmbARManyToManyCollection('groups', $user);
     $collection->removeAll();
     $user2 = lmbActiveRecord::findById('UserForTest', $user->getId());
     $collection = new lmbARManyToManyCollection('groups', $user2);
     $this->assertEqual(sizeof($collection->getArray()), 0);
 }
Exemplo n.º 13
0
 function testDeleteRawByCriteria()
 {
     $object1 = $this->creator->createOneTableObject();
     $object2 = $this->creator->createOneTableObject();
     $criteria = new lmbSQLFieldCriteria('id', $object2->getId());
     lmbActiveRecord::deleteRaw('TestOneTableObject', $criteria);
     $this->assertEqual($this->db->count('test_one_table_object'), 1);
     $found = lmbActiveRecord::findById('TestOneTableObject', $object1->getId());
     $this->assertEqual($found->getContent(), $object1->getContent());
 }
Exemplo n.º 14
0
 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.º 15
0
 function testSaveLoadAggrigatedObjectByGetter()
 {
     $place = new GeoCoordinate();
     $place->setLatitude($latitude = 'latitude');
     $place->setLongitude($longitude = 'longitude');
     $photo = new PhotoForTest();
     $photo->setPlace($place);
     $photo->save();
     $photo2 = lmbActiveRecord::findById('PhotoForTest', $photo->getId());
     $this->assertIsA($photo2->getPlace(), 'GeoCoordinate');
     $this->assertEqual($photo2->getPlace()->getLatitude(), $latitude);
     $this->assertEqual($photo2->getPlace()->getLongitude(), $longitude);
 }
 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 testCustomLazyFieldsInFindById()
 {
     $object = new TestOneTableObject();
     $object->setAnnotation($annotation = "Annotation");
     $object->setContent($content = "Content");
     $object->save();
     $object2 = lmbActiveRecord::findById('TestOneTableObject', array('id' => $object->getId(), '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);
 }
Exemplo n.º 18
0
 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);
 }