Yii create child form in parent view

Hello,

Lets say I have 2 models "Project" and "Task" in a 1:M relationship, both have Gii generated CRUD(actions and views).

Now, when viewing a project in views/project/view.php, I want the viewer to be able to create new Tasks for the currently viewed project, directly in that view.php file. Tasks have a project_id foreign key field that would need to be filled.

I’m don’t know if I should render the current secret/create action in the project/view file somehow, or if I should generate a new form with Gii, and somehow rendering it in the project/view. Basically I don’t know what I’m doing here. :(

Can someone show me how to do what I mentioned above, or provide a link to a resource showing how this is done, please?

Thanks,

if i were you :lol: , i will do the following :

in project view.php file , create a link : CHtml::link(‘create a task’,$this->createUrl(’/task/create’,array(‘project_id’, $model->id)));

then use gii to generate the crud views for "Task" in the TaskController::actionCreate function :





  

   $model=new Task;


   if(isset($_GET['project_id'])){


   $model->project_id = $_GET['project_id'];

  }

  .....



just my thoughts , never test it . :D

Thank you for your thoughts yiqing95. :) I have already got the CRUD generated for the Task model, and I currently have a link, as you described, that will allow me to create new tasks for a Project, but this loads a seperate view file for doing so.

To clarify: my goal is to have a (partial view?) form directly in the views/project/view.php file for creating new Tasks associated with the currently viewed project. In the form, I would probably just use a hiddenField to pass the project_id. I have been able to get a partial view form to show up from the views/project/view.php file: but, I have no idea how to handle the backend side of things for saving the forms data. My understanding in this area is pretty weak, and I would appreciate any advice on how to have a form for creating child objects, directly in the parents view.

I have read through this part of the definitive guide to yii: Working With Forms, but it is a bit too vague for my understanding. Could someone tell me if following this section of the guide is the proper approach for what I am trying to achieve, and maybe what major differences I should expect? :huh: Such as how I will need my form to be a partial view file. Then I can at least know what to study.

Thanks,

Can anyone help me with this please? Even a push in the right direction would be much appreciated at this point. :(

I am looking for a similar solution with out any luck. A good tutorial for having a parent form with child records (allowing editing) would be fantastic. All i get is Collecting Tabular Input links…

i am a bit newbie but it should be simple

this is what i will do if i was you was try to do:

in short

copy the content from you task controller create action something like this:


  $model = new Task;

        if (isset($_POST['Task'])) {

            $model->setAttributes($_POST['Task']);


            if ($model->save()) {

                if (Yii::app()->getRequest()->getIsAjaxRequest())

                    Yii::app()->end();

                else {

                      $this->redirect(array('view', 'id' => $model->id));

                }

            }

        }



now in your project view,renderPartial your task view->_form

and it should work try it(i dont know if its the best way)