/** * Fetches column values from database. * * Examples: * ```php * // fetches a single column * $col0 = $r->fetch("col0"); * * // fetches multiple columns * list($col0, $col1, $col2) = $this->fetch(["col0", "col1", "col2"]); * ``` * * @param array|mixed $colPaths Column paths * * @return mixed|mixed[] */ public function fetch($colPaths = []) { $ret = []; // method overloading $isArrayColPaths = is_array($colPaths); if (!$isArrayColPaths) { $colPaths = [$colPaths]; } if (count($colPaths) > 0) { // registers columns and fetches values $cols = $this->_regColumns($colPaths); foreach ($cols as $col) { array_push($ret, $col->getValue()); } if (!$isArrayColPaths) { $ret = $ret[0]; } } else { if ($this->_primaryKey->hasChanged()) { // gets the columns that haven't changed $columns = array_diff($this->_columns, $this->_getChangedColumns()); // fills columns if (count($columns) > 0) { $row = $this->_db->query($this->_getSelectStatement($columns)); foreach ($columns as $column) { $column->setDbValue($row[$column->getName()]); } } } $this->_isUpdated = true; } return $ret; }
/** * Fetches column values from database. * * @return void */ public function fetch() { if ($this->_primaryKey->hasChanged()) { // gets the columns that haven't changed $columns = array_diff($this->_columns, $this->_getChangedColumns()); // fills columns if (count($columns) > 0) { $row = $this->_db->query($this->_getSelectStatement($columns)); foreach ($columns as $column) { $column->setDbValue($row[$column->getName()]); } } } $this->_isUpdated = true; }