atk4\data\Model::tryLoad PHP Method

tryLoad() public method

Will not throw exception if record doesn't exist.
public tryLoad ( mixed $id )
$id mixed
    public function tryLoad($id)
    {
        if (!$this->persistence) {
            throw new Exception(['Model is not associated with any database']);
        }
        if ($this->loaded()) {
            $this->unload();
        }
        $this->data = $this->persistence->tryLoad($this, $id);
        if ($this->data) {
            $this->id = $id;
            $this->hook('afterLoad');
        } else {
            $this->unload();
        }
        return $this;
    }

Usage Example

Beispiel #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']);
 }
All Usage Examples Of atk4\data\Model::tryLoad