Write Id From Selected Row From Cgridview To Database

Hey, I am new to yii and struggle with that problem now for hours. I don’t even know, how i should search this or name it.

Let’s say i have 3 tables.

CREATE  TABLE viewer (


  name VARCHAR(45))




  title VARCHAR(45))


CREATE  TABLE viewer_watched_movie (

  viewer_id INT NOT NULL,

  movie_id INT NOT NULL,

  liked TINYINT(1),

  PRIMARY KEY (viewer_id, movie_id),

  CONSTRAINT fk_viewer_watched

    FOREIGN KEY (movie_id) REFERENCES movie (id)

  CONSTRAINT fk_movie_watched_by

    FOREIGN KEY (viewer_id) REFERENCES viewer (id))

I have a CRUD for viewer_watched_movie. Now what i want is, that the user can create viewer_watched_movie and select the viewer from a dropdownlist, which works fine, and to select the movie from a cgridview. The id from movie should then be saved to user_watched_movie. How should I do that or If there is a better way of getting the id, please let me know.

What does your CGridView look like? If you use CCheckBoxColumn you can set its name and and use $_POST[name] to get the selected row. Otherwise you probably need to use $(gridID).yiiGridView(‘getSelection’) with JavaScript.

I haven’t created the CGridView yet. I tried to copy the CGridView from admin.php into create.php which doesn’t work and it seems something is missing to display any results.

To use CheckBox is a good idea. But if you select multiple rows, what happens then? It would be nice to add multiple selections at once. But I think this is only possible with a script then and I don’t know how to write a script that is able to do so.

I thought to catch the row selection, and write the id into the textField, so i can use the required too. Don’t know if this works.

Well documentation on how to create a CGridView is of course on /doc/api/CGridView. selectableRows defines how many rows you can select at once, $_POST[name] and $(gridID).yiiGridView(‘getSelection’) will be an array.

Thx for now.

I named the model of movie the same like viewer_watched_movie.

Trying to do the selectableRows now.

Is it possible to drag and drop rows between two tables. So I can drop movie to user and write to viewer_watched_movie?

I can’t get the Id in my Controller.

I tried to do this

<div class="row buttons">

		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save',  array(


           'data'=>'js:{theIds : $.fn.yiiGridView.getChecked("movie-grid","selectedIds").toString()}'

        )); ?>

and grap it in the actionCreate(), but I am doing something wrong here.

$arra=explode(',', $_POST['theIds']);

I think you’re confusing CHtml::ajaxSubmitButton with CHtml::submitButton.