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']); }
public function testModelInsertRows() { $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'); $m->import($a['user']); $this->assertEquals(2, $m->action('count')->getOne()); }
/** * Model is not associated with any database - persistence should be set. * * @expectedException Exception */ public function testException7() { $m = new Model(); $m->action('insert'); }