Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>TLDR:</strong> do your find on Genre and contain it's Movies, or use joins() - doing your search on Movies and containing Genre with conditions won't work for the results you want.</p> <hr> <p><strong>Explanation:</strong></p> <p>Below is your corrected 'contain' code, but more importantly, doing a 'contain' on Genre won't return the results you're looking for.</p> <p>What it does - limits the contained genres based on your condition... so it will pull ALL movies, and contain the genres that match <code>$genre</code>.</p> <hr> <p><strong>Solutions (depending on your needs):</strong></p> <p><em>Solution 1)</em></p> <ul> <li>Do a <code>find()</code> on Genre, with the condition, and contain it's movies. This will pull the genre that matches, then only the movies that are related to it.</li> </ul> <p><em>Solution 2)</em> - the one I'd recommend</p> <ul> <li>Use 'joins()':</li> </ul> <hr> <pre><code>$conditions = array(); $conditions['joins'] = array( array( 'table' =&gt; 'genres_movies', //or movie_genres if you've specified it as the table to use 'alias' =&gt; 'GenresMovie', 'type' =&gt; 'INNER' 'conditions' =&gt; array( 'GenresMovie.movie_id = Movie.id' ) ), array( 'table' =&gt; 'genres', 'alias' =&gt; 'Genre', 'type' =&gt; 'INNER', 'conditions' =&gt; array( 'Genre.id = GenresMovie.genre_id', 'Genre.name = "' . $genre . '"' ) ) ); $this-&gt;Movie-&gt;find('all', $conditions); </code></pre> <hr> <p><strong>Your edited (corrected imo) 'contain' example</strong></p> <pre><code>//edited example $genre = "drama"; $options = array( 'contain' =&gt; array( 'Genre' =&gt; array( 'conditions' =&gt; array('Genre.name' =&gt; $genre) ) ), 'recursive' =&gt; -1, 'limit' =&gt; 10 ); $this-&gt;paginate = $options; $this-&gt;set('movies', $this-&gt;paginate('Movie')); </code></pre> <ol> <li>you don't "contain" the model you're doing the find on (ie I removed 'Movie' from your contain array).</li> <li>you only contain models, not things like "MovieGenre.Genre" (I can't think of any time you would use '.' concatenated models)</li> <li>recursive needs to be -1 to use Containable - you should set this to -1 in the AppModel and forget the concept of recursive - it's bad</li> </ol>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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