示例#1
0
 /**
  * @covers Cradle\Sql\SqlException::forTableNotSet
  */
 public function testForTableNotSet()
 {
     $message = null;
     try {
         throw SqlException::forTableNotSet();
     } catch (SqlException $e) {
         $message = $e->getMessage();
     }
     $this->assertEquals('No default table set or was passed.', $message);
 }
示例#2
0
 /**
  * Updates model to database
  *
  * @param string|null       $table    Table name
  * @param SqlInterface|null  $database Dabase object
  * @param string|array|null $primary  The primary column if you know it
  *
  * @return Model
  */
 public function update($table = null, SqlInterface $database = null, $primary = null)
 {
     //if no table
     if (is_null($table)) {
         //if no default table either
         if (!$this->table) {
             //throw error
             throw SqlException::forTableNotSet();
         }
         $table = $this->table;
     }
     //if no database
     if (is_null($database)) {
         //and no default database
         if (!$this->database) {
             throw SqlException::forDatabaseNotSet();
         }
         $database = $this->database;
     }
     //get the meta data, the valid column values and whether is primary is set
     $meta = $this->getMeta($table, $database);
     $data = $this->getValidColumns(array_keys($meta[self::COLUMNS]));
     //update original data
     $this->original = $this->data;
     //from here it means that this table has primary
     //columns and all primary values are set
     if (is_null($primary)) {
         $primary = $meta[self::PRIMARY];
     }
     if (is_string($primary)) {
         $primary = [$primary];
     }
     $filter = [];
     //for each primary key
     foreach ($primary as $column) {
         //add the condition to the filter
         $filter[] = [$column . '=%s', $data[$column]];
     }
     //we update it
     $database->updateRows($table, $data, $filter);
     return $this;
 }