Default Relation Model Instnces


Is there any nice way to achieve something like this?:

I have a model Work, Work has 2 HAS_MANY relations and 1 HAS_ONE relation:

  • (many)Incomes

  • (many)Tasks

  • (one) Assignee

now when I am building a form for a new Work model, I have to do all the needed logic in the model controller create action, example:

 $model = new Work;

 $incomes = array(new Income);

 $tasks = array(new Task);

 $assignee = new User

 $this->render('form_fom_creating_new_work_record', array(/*all the models I created*/));

The thing I am thinking of, is invoking some magic for example in afterConstruct() method, to create those needed models and attach them to relations of Work, so that I don’t have to remember, to do it everywhere in the code elswhere I will use new Work;

Did you practice that, do you thinks it’s very wrong or rather usefull for you too?


Hi @fifik

You could to do something like that

$model = new Work; 


$incomes = array(new Income);

$tasks = array(new Task);

$assignee = new User();

$incomes[0]->work_id = $model->id; $incomes[0]->save();

$tasks[0]->work_id =   $model->id; $tasks[0]->save();

$assignee[0]->work_id = $model->id; $assignee[0]->save();