Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding conditions to Containable in CakePHP
    primarykey
    data
    text
    <p>Previously I was relying on recursive, but I didn't get solution for some, then I found that Containable works fine for these.</p> <p>I am developing a movie review website. In that I need to show the list of movies which is related to a particular Genre.</p> <p>I have this below code:</p> <pre><code>//example $genre = "drama"; $options = array( 'contain' =&gt; array( 'Movie', 'MoveiGenre.Genre' =&gt; array( 'conditions' =&gt; array('MovieGenre.Genre.name = "'.$genre.'"') ), 'MovieGenre.Genre.name' ), 'recursive' =&gt; 2, 'limit' =&gt; 10 ); $this-&gt;paginate = $options; $this-&gt;set('movies',$this-&gt;paginate()); </code></pre> <p>The real problem starts here, I get all the movies, even if its not related to the genre "drama". Where am I going wrong ?</p> <p>Let me explain the database table:</p> <p>Table: movies</p> <pre><code> ---------------------------- | id | title | description | ---------------------------- | 1 | mov1 | something1 | | 2 | mov2 | something2 | | 3 | mov3 | something3 | ---------------------------- </code></pre> <p>Table: genres</p> <pre><code> --------------- | id | name | --------------- | 1 | drama | | 2 | sci-fi | | 3 | comedy | --------------- </code></pre> <p>Table: movie_genres</p> <pre><code> ------------------------------- | id | movie_id | genre_id | ------------------------------- | 1 | 1 | 1 | | 2 | 1 | 2 | | 3 | 2 | 2 | ------------------------------- </code></pre> <p>Here you can see that one movie_id has multiple genre_id. I should get only <code>mov1</code> but I'm getting both movies in an array.</p> <p>~~ <strong>EDIT</strong> ~~ oops!! sorry forgot to mention, I'm using this code in <code>MoviesController</code>. All 3 table has respective controller. So pls suggest me in which controller I can use.</p> <p><strong>EDIT:2</strong></p> <pre><code>class Movie extends AppModel { public $displayField = 'title'; public $actsAs = array('Containable'); public $hasMany = array( 'MovieGenre' =&gt; array( 'className' =&gt; 'MovieGenre', 'foreignKey' =&gt; 'movie_id', 'dependent' =&gt; false, 'conditions' =&gt; '', 'fields' =&gt; '', 'order' =&gt; '', 'limit' =&gt; '', 'offset' =&gt; '', 'exclusive' =&gt; '', 'finderQuery' =&gt; '', 'counterQuery' =&gt; '' ), 'MovieLanguage' =&gt; array( 'className' =&gt; 'MovieLanguage', 'foreignKey' =&gt; 'movie_id', 'dependent' =&gt; false, 'conditions' =&gt; '', 'fields' =&gt; '', 'order' =&gt; '', 'limit' =&gt; '', 'offset' =&gt; '', 'exclusive' =&gt; '', 'finderQuery' =&gt; '', 'counterQuery' =&gt; '' ), ); } </code></pre> <p>Model: Genre</p> <pre><code>class Genre extends AppModel { public $displayField = 'name'; public $hasAndBelongsToMany = array( 'Movie' =&gt; array( 'className' =&gt; 'Movie', 'joinTable' =&gt; 'movie_genres', 'foreignKey' =&gt; 'genre_id', 'associationForeignKey' =&gt; 'movie_id', 'unique' =&gt; 'keepExisting', 'conditions' =&gt; '', 'fields' =&gt; '', 'order' =&gt; '', 'limit' =&gt; '', 'offset' =&gt; '', 'finderQuery' =&gt; '', 'deleteQuery' =&gt; '', 'insertQuery' =&gt; '' ) ); } </code></pre> <p>Model: MovieGenre</p> <pre><code>class MovieGenre extends AppModel { public $belongsTo = array( 'Movie' =&gt; array( 'className' =&gt; 'Movie', 'foreignKey' =&gt; 'movie_id', 'conditions' =&gt; '', 'fields' =&gt; '', 'order' =&gt; '' ), 'Genre' =&gt; array( 'className' =&gt; 'Genre', 'foreignKey' =&gt; 'genre_id', 'conditions' =&gt; '', 'fields' =&gt; '', 'order' =&gt; '' ) ); } </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.
 

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