Hi There
I’m new to Yii and MVC frameworks, but so far i’m enjyoying my Yii experience.
Currently as a test application to learn Yii i’m building myself a task manager which so far is working well. however i’m a little unsure as to whether i’m doing things correctly.
So the situation is that i have 3 tables, projects, jobs and tasks.
-> Project, which is the parent
-> Job , which is a child of project, can have many jobs for each project
-> Task, child of job, can have many tasks for each job.
In order to add a job they first have to select a project, then click "add job" and that will then add a job to the project.
likewise for a task, they need to choose a project, select a job, and then click "add task" that adds a task to the job.
Currently i have all my controller code in ProjectsController.php as they always need to have selected a project first before they can add a job. This is all working, however it strikes me that maybe the JobsController.php & TasksController.php should be handling the "Create" and "Update" calls when dealing with jobs and tasks.
Example code from my ProjectsController.php when adding a job to a project.
public function actionAddJob($id)
{
$project=$this->loadModel($id);
$jobs=$this->newJob($project);
$this->render('view/job/create',array(
'model'=>$project,
'jobs'=>$jobs,
));
}
and the newJob function
protected function newJob($project)
{
$jobs=new Jobs;
if(isset($_POST['ajax']) && $_POST['ajax']==='jobs-form')
{
echo CActiveForm::validate($jobs);
Yii::app()->end();
}
if(isset($_POST['Jobs']))
{
$jobs->attributes=$_POST['Jobs'];
if($project->addJob($jobs))
{
$this->redirect(array('viewjobs','id'=>$project->id));
}
}
return $jobs;
}
So the question is, is this the right way to do it, and if not what’s the best approach.
Hope that makes some sense
Thanks