public function testDelete() { $user = new User(); $user->name = 'John'; $user->save(); $sql = 'SELECT COUNT(*) FROM user'; $this->assertEquals('1', $this->orm->execute($sql)->fetchColumn()); $user->delete(); $this->assertEquals('0', $this->orm->execute($sql)->fetchColumn()); }
protected function setUp() { $this->orm = new ORM(); $this->orm->connect('sqlite::memory:'); $sql = <<<'TAG' CREATE TABLE user ( identifier INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT ) TAG; $this->orm->execute($sql); }
/** * Returns the beginning of any SQL statement, depending on $this->queryType. * * @return string */ protected function getTypeSQL() { $sql = ''; $quotedTableName = $this->orm->quoteIdentifier($this->tableName); switch ($this->queryType) { case self::QUERY_TYPE_SELECT: $top = ''; if ($this->limit !== null && $this->detectLimitStyle() === self::LIMIT_STYLE_TOP) { $top = ' TOP ' . $this->limit; } $sql = sprintf('SELECT %s * FROM %s', $top, $quotedTableName); break; case self::QUERY_TYPE_DELETE: $sql = sprintf('DELETE FROM %s', $quotedTableName); break; case self::QUERY_TYPE_UPDATE: $sets = array(); foreach ($this->modelData as $columnName => $columnValue) { $sets[] = sprintf('%s = ?', $this->orm->quoteIdentifier($columnName)); $this->parameters[] = $columnValue; } $sql = sprintf('UPDATE %s SET %s', $quotedTableName, implode(',', $sets)); break; case self::QUERY_TYPE_INSERT: $quotedColumnNames = $this->orm->quoteIdentifier(array_keys($this->modelData)); $placeholders = implode(',', array_fill(0, count($this->modelData), '?')); $sql = sprintf('INSERT INTO %s (%s) VALUES (%s)', $quotedTableName, $quotedColumnNames, $placeholders); $this->parameters = array_merge($this->parameters, array_values($this->modelData)); break; } return $sql; }
public function testUpdate() { $user = new User(); $user->name = 'John'; $user->save(); $user->id = null; $user->name = 'Jane'; $user->save(); $user->id = null; $user->name = 'Mark'; $user->save(); $fluentUser = new FluentUser(); $fluentUser->name = 'oops'; $fluentUser->updateAll(); $names = $this->orm->execute('SELECT name FROM user')->fetchAll(\PDO::FETCH_COLUMN); foreach ($names as $name) { $this->assertEquals('oops', $name); } }