tryLoad() public method

Will not throw exception if record doesn't exist.
public tryLoad ( mixed $id )
$id mixed
示例#1
0
 public function testExpressions()
 {
     $a = ['user' => [1 => ['id' => 1, 'name' => 'John', 'surname' => 'Smith', 'cached_name' => 'John Smith'], 2 => ['id' => 2, 'name' => 'Sue', 'surname' => 'Sue', 'cached_name' => 'ERROR']]];
     $this->setDB($a);
     $db = new Persistence_SQL($this->db->connection);
     $m = new Model($db, 'user');
     $m->addFields(['name', 'surname', 'cached_name']);
     $m->addExpression('full_name', '[name] || " " || [surname]');
     $m->addCondition($m->expr('[full_name] != [cached_name]'));
     $this->assertEquals('select `id`,`name`,`surname`,`cached_name`,(`name` || " " || `surname`) `full_name` from `user` where (`name` || " " || `surname`) != `cached_name`', $m->action('select')->render());
     $m->tryLoad(1);
     $this->assertEquals(null, $m['name']);
     $m->tryLoad(2);
     $this->assertEquals('Sue', $m['name']);
 }
示例#2
0
文件: JoinSQLTest.php 项目: atk4/data
 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());
 }
示例#3
0
 public function testJoinUpdate()
 {
     $a = ['user' => [1 => ['id' => 1, 'name' => 'John', 'contact_id' => 1], 2 => ['id' => 2, 'name' => 'Peter', 'contact_id' => 1], 3 => ['id' => 3, 'name' => 'Joe', 'contact_id' => 2]], 'contact' => [1 => ['id' => 1, 'contact_phone' => '+123'], 2 => ['id' => 2, 'contact_phone' => '+321']]];
     $db = new Persistence_Array($a);
     $m_u = new Model($db, 'user');
     $m_u->addField('contact_id');
     $m_u->addField('name');
     $j = $m_u->join('contact');
     $j->addField('contact_phone');
     $m_u->load(1);
     $m_u['name'] = 'John 2';
     $m_u['contact_phone'] = '+555';
     $m_u->save();
     $this->assertEquals(['user' => [1 => ['id' => 1, 'name' => 'John 2', 'contact_id' => 1], 2 => ['id' => 2, 'name' => 'Peter', 'contact_id' => 1], 3 => ['id' => 3, 'name' => 'Joe', 'contact_id' => 2]], 'contact' => [1 => ['id' => 1, 'contact_phone' => '+555'], 2 => ['id' => 2, 'contact_phone' => '+321']]], $a);
     $m_u->load(3);
     $m_u['name'] = 'XX';
     $m_u['contact_phone'] = '+999';
     $m_u->save();
     $this->assertEquals(['user' => [1 => ['id' => 1, 'name' => 'John 2', 'contact_id' => 1], 2 => ['id' => 2, 'name' => 'Peter', 'contact_id' => 1], 3 => ['id' => 3, 'name' => 'XX', 'contact_id' => 2]], 'contact' => [1 => ['id' => 1, 'contact_phone' => '+555'], 2 => ['id' => 2, 'contact_phone' => '+999']]], $a);
     $m_u->tryLoad(4);
     $m_u['name'] = 'YYY';
     $m_u['contact_phone'] = '+777';
     $m_u->save();
     $this->assertEquals(['user' => [1 => ['id' => 1, 'name' => 'John 2', 'contact_id' => 1], 2 => ['id' => 2, 'name' => 'Peter', 'contact_id' => 1], 3 => ['id' => 3, 'name' => 'XX', 'contact_id' => 2], 4 => ['id' => 4, 'name' => 'YYY', 'contact_id' => 3]], 'contact' => [1 => ['id' => 1, 'contact_phone' => '+555'], 2 => ['id' => 2, 'contact_phone' => '+999'], 3 => ['id' => 3, 'contact_phone' => '+777']]], $a);
 }
示例#4
0
 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']);
 }
示例#5
0
 public function testTryLoad()
 {
     $a = ['types' => [['date' => '2013-02-20']]];
     $this->setDB($a);
     $db = new Persistence_SQL($this->db->connection);
     $m = new Model($db, ['table' => 'types']);
     $m->addField('date', ['type' => 'date', 'dateTimeClass' => '\\atk4\\data\\tests\\MyDate']);
     $m->tryLoad(1);
     $this->assertTrue($m['date'] instanceof MyDate);
 }
示例#6
0
 /**
  * Model is not associated with any database - persistence should be set.
  *
  * @expectedException Exception
  */
 public function testException2()
 {
     $m = new Model();
     $m->tryLoad(1);
 }