Exemple #1
0
 /**
  * Magic method to access properties
  *
  * @param  string $name
  *
  * @throws \InvalidArgumentException
  * @return mixed
  */
 public function __get($name)
 {
     if (array_key_exists($name, $this->data)) {
         return $this->data[$name];
     } else {
         $key = $this->tableGateway->column($name);
         if ($key && array_key_exists($key, $this->data)) {
             return $this->data[$key];
         }
         throw new \InvalidArgumentException('Not a valid column in this row: ' . $name);
     }
 }
Exemple #2
0
 /**
  * Save a row
  *
  * @param bool $rePopulate  To re-populate data
  * @param bool $filter Filter invalid columns
  *
  * @return int
  */
 public function save($rePopulate = true, $filter = true)
 {
     $this->initialize();
     /**#@+
      * Encode data to make it db-ready
      */
     $this->data = $this->encode($this->data);
     if ($filter) {
         $columns = $this->columns ?: $this->model->getColumns();
         if ($columns) {
             foreach (array_keys($this->data) as $column) {
                 if (!in_array($column, $columns)) {
                     unset($this->data[$column]);
                 }
             }
         }
     }
     /**#@-*/
     if ($this->rowExistsInDatabase()) {
         // UPDATE
         $data = $this->data;
         $where = array();
         // primary key is always an array even if its a single column
         foreach ($this->primaryKeyColumn as $pkColumn) {
             $where[$pkColumn] = $this->primaryKeyData[$pkColumn];
             if ($data[$pkColumn] == $this->primaryKeyData[$pkColumn]) {
                 unset($data[$pkColumn]);
             }
         }
         $statement = $this->sql->prepareStatementForSqlObject($this->sql->update()->set($data)->where($where));
         $result = $statement->execute();
         $rowsAffected = $result->getAffectedRows();
         unset($statement, $result);
         // cleanup
     } else {
         // INSERT
         $insert = $this->sql->insert();
         $insert->values($this->data);
         $statement = $this->sql->prepareStatementForSqlObject($insert);
         $result = $statement->execute();
         if (($primaryKeyValue = $result->getGeneratedValue()) && count($this->primaryKeyColumn) == 1) {
             $this->primaryKeyData = array($this->primaryKeyColumn[0] => $primaryKeyValue);
         } else {
             // make primary key data available so that
             // $where can be complete
             $this->processPrimaryKeyData();
         }
         $rowsAffected = $result->getAffectedRows();
         unset($statement, $result);
         // cleanup
         $where = array();
         // primary key is always an array even if its a single column
         foreach ($this->primaryKeyColumn as $pkColumn) {
             $where[$pkColumn] = $this->primaryKeyData[$pkColumn];
         }
     }
     if ($rePopulate) {
         // refresh data
         $statement = $this->sql->prepareStatementForSqlObject($this->sql->select()->where($where));
         $result = $statement->execute();
         $rowData = $result->current();
         unset($statement, $result);
         // cleanup
         // make sure data and original data are in sync after save
         $this->populate($rowData, true);
     }
     // return rows affected
     return $rowsAffected;
 }
 /**
  * {@inheritDoc}
  */
 public function delete($where)
 {
     $this->adapter = $this->masterAdapter;
     return parent::delete($where);
 }
Exemple #4
0
 /**
  * Remove a node
  *
  * @param   mixed   $objective  target node ID or Node
  * @param   bool    $recursive  Whether to delete all children nodes
  * @return int|false Affected rows
  */
 public function remove($objective, $recursive = false)
 {
     $row = $this->normalizeNode($objective);
     if (!$row) {
         return false;
     }
     list($left, $right) = array($row->left, $row->right);
     //$result = parent::delete(array($this->primaryKeyColumn => $row->id));
     $result = $row->delete();
     /*
     if (!$result) {
         return false;
     }
     */
     // Remove all children
     if ($recursive) {
         // Prepare for clause for children nodes with quoted identifier
         $where = array($this->quoteColumn('left') . ' > ?' => $left, $this->quoteColumn('right') . ' < ?' => $right);
         // Delete children and add up deleted row number
         $result += parent::delete($where);
         // shift right hand nodes with width
         if (!$this->shift($right + 1, -1 * ($right - $left + 1))) {
             return false;
         }
         // Keep children
     } else {
         $data = array($this->column('depth') => new Expression($this->quoteColumn('depth') . ' - 1'));
         $where = array($this->quoteColumn('left') . ' > ?' => $left, $this->quoteColumn('right') . ' < ?' => $right);
         $this->update($data, $where);
         if (!$this->shift($left + 1, -1, $right - 1)) {
             return false;
         }
         if (!$this->shift($right + 1, -2)) {
             return false;
         }
     }
     return $result;
 }