开发者

Retrieve all data from one table, and some from another in CakePHP

In my CakePHP site, I want to make a drop-down list of all Venues, and any Restaurants that have is_venue=1.

I've tried this in my events_controller:

        $venueOptions = array(
        'fields' => array('id', 'name_address'),
        'order' => array('name'),
        'join' => array(
            array(
                'table' => 'restaurants',
                'alias' => 'Restaurants',
                'type' => 'inner',
                                    'fields' => array('id', 'name'),
                'foreignKey' => false,
                'conditions' => array('restaurants.is_venue = 1')
            )
        ),
    );
    $venues = $this->Event->Venue->fi开发者_如何学运维nd('list', $venueOptions);

But it appears to still just be getting the venues. I don't really need an association between the two, since their associations will both be with an event, not each other.

Where have I gone wrong? Am I close, but just need to tweak this code, or am I just all-together doing it wrong?


I think you could do something along the lines of:

<?php
    ....
    $v = $this->Venue->find( 'list' );
    $r = $this->Restaurant->find( 'list' );
    $venues = Set::merge( $v, $r );
    natcasesort( $venues );
    // print_r( $venues );

    $this->set( 'venues', $venues );
    ...
?>

Which is quite like the code above - I just use the Set class and make sure to Controller::set the variable to the view.

Also added some basic sorting to show you one option even though array sorting has nothing really specific to do with CakePHP.

Also fixed some bad variable names where I had originally used $venues, and $restaurants - changed to be consistently $v and $r.


Join will not work if there's no relation between. Venue and Restaurant. You should call them separately and merge the results

$venues = $this->Event->Venue->find('list', $venueOptions);
$restaurants = $this->Event->Restaurant->find('list', array('conditions' => array('is_venue' => '1')));
$results = array_merge($venues, $restaurants);

// sort results
asort($results);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