/** * Test constructor. */ public function testConstructFields() { $m = new Model(); $m->addField('name'); $f = $m->getElement('name'); $this->assertEquals('name', $f->short_name); $m->add(new Field(), 'surname'); $f = $m->getElement('surname'); $this->assertEquals('surname', $f->short_name); }
public function testEditableHasOne() { $gender = new Model(); $gender->addField('name'); $m = new Model(); $m->addField('name'); $m->hasOne('gender_id', $gender); $this->assertEquals(false, $m->getElement('gender_id')->system); $this->assertEquals(true, $m->getElement('gender_id')->isEditable()); }
public function testDoubleJoin() { $a = ['user' => [10 => ['id' => 10, 'name' => 'John 2', 'contact_id' => 100], 20 => ['id' => 20, 'name' => 'Peter', 'contact_id' => 100], 30 => ['id' => 30, 'name' => 'XX', 'contact_id' => 200], 40 => ['id' => 40, 'name' => 'YYY', 'contact_id' => 300]], 'contact' => [100 => ['id' => 100, 'contact_phone' => '+555', 'country_id' => 1], 200 => ['id' => 200, 'contact_phone' => '+999', 'country_id' => 2], 300 => ['id' => 300, 'contact_phone' => '+777', 'country_id' => 5]], 'country' => [1 => ['id' => 1, 'name' => 'UK'], 2 => ['id' => 2, 'name' => 'US'], 3 => ['id' => 3, 'name' => 'India']]]; $this->setDB($a); $db = new Persistence_SQL($this->db->connection); $m_u = new Model($db, 'user'); $m_u->addField('contact_id'); $m_u->addField('name'); $j_contact = $m_u->join('contact'); $j_contact->addField('contact_phone'); $j_country = $j_contact->join('country'); $j_country->addField('country_name', ['actual' => 'name']); $m_u->load(10); $m_u->delete(); $m_u->loadBy('country_name', 'US'); $this->assertEquals(30, $m_u->id); $m_u['country_name'] = 'USA'; $m_u->save(); $m_u->tryLoad(40); $this->assertEquals(false, $m_u->loaded()); $this->assertSame($m_u->getElement('country_id')->join, $m_u->getElement('contact_phone')->join); $m_u->unload(); $m_u->save(['name' => 'new', 'contact_phone' => '+000', 'country_name' => 'LV']); $this->assertEquals(['user' => [20 => ['id' => 20, 'name' => 'Peter', 'contact_id' => 100], 30 => ['id' => 30, 'name' => 'XX', 'contact_id' => 200], 40 => ['id' => 40, 'name' => 'YYY', 'contact_id' => 300], 41 => ['id' => 41, 'name' => 'new', 'contact_id' => 301]], 'contact' => [200 => ['id' => 200, 'contact_phone' => '+999', 'country_id' => 2], 300 => ['id' => 300, 'contact_phone' => '+777', 'country_id' => 5], 301 => ['id' => 301, 'contact_phone' => '+000', 'country_id' => 4]], 'country' => [2 => ['id' => 2, 'name' => 'USA'], 3 => ['id' => 3, 'name' => 'India'], 4 => ['id' => 4, 'name' => 'LV']]], $this->getDB()); }
public function testExpressions2() { $a = ['user' => [1 => ['id' => 1, 'name' => 'John', 'surname' => 'Smith', 'gender' => 'M'], 2 => ['id' => 2, 'name' => 'Sue', 'surname' => 'Sue', 'gender' => 'F']]]; $this->setDB($a); $db = new Persistence_SQL($this->db->connection); $m = new Model($db, 'user'); $m->addFields(['name', 'gender', 'surname']); $m->tryLoad(1); $this->assertEquals('John', $m['name']); $m->tryLoad(2); $this->assertEquals('Sue', $m['name']); $mm = clone $m; $mm->addCondition($mm->expr('[name] = [surname]')); $mm->tryLoad(1); $this->assertEquals(null, $mm['name']); $mm->tryLoad(2); $this->assertEquals('Sue', $mm['name']); $mm = clone $m; $mm->addCondition($m->getElement('name'), $m->getElement('surname')); $mm->tryLoad(1); $this->assertEquals(null, $mm['name']); $mm->tryLoad(2); $this->assertEquals('Sue', $mm['name']); $mm = clone $m; $mm->addCondition($mm->expr('[name] != [surname]')); $mm->tryLoad(1); $this->assertEquals('John', $mm['name']); $mm->tryLoad(2); $this->assertEquals(null, $mm['name']); $mm = clone $m; $mm->addCondition($m->getElement('name'), '!=', $m->getElement('surname')); $mm->tryLoad(1); $this->assertEquals('John', $mm['name']); $mm->tryLoad(2); $this->assertEquals(null, $mm['name']); }
public function testSystem1() { $m = new Model(); $m->addField('foo', ['system' => true]); $m->addField('bar'); $this->assertEquals(false, $m->getElement('foo')->isEditable()); $this->assertEquals(false, $m->getElement('foo')->isVisible()); $m->onlyFields(['bar']); // TODO: build a query and see if the field is there }