Yii Framework Forum: Write Id From Selected Row From Cgridview To Database - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Write Id From Selected Row From Cgridview To Database Rate Topic: -----

#1 User is offline   FR45 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 22-March 13

Posted 27 March 2013 - 07:27 AM

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 (
  id INT NOT NULL PRIMARY KEY,
  name VARCHAR(45))
 
CREATE  TABLE movie (
  id INT NOT NULL PRIMARY KEY,
  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.
0

#2 User is offline   Tsunami 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 150
  • Joined: 16-February 12

Posted 27 March 2013 - 07:53 AM

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.
0

#3 User is offline   FR45 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 22-March 13

Posted 27 March 2013 - 08:33 AM

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.
0

#4 User is offline   Tsunami 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 150
  • Joined: 16-February 12

Posted 27 March 2013 - 11:26 AM

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.
0

#5 User is offline   FR45 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 22-March 13

Posted 28 March 2013 - 04:07 AM

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?
0

#6 User is offline   FR45 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 22-March 13

Posted 29 March 2013 - 07:15 AM

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(
           'type'=>'POST',
           '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']);

0

#7 User is offline   Tsunami 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 150
  • Joined: 16-February 12

Posted 29 March 2013 - 08:21 AM

I think you're confusing CHtml::ajaxSubmitButton with CHtml::submitButton.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users