atk4\data\Model::load PHP Method

load() public method

Load model.
public load ( mixed $id )
$id mixed
    public function load($id)
    {
        if (!$this->persistence) {
            throw new Exception(['Model is not associated with any database']);
        }
        if ($this->loaded()) {
            $this->unload();
        }
        if ($this->hook('beforeLoad', [$id]) === false) {
            return $this;
        }
        $this->data = $this->persistence->load($this, $id);
        if (is_null($this->id)) {
            $this->id = $id;
        }
        $this->hook('afterLoad');
        return $this;
    }

Usage Example

Beispiel #1
0
 public function testModelInsert()
 {
     $a = ['user' => [1 => ['name' => 'John', 'surname' => 'Smith'], 2 => ['name' => 'Sarah', 'surname' => 'Jones']]];
     $p = new Persistence_SQL('sqlite::memory:');
     $p->connection->expr('drop table if exists user')->execute();
     $p->connection->expr('create table user(id integer primary key autoincrement, name varchar(255), surname varchar(255))')->execute();
     $m = new Model($p, 'user');
     $m->addField('name');
     $m->addField('surname');
     $ms = [];
     foreach ($a['user'] as $id => $row) {
         $ms[] = $m->insert($row);
     }
     $this->assertEquals('John', $m->load($ms[0])['name']);
     $this->assertEquals('Jones', $m->load($ms[1])['surname']);
 }
All Usage Examples Of atk4\data\Model::load