Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8678723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:47:45+00:00 2026-06-12T20:47:45+00:00

Previously I was relying on recursive, but I didn’t get solution for some, then

  • 0

Previously I was relying on recursive, but I didn’t get solution for some, then I found that Containable works fine for these.

I am developing a movie review website. In that I need to show the list of movies which is related to a particular Genre.

I have this below code:

//example
$genre = "drama";

$options = array(
    'contain' => array(
        'Movie',
        'MoveiGenre.Genre' => array(
            'conditions' => array('MovieGenre.Genre.name = "'.$genre.'"')
        ),
        'MovieGenre.Genre.name'
    ),
    'recursive' => 2,
    'limit' => 10
);
$this->paginate = $options;
$this->set('movies',$this->paginate());

The real problem starts here, I get all the movies, even if its not related to the genre “drama”.
Where am I going wrong ?

Let me explain the database table:

Table: movies

 ----------------------------
 | id | title | description |
 ----------------------------
 | 1  | mov1  | something1  |
 | 2  | mov2  | something2  |
 | 3  | mov3  | something3  |
 ----------------------------

Table: genres

 ---------------
 | id | name   |
 ---------------
 | 1  | drama  |
 | 2  | sci-fi |
 | 3  | comedy |
 ---------------

Table: movie_genres

 -------------------------------
 | id | movie_id | genre_id    |
 -------------------------------
 | 1  | 1        | 1           |
 | 2  | 1        | 2           |
 | 3  | 2        | 2           |
 -------------------------------

Here you can see that one movie_id has multiple genre_id. I should get only mov1 but I’m getting both movies in an array.

~~ EDIT ~~
oops!!
sorry forgot to mention, I’m using this code in MoviesController. All 3 table has respective controller. So pls suggest me in which controller I can use.

EDIT:2

class Movie extends AppModel {
    public $displayField = 'title';

    public $actsAs = array('Containable');

    public $hasMany = array(
        'MovieGenre' => array(
            'className' => 'MovieGenre',
            'foreignKey' => 'movie_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'MovieLanguage' => array(
            'className' => 'MovieLanguage',
            'foreignKey' => 'movie_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
    );

}

Model: Genre

class Genre extends AppModel {

    public $displayField = 'name';

    public $hasAndBelongsToMany = array(
        'Movie' => array(
            'className' => 'Movie',
            'joinTable' => 'movie_genres',
            'foreignKey' => 'genre_id',
            'associationForeignKey' => 'movie_id',
            'unique' => 'keepExisting',
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
            'deleteQuery' => '',
            'insertQuery' => ''
        )
    );

}

Model: MovieGenre

class MovieGenre extends AppModel {

    public $belongsTo = array(
        'Movie' => array(
            'className' => 'Movie',
            'foreignKey' => 'movie_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Genre' => array(
            'className' => 'Genre',
            'foreignKey' => 'genre_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T20:47:47+00:00Added an answer on June 12, 2026 at 8:47 pm

    TLDR: 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.


    Explanation:

    Below is your corrected ‘contain’ code, but more importantly, doing a ‘contain’ on Genre won’t return the results you’re looking for.

    What it does – limits the contained genres based on your condition… so it will pull ALL movies, and contain the genres that match $genre.


    Solutions (depending on your needs):

    Solution 1)

    • Do a find() 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.

    Solution 2) – the one I’d recommend

    • Use ‘joins()’:

    $conditions = array();
    $conditions['joins'] = array(
        array(
            'table' => 'genres_movies', //or movie_genres if you've specified it as the table to use
            'alias' => 'GenresMovie',
            'type' => 'INNER'
            'conditions' => array(
                'GenresMovie.movie_id = Movie.id'
            )
        ),
        array(
            'table' => 'genres',
            'alias' => 'Genre',
            'type' => 'INNER',
            'conditions' => array(
                'Genre.id = GenresMovie.genre_id',
                'Genre.name = "' . $genre . '"'
            )
        )
    );
    $this->Movie->find('all', $conditions);
    

    Your edited (corrected imo) ‘contain’ example

    //edited example
    $genre = "drama";
    
    $options = array(
        'contain' => array(
            'Genre' => array(
                'conditions' => array('Genre.name' => $genre)
            )
        ),
        'recursive' => -1,
        'limit' => 10
    );
    $this->paginate = $options;
    $this->set('movies', $this->paginate('Movie'));
    
    1. you don’t “contain” the model you’re doing the find on (ie I removed ‘Movie’ from your contain array).
    2. you only contain models, not things like “MovieGenre.Genre” (I can’t think of any time you would use ‘.’ concatenated models)
    3. 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
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Previously I've used PyVisa1.4 in Python2.7 , and everything works fine. Now I need
Previously, I have worked on local notifications, but I got problem in it now.
Previously I did a Tutorial on OpenGL-ES 1.0. For reference, this can be found
Previously, I have an app that uses core data. I use same store url
Previously I'd worked on a flash application that would be embedded into a webpage
Previously .NET SQLite libraries were available from http://sqlite.phxsoftware.com , but they have recently been
Previously I've worked alone... Now I may have many collaborators. But I don't want
Previously i wrote an app that used reflection to serialize data from json to
Previously I read posts with same problem but my question persists. My app needs
Previously i have used oracle 10g version . but today i uninstalled oracle 10g

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.