1

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->find('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?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

11 Comments

What's the point of the Set::merge( as opposed to the array_merge mentioned below? (also - i assume you mean merge($v, $r), correct?)
@Abba - With some alterations, this pretty much works - the only issue now, is - all the restaurants show up at the bottom instead of being in alphabetical order - any thoughts on how to fix this?
It's an array - try using some of the handy sorting functions built into php. php.net/manual/en/array.sorting.php -- Example edited to show natural case-insensitive sorting but there are a ton of options.
Set::merge is not an exact functional copy of array_merge. I prefer to use cake's classes for such things when they exist as I assume they were written to handle any cake specific edge cases and to work with their data structuring.
I've tried just about every PHP sort, asort, ksort...etc I could think of - they all make the array print_r NOTHING - not sure why. I'll try natcasesort to see if that works, thanks!
|
1

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);

5 Comments

@Ish Kumar With some alterations, this pretty much works - the only issue now, is - all the restaurants show up at the bottom instead of being in alphabetical order - any thoughts on how to fix this?
@Ish - I thought that'd work too, but it doesn't - when I try to print_r it, it prints nothing. :/
Make sure you are NOT doing it like this $results = asort($results); ? because asort is called by reference and return boolean.
unfortunately array_merge doesn't retain key=>value relationship
$results = $venues + $restaurants instead of array_merge

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.