function testLoadingWithMultipleAssociations()
 {
     $articles = SActiveStore::findAll('Article', Null, array('include' => array('comments', 'categories')));
     $this->assertTrue($articles[0]->comments->isLoaded());
     $this->assertEqual(2, $articles[0]->countComments());
     $this->assertTrue($articles[0]->categories->isLoaded());
     $this->assertEqual(3, $articles[0]->countCategories());
 }
 function testAssociations()
 {
     $company = new DummyDecorator(SActiveStore::findByPk('Company', 1), 'test');
     $this->assertEqual(1, $company->products->count());
     $this->assertEqual(1, $company->countProducts());
     $product = new Product(array('name' => 'CD-R', 'price' => '0.75'));
     $company->products[] = $product;
     $this->assertEqual(2, $company->products->count());
     $this->assertEqual(2, $company->countProducts());
     $company->save();
     $companyReloaded = new DummyDecorator(SActiveStore::findByPk('Company', 1), 'test');
     $this->assertEqual(2, $companyReloaded->products->count());
     $this->assertEqual(2, $companyReloaded->countProducts());
 }
 public function __construct($values = Null, $dontInitAssocs = false, $newRecord = True)
 {
     if ($this->tableName == Null) {
         $this->tableName = SInflection::pluralize(strtolower(get_class($this)));
     }
     if (empty($this->attributes)) {
         $this->attributes = SActiveStore::getAttributes($this->tableName);
     } else {
         $this->initAttributes();
     }
     $this->initValues();
     if ($values != Null && is_array($values)) {
         $this->populate($values);
     }
     $this->newRecord = $newRecord;
     if (!$dontInitAssocs) {
         $this->initAssociations();
     }
 }
示例#4
0
 public static function validateUniqueness($record, $attr, $options = array())
 {
     $config = array('message' => 'ERR_VALID_UNIQUE', 'on' => 'save', 'scope' => Null);
     $config = array_merge($config, $options);
     $value = $record->{$attr};
     $conditionsSql = $attr . ' ' . self::attributeCondition($value);
     $conditionsValues = array($value);
     if ($config['scope'] !== Null) {
         $scopeValue = $record->readAttribute($config['scope']);
         $conditionsSql .= ' AND ' . $config['scope'] . ' ' . self::attributeCondition($scopeValue);
         $conditionsValues[] = $scopeValue;
     }
     if (!$record->isNewRecord()) {
         $conditionsSql .= ' AND ' . $record->identityField . ' <> ?';
         $conditionsValues[] = $record->id;
     }
     if (SActiveStore::findFirst(get_class($record), array($conditionsSql, $conditionsValues))) {
         self::addError($record, $attr, $config['message']);
     }
 }
 function testSaveWithTimestamps()
 {
     $created_date = new SDateTime(2005, 12, 01, 20, 30, 00);
     $post = SActiveStore::findByPk('Post', 2);
     $this->assertIsA($post->created_on, 'SDateTime');
     $this->assertEqual($created_date, $post->created_on);
     $this->assertEqual($post->created_on, $post->updated_on);
     $post->text = 'blo blo blo';
     $post->save();
     $this->assertNotEqual($post->created_on, $post->updated_on);
     $today = SDate::today();
     $new_post = new Post(array('title' => 'Timestamps and MySQL', 'author' => 'Goldoraf', 'text' => 'ttttt'));
     $this->assertNull($new_post->created_on);
     $this->assertNull($new_post->updated_on);
     $new_post->save();
     $this->assertIsA($new_post->created_on, 'SDateTime');
     $this->assertIsA($new_post->created_on, 'SDateTime');
     $this->assertEqual($new_post->created_on, $new_post->updated_on);
     $this->assertEqual($today->year, $new_post->created_on->year);
     $this->assertEqual($today->month, $new_post->created_on->month);
     $this->assertEqual($today->day, $new_post->created_on->day);
 }
