/** * Return parent record * * @throws Exception\LogicException * @throws Exception\RelationNotFoundException * * @param Record $record * @param string $parent_table * @return Record|false */ public function getParent(Record $record, $parent_table) { if ($record->getState() == Record::STATE_DELETED) { throw new Exception\LogicException(__METHOD__ . ": Logic exception, cannot operate on record that was deleted"); } $tableName = $this->table->getTableName(); $relations = $this->table->getTableManager()->metadata()->getRelations($tableName); //$rels = array(); foreach ($relations as $column => $parent) { if ($parent['referenced_table'] == $parent_table) { // @todo, check the case when // table has many relations to the same parent // we'll have to throw an exception $record = $this->table->getTableManager()->table($parent_table)->findOneBy(array($parent['referenced_column'] => $record->offsetGet($column))); return $record; } } throw new Exception\RelationNotFoundException(__METHOD__ . ": Cannot find parent relation between table '{$tableName}' and '{$parent_table}'"); }
/** * Return a record object for this table * If $data is specified, the record will be filled with the * data present in the associative array * * * If $throwException is true, if any non existing column is found * an error will be thrown * * @throws Exception\ColumnNotFoundException if $ignore_invalid_columns is false and some columns does not exists in table * * @param array|ArrayObject $data associative array containing initial data * @param boolean $ignore_invalid_columns if true will throw an exception if a column does not exists * @return Record */ public function record($data = array(), $ignore_invalid_columns = true) { if (!$ignore_invalid_columns) { $this->checkDataColumns((array) $data); } $record = new Record((array) $data, $this); $record->setState(Record::STATE_NEW); return $record; }