में कंटेनर करने योग्य स्थितियों को जोड़ना इससे पहले कि मैं रिकर्सिव पर निर्भर था, लेकिन मुझे कुछ के लिए समाधान नहीं मिला, तो मैंने पाया कि इनके लिए कंटेनबल काम ठीक है।केकेपीएचपी
मैं एक फिल्म समीक्षा वेबसाइट विकसित कर रहा हूं। उसमें मुझे किसी विशेष शैली से संबंधित फिल्मों की सूची दिखाने की ज़रूरत है।
मैं कोड के नीचे इस राशि:
//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());
वास्तविक समस्या यहाँ शुरू होता है, मैं सभी फिल्मों मिलता है, भले ही अपनी शैली "नाटक" से संबंधित नहीं। मैं गलत कहां जा रहा हूं?
तालिका:: फिल्मों
----------------------------
| id | title | description |
----------------------------
| 1 | mov1 | something1 |
| 2 | mov2 | something2 |
| 3 | mov3 | something3 |
----------------------------
तालिका: शैलियों
---------------
| id | name |
---------------
| 1 | drama |
| 2 | sci-fi |
| 3 | comedy |
---------------
तालिका: movie_genres
-------------------------------
| id | movie_id | genre_id |
-------------------------------
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 2 |
-------------------------------
यहाँ आप देख सकते हैं कि
मुझे डेटाबेस तालिका के बारे में बताएं एक mov ie_id में एकाधिक genre_id है। मुझे केवल mov1
मिलना चाहिए, लेकिन मुझे दोनों फिल्मों को एक सरणी में मिल रहा है।
~~ संपादित करें ~~ ओह !! क्षमा करें क्षमा करना भूल गया, मैं इस कोड का उपयोग MoviesController
में कर रहा हूं। सभी 3 टेबल में संबंधित नियंत्रक है। तो कृपया मुझे सुझाव दें कि मैं किस नियंत्रक का उपयोग कर सकता हूं।
संपादित करें: 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' => ''
),
);
}
मॉडल: शैली
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' => ''
)
);
}
मॉडल: 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' => ''
)
);
}
आपका मॉडल कहां है? –
मैंने मॉडल जोड़ा है :) –