Howto implement input form to table X into a controller view of table Z

Hi, well i’m very new to oop, mvc, and Yii framework, and i’m no developer at full time…i just do it for fun.

Since i’m trying Yii and MVC for the first time…and i’m facing a problem that i think can be easily done with the right knowledge, i tought to ask here, cos i can’t find a solution anywhere :\

i’ve my webapp running with two tables tbl_videos and tbl_comments.

i’ve added some videos, and manualy some video comments to my tables.

And now i can view my videos (videos/view&id=1) where ‘id’ it’s the ‘id’ of the video under tbl_videos.

On that same video view i have a nice and working CListView widget with a great ajaxed pagination with this code below :)


<h1>Video Comments</h1>

<?php $this->widget('zii.widgets.CListView', array(

	'dataProvider' => new CActiveDataProvider('VideosComentarios', array(

				'criteria' => array(

				'condition' => 'video_id=:vId AND activo=:act',

				'order' => 'id DESC',

				'params' => array(

						':vId' => $model->id,

						':act' => true,



			'pagination' => array(

				'pageSize' => 2,



	'itemView' => '/videos/_view_comentarios',

	'ajaxUpdate' => true,

	//'ajaxVar' => 'ajax1',

	'id' => 'comentariosVideo',



that shows me the video that i’ve requested plus the corresponded comments for that same video.

And now what i want is to create a way in this same controller/view (videos/view.html) above this create a CForm/CActiveForm to insert new coments direcly into tbl_comments.

but i’m a little lost on how to implement that. can anyone show me some light on it.

in short: i want to be in the videos controller, ‘view’ show the flash video player, showing the form to insert into tbl_comments a new comment, and bellow the already inserted comments.

BUT i don’t want create a model and CRUD implmentation with gii in a way that users can see that 'insert comments form’if they directly go to ‘commentsController/create’ .

do i make myself clear?

and sorry for my bad english, it ain’t my native language.

thanks in advance

Hi optikool,

I think I understand your needs.

But, if I were you, I would do like this:


[*] Use Gii to generate a model for Comment.

[*] Use Gii to generate "tasteless" CRUD operation pages for Comment.

[*] Write fun codes in Video controller using the generated Comment model and making use of the generated Comment CRUD codes as tips.

[*] Remove or hide the unnecessary Comment CRUD pages from the end user.


You will need a model for Comment, anyway.

And there’s no need to limit the use of a model to the corresponding controller. You can use model A, B, and C in the controller A.

I believe that the simple but working codes generated by Gii will help you a lot when you try to develop some complicated function.

For example, you can use them as a set of tools to manipulate the test data while you are developing the fun part. And I think they are also good enough for back-end administrative pages in most cases.

hey softark,

Thanks for your fast reply and help!

yeah… in fact i got it working already… more or less the way you provide it to me…

  1. i’ve created a new videos view ‘_add_comment’ with simple CActiveForm for tbl_video_comments

  2. created that comments module

  3. added a public function addCommentsToVideo() into videos module that i use the Yii::app()->db->createCommand method and bindValues to my $sql query. and it’s working :)

my next step is:

  1. to set that CActiveForm to be ajax powered. (so the movie can keep playing, while data is inserted into tbl)

  2. find a way to delay some seconds the use of the CActiveForm, for preventing double record insertion

  3. find/learn if Yii have some built in methods for validating user input fields.

  4. and possibly after a new comment insertion, force in some way the refresh o CListView for showing the comments that we have for that video.

btw. are there any built in methods in Yii to validate user input? for exemple txt, double, int, email, etc?

in joomla you have the getInt() / getVar() / etc… etc… ?

PS - once again thanks alot softark for your help, thumbs up!

Yes. There are many of them.

Take a look at the definitive guide : Working with Forms > Creating Model > Declaring Validation Rules

In yii, we use some data model (CFormModel or CActiveRecord) to handle user input, and the model itself is responsible for its data validation.

I recommend you to read through the whole section of "Working with Forms".


And also “Working with Databases”, before you are going too far … you look so agile :)

ok softark,

thanks for the info…i’ll check those links and i’ll try to implement those next steps that i’ve talked you about.

btw… when you say …

4. Remove or hide the unnecessary Comment CRUD pages from the end user.

you mean delete the view files that i don’t want users to access directly, right ? if so will Yii rpovide a 404 page not found to user?

thanks for your help once again mate! :)

You have to delete the views and the corresponding actions for the views.

Yes there will be a 404 default error page (which you can customize of course) if someone try to access a controller action/route which doesn’t exist.

Hi kokomo,

ok great! thanks for the clarification mate!