public function testInstantiate() { $join = new Mad_Model_Join_Base(new UnitTest()); $row = array('T0_R0' => '1', 'T0_R1' => '1', 'T0_R2' => 'string value', 'T0_R3' => 'text value', 'T0_R4' => '1.2', 'T0_R5' => '1.2', 'T0_R6' => '2005-12-23 12:34:23', 'T0_R7' => '2005-12-23', 'T0_R8' => '12:34:23 ', 'T0_R9' => 'some blob data', 'T0_R10' => '1', 'T0_R11' => 'b', 'T0_R12' => '*****@*****.**', 'T1_R0' => 'test more data', 'T1_R1' => 'test another col'); $record = $join->extractRecord($row); $this->assertEquals('1', $join->instantiate($row)->id); $this->assertEquals('1', $join->instantiate($row)->integer_value); $this->assertEquals('1', $join->instantiate($row)->boolean_value); }
/** * Construct new Join Association. * * @param object $reflection * @param object $joinDependency * @param object $parent */ public function __construct(Mad_Model_Association_Base $reflection, Mad_Model_Join_Dependency $joinDependency, Mad_Model_Join_Base $parent) { parent::__construct($reflection->getAssocModel()); $this->_parent = $parent; $this->_reflection = $reflection; $this->_aliasedPrefix = 'T' . sizeof($joinDependency->joins()); $this->_aliasedTableName = $this->tableName(); // start with table name $this->_parentTableName = $parent->model()->tableName(); // if the table name has been used, then use an alias $alias = $this->_aliasedTableName; if ($joinDependency->tableAlias($this->_aliasedTableName)) { $alias = $reflection->getAssocTable() . "_{$this->_parentTableName}"; $this->_aliasedTableName = $this->_model->connection->tableAliasFor($alias); // make sure to get a unique name. careful of name restrictions $alias = $this->_aliasedTableName; $i = $joinDependency->tableAlias($alias); if ($i > 0) { $maxLen = $this->_model->connection->tableAliasLength() - 3; $this->_aliasedTableName = substr($this->_aliasedTableName, 0, $maxLen) . "_" . ($i + 1); } } $joinDependency->addTableAlias($alias); // create alias for join table (only should be executed for hasAndBelongsToMany|belongsTo if ($this->_aliasedJoinTableName = $this->_reflection->getJoinTable()) { if ($joinDependency->tableAlias($this->_aliasedJoinTableName)) { $alias = $reflection->getAssocTable() . '_' . $this->_parentTableName . '_join'; $this->_aliasedJoinTableName = $this->_model->connection->tableAliasFor($alias); // make sure to get a unique name. careful of oracle name restrictions $i = $joinDependency->tableAlias($this->_aliasedJoinTableName); if ($i > 0) { $maxLen = $this->_model->connection->tableAliasLength() - 3; $this->_aliasedJoinTableName = substr($this->_aliasedJoinTableName, 0, $maxLen) . "_" . ($i + 1); } } $joinDependency->addTableAlias($this->_aliasedJoinTableName); } }
/** * Construct associations for model from record/row * * @param object $record * @param object $join * @param array $row */ protected function _constructAssociation(Mad_Model_Base $record, Mad_Model_Join_Base $join, $row) { // set that we've loaded this association $record->setAssociationLoaded($join->reflection()->getAssocName()); if ($record->id != $join->parent()->recordId($row) || empty($row[$join->aliasedPrimaryKey()])) { return; } $association = $join->instantiate($row); $macro = $join->reflection()->getMacro(); $singular = Mad_Support_Inflector::singularize($join->reflection()->getAssocName()); if ($macro == 'hasAndBelongsToMany' || $macro == 'hasMany' || $macro == 'hasManyThrough') { $addMethod = Mad_Support_Inflector::camelize('add' . ucfirst($singular), 'lower'); $addMethod = str_replace('/', '_', $addMethod); // make sure object isn't already included $getter = Mad_Support_Inflector::camelize($join->reflection()->getAssocName(), 'lower'); $getter = str_replace('/', '_', $getter); $exists = array(); foreach ($record->{$getter} as $val) { $exists[] = $val->id; } if (!in_array($association->id, $exists)) { $record->{$addMethod}($association); } } elseif ($macro == 'belongsTo' || $macro == 'hasOne') { $assignMethod = Mad_Support_Inflector::camelize($singular, 'lower'); $record->{$assignMethod} = $association; } return $association; }