/**
  * 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}'");
 }
 /**
  * Prefix table join condition
  *
  * @param string|array $table
  * @return array|string
  */
 protected function prefixTableJoinCondition($table)
 {
     $tm = $this->table->getTableManager();
     if (is_array($table)) {
         $alias = key($table);
         $prefixed_table = $tm->getPrefixedTable($table[$alias]);
         $table = array($alias => $prefixed_table);
     } elseif (is_string($table)) {
         $prefixed_table = $tm->getPrefixedTable($table);
         $table = $prefixed_table;
     }
     return $table;
 }
 /**
  * Iterator: get current item
  *
  * @throws Exception\LogicException whenever a record cannot be instanciated, due to missing column specs
  * @return Record
  */
 public function current()
 {
     $data = $this->dataSource->current();
     if (!$this->has_complete_record_definition) {
         $data_columns = array_keys($this->table->getColumnsInformation());
         $record_columns = array_keys((array) $data);
         $matches = array_intersect($data_columns, $record_columns);
         if (count($matches) != count($data_columns)) {
             $missings = join(',', array_diff($data_columns, $record_columns));
             $msg = __METHOD__ . ": Cannot create a Record due to incomplete or aliased column definition (missing: {$missings}).";
             $msg .= "Check whether columns have been modified in TableSearch::columns() method, or use an toArray(), toJson()... version of the ResultSet.";
             throw new Exception\LogicException($msg);
         }
         $this->has_complete_record_definition = true;
     }
     $record = $this->table->record($data, $ignore = true);
     $record->setState(Record::STATE_CLEAN);
     return $record;
 }