Has_Many Related Data Gridview In _Form

Hi Yii!! other question is: the "Project" model [color="#556B2F"]HAS_MANY[/color] "Tags", so i want to insert a related Tags [color="#FF8C00"]GridView [/color] like this "[color="#00BFFF"]admin.php[/color]" in "Tags" view (which work well):

To insert it into [color="#00BFFF"]"_form" [/color]"Project" view, i solved it with:

"Project" [color="#00BFFF"]_form[/color] view:




<?php /** @var BootActiveForm $form */

	$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(

		'id'=>'projects-form',

		'htmlOptions'=>array('class'=>'well'),

		'type'=>'horizontal',

	)); 

?>


	<fieldset>

		<p>Fields with <span class="required">*</span> are required.</p>


	<?php echo $form->errorSummary($model); ?>

		

		<?php echo $form->textFieldRow($model, 'title', array('class'=>'span3')); ?>

		<?php echo $form->textAreaRow($model, 'description', array('class'=>'span3', 'rows'=>3)); ?>

		<?php

			echo $this->renderPartial('_tags_admin', array('model'=>$model));

		?>


	</fieldset>


	<?php $form->widget('bootstrap.widgets.TbButton', array('buttonType'=>'submit', 'type'=>'primary', 'label'=>($model->isNewRecord ? 'Create' : 'Save'))); ?>


<?php $this->endWidget(); ?>



the "Project" [color="#00BFFF"]"_tags_admin.php"[/color] view:




<?php 

    $tags = new Tags;

    $this->widget('bootstrap.widgets.TbGridView',array(

        'id'=>'tags-grid',

        'dataProvider'=>$tags->searchProjectTags($model),

        'filter'=>$tags,

        'columns'=>array(

            'id',

            'tag',

            'position',

            'tag_project',

            'status',

            'create_time',

            array(

                    'class'=>'bootstrap.widgets.TbButtonColumn',

            ),

        ),

    ));

?>



The "Tags.php" model:




public function searchProjectTags($project)

	{

		$criteria=new CDbCriteria;


		$criteria->compare('tag_project',$project->id);

		

		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}



It works! the [color="#FF8C00"]GridView[/color] is showed with the correct related tags, but the [color="#800080"]"ButtonColumn"[/color] action is wrong, this seems to be linking to "Project", not to "Tags", i mean, the Tag1 "Update" "ButtonColumn" link to (…&r=project/update&id=1) but this shold be (…&r=tag/update&id=1).

Does any one knows why? And how to solve it?

Thank you!

Hi @Cesar

I use often TbActiveForm but I never have similar problem!

When the link related with tags controller ? (comment step by step your last code changes until you see the correct url to trace your code)

Also you could change manually the links buttons

http://www.yiiframework.com/wiki/106/using-cbuttoncolumn-to-customize-buttons-in-cgridview/

Hi KonApaz! Thank you for reply, the “[color=”#FF8C00"]TbActiveForm[/color]" on the “[color=”#00BFFF"]admin.php[/color]" “Tags” view work well, but if i use a “[color=”#FF8C00"]TbActiveForm[/color]" on the “[color=”#00BFFF"]_tags_admin.php[/color]" in “Projects” view give me the wrong result. I´m thinking to move “[color=”#00BFFF"]_tags_admin.php[/color]" to “Tags” views folder, but i don´t know how to use the “[color=”#FF8C00"]renderPartial(’[color="#00BFFF"]_tags_admin[/color]’, array(‘model’=>$model))[/color]" in the “[color=”#00BFFF"]_form.php[/color]" “Project” view

When you render a view from controller Project / Action then all generated Forms , gridview etc point to this controller/action (Project/Action). So rendering _tags_admin.php by controller "Project" generates all links (Project/Action). So you have to set manually the links to another Controller (tags controller)

[color="#006400"] - SOLVED -[/color]

"[color="#00BFFF"]_tags_admin.php[/color]" in "Projects" view




<?php 

    $tags = new Tags;

    $this->widget('bootstrap.widgets.TbGridView',array(

        'id'=>'tags-grid',

        'dataProvider'=>$tags->searchProjectTags($model),

        'filter'=>$tags,

        'columns'=>array(

            'id',

            'tag',

            'position',

            'tag_project',

            'status',

            'create_time',

            array(

                    'class'=>'bootstrap.widgets.TbButtonColumn',

                    'template'=>'{update}{delete}',

                    'buttons'=>array

                    (

                        'update' => array

                        (

                            'url'=>'Yii::app()->createUrl("tags/tags/update", array("id"=>$data->id))',

                        ),

                        'delete' => array

                        (

                            'url'=>'Yii::app()->createUrl("tags/tags/delete", array("id"=>$data->id))',

                        ),

                    ),

            ),

        ),

    ));

?>



Thank you KonApaz