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 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; }