Note that there are some explanatory texts on larger screens.

plurals
  1. POCakephp HABTM saving -.-
    primarykey
    data
    text
    <p>I'm googling high and low on this. I'm not sure where i went wrong cause I don't get any errors.</p> <p>i have 3 tables: </p> <p>MOVIES - id, title, genre, etc</p> <p>GENRES - id, genre</p> <p>GENRES_MOVIES - movie_id, genre_id</p> <p>Movie model:</p> <pre><code>public $hasAndBelongsToMany = array( 'Genre' =&gt; array( 'className' =&gt; 'Genre', 'joinTable' =&gt; 'genres_movies', 'foreignKey' =&gt; 'movie_id', 'associationForeignKey' =&gt; 'genre_id' ) ); </code></pre> <p>View:</p> <pre><code>echo $this-&gt;Form-&gt;create('Movie', array('controller' =&gt; 'movies', 'action' =&gt; 'add', 'type' =&gt; 'file')); echo $this-&gt;Form-&gt;input('title'); echo $this-&gt;Form-&gt;input('description', array('type' =&gt; 'textarea'); echo $this-&gt;Form-&gt;input('imdb_url'); echo $this-&gt;Form-&gt;year('release_year', 1900, date('Y')); echo $this-&gt;Form-&gt;input('length', array('type' =&gt; 'number')); echo $this-&gt;Form-&gt;input('genre', array('type'=&gt;'select', 'multiple'=&gt;'checkbox', 'options'=&gt; $genres)); echo $this-&gt;Form-&gt;input('image', array('type' =&gt; 'file')); echo $this-&gt;Form-&gt;button('Submit'); </code></pre> <p>MoviesController:</p> <pre><code> public function add() { /* for populating checkbox */ $this-&gt;set('genres', $this-&gt;Movie-&gt;Genre-&gt;find('list', array('fields' =&gt; array('Genre.id', 'Genre.genre')))); if($this-&gt;request-&gt;is('post')) { $data_movie = array( 'title' =&gt; $this-&gt;request-&gt;data['Movie']['title'], 'description' =&gt; $this-&gt;request-&gt;data['Movie']['description'], 'imdb_url' =&gt; $this-&gt;request-&gt;data['Movie']['imdb_url'], 'release_year' =&gt; (int)$this-&gt;request-&gt;data['Movie']['release_year']['year'], 'length' =&gt; (int)$this-&gt;request-&gt;data['Movie']['length'], 'user_id' =&gt; (int)$this-&gt;Auth-&gt;user('id') ); $data_genre = array('Genre' =&gt; array()); foreach($this-&gt;request-&gt;data['Movie']['genre'] as $genre) array_push($data_genre['Genre'], (int)$genre); $data = array('Movie' =&gt; $data_movie, 'Genre' =&gt; $data_genre); $this-&gt;Movie-&gt;create(); if($this-&gt;Movie-&gt;save($data) { echo 'success'; } } } </code></pre> <p>So, why did I bother setting up data manually for saving in the movieController, it is because the tutorials are telling me data should be organised like it is now (before that it looked a little different.. so i tried and this is what i have now (it looks ok to me) <a href="http://s10.postimg.org/ikewszo95/Untitled_1.jpg" rel="nofollow">http://s10.postimg.org/ikewszo95/Untitled_1.jpg</a></p> <p>you can see the form here <a href="http://marko-stimac.iz.hr/temp/movies/movies/add" rel="nofollow">http://marko-stimac.iz.hr/temp/movies/movies/add</a> (although submit wont work, i guess it is because of the Auth component not letting non-registered users)</p> <p>hm any help would be greeatly appreciated :)</p> <p>EDIT - updated controller</p> <pre><code>public function add() { $this-&gt;set('genres', $this-&gt;Movie-&gt;Genre-&gt;find('list', array('fields' =&gt; array('Genre.id', 'Genre.genre')))); if($this-&gt;request-&gt;is('post')) { ($this-&gt;Movie-&gt;saveAssociated($this-&gt;request-&gt;data)) { $this-&gt;redirect('/'); $this-&gt;Session-&gt;setFlash('Success.'); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload