public function testExpressionJoin() { $a = ['user' => [1 => ['id' => 1, 'name' => 'John', 'surname' => 'Smith', 'gender' => 'M', 'contact_id' => 1], 2 => ['id' => 2, 'name' => 'Sue', 'surname' => 'Sue', 'gender' => 'F', 'contact_id' => 2], 3 => ['id' => 3, 'name' => 'Peter', 'surname' => 'Smith', 'gender' => 'M', 'contact_id' => 1]], 'contact' => [1 => ['id' => 1, 'contact_phone' => '+123 smiths'], 2 => ['id' => 2, 'contact_phone' => '+321 sues']]]; $this->setDB($a); $db = new Persistence_SQL($this->db->connection); $m = new Model($db, 'user'); $m->addFields(['name', 'gender', 'surname']); $m->join('contact')->addField('contact_phone'); $m->tryLoad(1); $this->assertEquals('John', $m['name']); $this->assertEquals('+123 smiths', $m['contact_phone']); $m->tryLoad(2); $this->assertEquals('Sue', $m['name']); $this->assertEquals('+321 sues', $m['contact_phone']); $m->tryLoad(3); $this->assertEquals('Peter', $m['name']); $this->assertEquals('+123 smiths', $m['contact_phone']); $mm = clone $m; $mm->addCondition($mm->expr('[name] = [surname]')); $mm->tryLoad(1); $this->assertEquals(false, $mm->loaded()); $mm->tryLoad(2); $this->assertEquals('Sue', $mm['name']); $this->assertEquals('+321 sues', $mm['contact_phone']); $mm->tryLoad(3); $this->assertEquals(false, $mm->loaded()); $mm = clone $m; $mm->addCondition($mm->expr('"+123 smiths" = [contact_phone]')); $mm->tryLoad(1); $this->assertEquals('John', $mm['name']); $this->assertEquals('+123 smiths', $mm['contact_phone']); $mm->tryLoad(2); $this->assertEquals(null, $mm['name']); $this->assertEquals(null, $mm['contact_phone']); $mm->tryLoad(3); $this->assertEquals('Peter', $mm['name']); $this->assertEquals('+123 smiths', $mm['contact_phone']); }
public function testReloading() { $a = ['math' => [['a' => 2, 'b' => 2]]]; $this->setDB($a); $db = new Persistence_SQL($this->db->connection); $m = new Model($db, 'math'); $m->addFields(['a', 'b']); $m->addExpression('sum', '[a] + [b]'); $m->load(1); $this->assertEquals(4, $m['sum']); $m->save(['a' => 3]); $this->assertEquals(5, $m['sum']); $this->assertEquals(9, $m->unload()->save(['a' => 4, 'b' => 5])->get('sum')); $this->setDB($a); $m = new Model($db, ['math', 'reload_after_save' => false]); $m->addFields(['a', 'b']); $m->addExpression('sum', '[a] + [b]'); $m->load(1); $this->assertEquals(4, $m['sum']); $m->save(['a' => 3]); $this->assertEquals(4, $m['sum']); $this->assertEquals(null, $m->unload()->save(['a' => 4, 'b' => 5])->get('sum')); }
/** * If first argument is array, then second argument should not be used. * * @expectedException Exception */ public function testException1() { $m = new Model(); $m->addFields(['name', 'salary']); $m->setOrder(['name', 'salary'], 'desc'); }