/** * Load a record for this table * * @param mixed $id Value(s) for primary key or criteria or NULL for a new record * @param string $mode Use property 'load.$mode' (defaults back to property 'load' and 'view') * @param int $resulttype A Q\DB::FETCH_% constant * @return DB_Record * * @throws DB_LimitException if query results in > 1 record */ public function load($id, $mode = null, $resulttype = DB::FETCH_RECORD) { if (!isset($id) && $resulttype != DB::FETCH_RECORD) { throw new Exception("Loading a new record for any other result type than DB::FETCH_RECORD is not supported."); } // No link or no table property, so create new record directly (mode is ignored) if (!$this->getConnection()) { if (isset($id)) { throw new Exception("Unable to load a record '{$id}' for table '{$this}': No database connection"); } return DB_Record::create($this); } // Create a record using though a query result if (isset($mode) && isset($this["load.{$mode}"])) { $statement = $this["load.{$mode}"]; } else { $statement = $this['load']; } $result = $this->select($statement, isset($id) ? $id : false)->execute(); if (!isset($id)) { return $result->newRecord(); } if ($result->countRows() > 1) { throw new DB_LimitException("Query returned " . $result->countRows() . " rows, while only 1 row was expected"); } return $result->fetchRow($resulttype); }
/** * Returns a new record based on the fields of the result * * @return DB_Record */ public function newRecord() { return DB_Record::create($this); }
/** * Set an item of (or add to) the array object * * @param int $offset * @param Q\DB_Record $value New Child record */ function offsetSet($offset, $value) { if (!$value instanceof DB_Record) { $value = DB_Record::create($this->parent, $value); } parent::offsetSet($offset, $value); }