示例#6
0
 protected function incrementPositionsOnAllItems()
 {
     SActiveStore::updateAll(get_class($this), "{$this->positionField()} = ({$this->positionField()} + 1)", $this->scopeCondition());
 }
 protected function incrementPositionsOnAllItems()
 {
     SActiveStore::updateAll(get_class($this->record), "{$this->column} = ({$this->column} + 1)", $this->scopeCondition());
 }
 protected function countRecords($condition)
 {
     return SActiveStore::count($this->assocClass, $this->constructSql($condition));
 }
示例#9
0
 public function hitsCount()
 {
     return SActiveStore::count($this->className, $this->condition);
 }
 public function testMigratorGoingDownDueToVersionTarget()
 {
     SMigrator::up(dirname(__FILE__) . '/fixtures/migrate', 1);
     SMigrator::migrate(dirname(__FILE__) . '/fixtures/migrate', 0);
     $this->assertFalse(in_array('last_name', array_keys(SActiveRecord::connection()->columns('people'))));
     $this->assertFalse(SActiveStore::tableExists('reminders'));
     SMigrator::migrate(dirname(__FILE__) . '/fixtures/migrate');
     $this->assertEqual(2, SMigrator::currentVersion());
     SActiveStore::resetAttributeInformation('people');
     $this->assertTrue(in_array('last_name', array_keys(SActiveRecord::connection()->columns('people'))));
     $r = new Reminder(array('content' => 'hello world', 'remind_at' => SDateTime::today()));
     $r->save();
     $this->assertEqual('hello world', SActiveStore::findFirst('reminder')->content);
 }
示例#11
0
 public function insertFixtures()
 {
     foreach ($this->values as $obj => $values) {
         $this->db->execute("INSERT INTO {$this->tableName} (" . implode(', ', array_keys($values)) . ")" . " VALUES (" . implode(', ', SActiveStore::arrayQuote(array_values($values))) . ")");
     }
 }
 function testFindWithBindedCondition()
 {
     $companies = SActiveStore::findAll('Company', array("name = :company", array(':company' => 'Groupe W')));
     $this->assertEqual(1, count($companies));
 }
 public function delete()
 {
     SActiveStore::findByPk($this->class_name, $this->params['id'])->delete();
     $this->redirectTo(array('action' => 'index'));
 }
 function testBuildBeforeChildSaved()
 {
     $client = SActiveStore::findByPk('Client', 3);
     // client without contract
     $contract = $client->buildContract(array('code' => 'test'));
     $this->assertEqual($contract, $client->contract);
     $this->assertTrue($contract->isNewRecord());
     $this->assertTrue($client->save());
     $this->assertFalse($contract->isNewRecord());
     $this->assertEqual($contract, $client->contract);
 }
 protected function findTarget()
 {
     return SActiveStore::findByPk($this->assocClass, $this->owner[$this->foreignKey]);
 }
 function testDelete()
 {
     $this->assertEqual(array($this->topics['list_1'], $this->topics['list_2'], $this->topics['list_3'], $this->topics['list_4']), SActiveStore::findAll('Topic', 'forum_id = 1', array('order' => 'position ASC')));
     $topic = new SListDecorator($this->topics['list_2'], 'forum');
     $topic->delete();
     $this->instanciateFixtures();
     $this->assertEqual(array($this->topics['list_1'], $this->topics['list_3'], $this->topics['list_4']), SActiveStore::findAll('Topic', 'forum_id = 1', array('order' => 'position ASC')));
     $this->assertEqual(1, $this->topics['list_1']->position);
     $this->assertEqual(2, $this->topics['list_3']->position);
     $this->assertEqual(3, $this->topics['list_4']->position);
     $topic = new SListDecorator($this->topics['list_1'], 'forum');
     $topic->delete();
     $this->instanciateFixtures();
     $this->assertEqual(array($this->topics['list_3'], $this->topics['list_4']), SActiveStore::findAll('Topic', 'forum_id = 1', array('order' => 'position ASC')));
     $this->assertEqual(1, $this->topics['list_3']->position);
     $this->assertEqual(2, $this->topics['list_4']->position);
 }
 protected function findTarget()
 {
     return SActiveStore::findBySql($this->assocClass, $this->constructSql());
 }