_table = Doctrine::getTable($table); $this->_model = $table; } public function getTable() { return $this->_table; } public function setRecord($record) { if(($record instanceof Doctrine_Record) === false) throw new InvalidArgumentException('Record not a Doctrine_Record'); $this->_record = $record; } public function getRecord() { return $this->_record; } public function saveRecord() { try { $this->_record->save(); }catch (Doctrine_Query_Exception $dqe){ print_r($dqe->getMessage()); } } public function getRecordValue($name) { return $this->_record->$name; } public function setRecordValue($name, $value) { $this->_record->$name = $value; } /** * Return all columns as an array * * Array must contain 'type' for column type, 'notnull' true/false * for the column's nullability, and 'values' for enum values, 'primary' * true/false for primary key. Key = column's name * * @return array */ public function getColumns() { $data = $this->_table->getColumns(); $cols = array(); foreach($data as $name => $d) { $cols[$name] = array( 'type' => $d['type'], 'notnull' => (isset($d['notnull'])) ? $d['notnull'] : false, 'values' => (isset($d['values'])) ? $d['values'] : array(), 'primary' => (isset($d['primary'])) ? $d['primary'] : false ); } return $cols; } /** * Return relations as an array * * Array must contain 'type' for relation type, 'id' for the name * of the PK column of the related table, 'model' for the related class * name, 'notnull' for nullability. 'local' for the name of the local column * Key must be the alias of the relation column * * @return array */ public function getRelations() { if(defined('Doctrine_Relation::ONE_AGGREGATE')) $oneType = Doctrine_Relation::ONE_AGGREGATE; else $oneType = Doctrine_Relation::ONE; $rels = $this->_table->getRelations(); $relations = array(); foreach($rels as $rel) { $relation = array(); if($rel->getType() == $oneType) $relation['type'] = CU_Form_Model::RELATION_ONE; else $relation['type'] = CU_Form_Model::RELATION_MANY; $identifier = $rel->getTable()->getIdentifier(); if(is_array($identifier)){ $relation['id'] = $identifier[0]; }else{ $relation['id'] = $identifier; } $relation['model'] = $rel->getClass(); $relation['local'] = $rel->getLocal(); $definition = $this->_table->getColumnDefinition($rel->getLocal()); $relation['notnull'] = (isset($definition['notnull'])) ? $definition['notnull'] : false; $relations[$rel->getAlias()] = $relation; } return $relations; } public function getRelatedRecord($record, $name) { return $record->$name; } /** * Return the value of a record's primary key * @param Doctrine_Record $record * @return mixed */ public function getRecordIdentifier($record) { $col = $record->getTable()->getIdentifier(); return $record->$col; } /** * Get the records for a many-relation * @param string $name Name of the relation * @return array */ public function getManyRecords($name) { return $this->_record->$name; } public function addManyRecord($name, $record) { $this->_record->{$name}[] = $record; } public function getOneRecords($relation) { return Doctrine::getTable($relation['model'])->findAll(); } public function deleteRecord($record) { $record->delete(); } public function getNewRecord() { return new $this->_model; } }