0

I have projects and tasks in my application, and I want (as a simple example) that after I create a project, two tasks will be generated by default and assigned to this project.

The tasks are at the beginning the same for all projects. Then it is possible to update the task for a single project...

For example:

I create Project_A -> generation of task1, task2

I create Project_B -> generation of task1, task2

Is this possible with Yii?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Leon
  • 255
  • 1
  • 4
  • 11
  • Yes. When you save your project model then create 2 task models and save them as well. Show us your code will show you how. – Think Different Oct 08 '14 at 14:07
  • afterSave() your project if it is new entry then count no of task of current project and if it is zero then create two default task for saved project. – Yatin Mistry Oct 08 '14 at 14:27

1 Answers1

0

Put this function in Project.php model file:

public function beforeSave() {

    // Create two tasks and save them to get an auto-increment key from the database
    $task1 = new Task;
    $task1->save();

    $task2 = new Task;
    $task2->save();

    // $this refers to the current project
    // Assuming we have two fields to store the IDs of the tasks
    $this->task1 = $task1->primaryKey;
    $this->task2 = $task2->primaryKey;

    return parent::beforeSave();
}
Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
  • Can you expand your answer to include an explanation of your code? It helps the reader more than you might think. – gunr2171 Oct 08 '14 at 17:03