Here are the tables:
create table Super (
id int unsigned auto_increment primary key,
superAttr char(10) not null default ''
);
create table Sub (
id int unsigned primary key,
subAttr char(10) not null default ''
);
column id in table Sub is foreign key to Super.id,but I removed the foreign key as it’s not necessary.
And classes,normal functions such as model(),tableName(),rules(),etc didn’t list below for focusing.
class Sub extends Super
{
/**
* The followings are the available columns in table 'Sub':
* @var integer $id
* @var string $subAttr
*/
public $superAttr;
/**
* I didn't use AR relation in my approach
* @return array relational rules.
*/
public function relations()
{
return array(
);
}
public static function getParentDao()
{
return parent::model();
}
public function populateRecord($attributes,$callAfterFind=true)
{
return $this->populateRecordWithParent($attributes,$callAfterFind);
}
}
class Super extends CAgsAr
{
/**
* The followings are the available columns in table 'Super':
* @var integer $id
* @var string $superAttr
*/
/**
* I didn't use AR relation in my approach
* @return array relational rules.
*/
public function relations()
{
return array(
);
}
}
/**
* AegeanSiren ActiveRecord
* A patch of CActiveRecord
*/
abstract class CAgsAr extends CActiveRecord {
public static function model($className=__CLASS__)
{
if (__CLASS__ === $className)
{
/**
* prevent class that directly inherited from CAgsAr
* to get a model of CAgsAr
*/
return false;
}
else{
return parent::model($className);
}
}
/**
* populate model with both child and parent table contents
*/
public function populateRecordWithParent($attributes,$callAfterFind=true)
{
//call CActiveRecord::populateRecord() to do the normal job
$record = parent::populateRecord($attributes,$callAfterFind);
//then our showtime
if($attributes!==false)
{
$parentDao = call_user_func(array($this,'getParentDao'));
if (false === $parentDao)
{
throw new CException(get_class($this).' is not defined correctly to use populate with parent.');
}
else
{
//get the related record of parent table,and then merge it into the model.
$parentRecord = $parentDao->findByPk($record->primaryKey);
foreach($parentRecord->getMetaData()->columns as $name=>$column)
{
$record->$name=$parentRecord->$name;
}
return $record;
}
}
else
return null;
}
}
So far,we could load from both table.But inserting and updating is not done.
Still I’m double if my approach is good and if there are better ways.This is why I post there young codes here - Any ideas?Even “fool,all the world use a better approach that…” is welcome.
I’m too sleepy to keep on working for this moment.
Later I will post about progress.