CakePHP - paginate HABTM request problem

Table

restaurants
cuisines
cuisines_restaurants

Both restaurant and kitchen models are tuned for each other's HABTM.

I try to get a broken list of restaurants where Cuisine.name = 'italian' (example), but keep getting this error:

1054: Unknown column 'Cuisine.name' in 'where clause'

Actual request:

SELECT `Restaurant`.`id`, `Restaurant`.`type` ..... 
`Restaurant`.`modified`, `Restaurant`.`user_id`, `User`.`display_name`,
`User`.`username`, `User`.`id`, `City`.`id`,`City`.`lat`  ..... 
FROM `restaurants` AS `Restaurant` LEFT JOIN `users` AS `User` ON 
(`Restaurant`.`user_id` = `User`.`id`) LEFT JOIN `cities` AS `City` ON 
(`Restaurant`.`city_id` = `City`.`id`) WHERE `Cuisine`.`name` = 'italian' 
LIMIT 10

The "....." parts are just extra fields that I removed to shorten the request, to show you.

I'm not a CakePHP professional, so hopefully there is some glaring mistake. I call paginate as follows:

$this->paginate = array(
    'conditions' => $opts,
    'limit' => 10,
);
$data = $this->paginate('Restaurant');
$this->set('data', $data);

$ opts is an array of options, one of which is 'Cuisine.name' => 'italian'

I also tried setting $ this-> Restaurant-> recursive = 2; but it seemed to do nothing (and I suppose I don't need to do this?)

Any help or direction is appreciated.


EDIT

models/cuisine.php
    var $hasAndBelongsToMany = array('Restaurant');

models/restaurant.php
    var $hasAndBelongsToMany = array(
    'Cuisine' => array(
        'order' => 'Cuisine.name ASC'
    ),
    'Feature' => array(
        'order' => 'Feature.name ASC'
    ),
    'Event' => array(
        'order' => 'Event.start_date ASC'
    )
);
+5
5

, Cake 2 . , Cuisine.

@vindia , Containable , Paginate.

, Cake . , , , , , paginate , Model->find('all'). joins.

var $joins = array(
    array(
        'table' => '(SELECT cuisines.id, cuisines.name, cuisines_restaurants.restaurant_id
                 FROM cuisines_restaurants 
                 JOIN cuisines ON cuisines_restaurants.cuisines_id = cuisines.id)',
        'alias' => 'Cuisine',
        'conditions' => array(
            'Cuisine.restaurant_id = Restaurant.id',
            'Cuisine.name = "italian"'
        )
    )
);

$this->paginate = array(
    'conditions' => $opts,
    'limit' => 10,
    'joins' => $joins
);

clunkier , .

+5

, contain .

, -

# in your restaurant_controller.php
var $paginate = array(
    'contain' => array(
        'Cuisine' => array(
            'conditions' => array('Cuisine.name' => 'italian')
        )
    ),
    'limit' => 10
);

# then, in your method (ie. index.php)
$this->set('restaurants', $this->paginate('Restaurant'));
+6

:

  • , , HABTM?
  • .. paginator

!

0

Cuisine ( ) FROM SELECT. :
1054: 'Cuisine.name' 'where clause'
, FROM

0

If you remove a part of the component and events of your HABTM link in a restaurant model, does it mean that it works? It sounds as if you could not determine the correct primary and force keys for the Cuisine model, because the HABTM model does not even include the Cuisine tab in the query that you posted here.

0
source

All Articles