문제

I think I'am hitting a limitation with phpactiverecord, but need clarification:

$data = array(
    'conditions' => array(
        'still_in_contest' => 1,
         'contest_month'   => $month,
         '`genres`.`id`'   => $genre, # THIS is the issue.. and an awkward "hack" that 
                                      # I was hoping would work..
     )
     'include' => array('song' => array('artist','genre')),
     'joins'   => array('
           LEFT JOIN songs 
               ON (songs.id = contest_submissions.song_id)
               LEFT JOIN genres 
                   ON (genres.id = songs.genre_id)',
     ),

Is it impossible to simply add something like genres.id into the query?

phpactiverecord is setting up the query as:

WHERE `contest_submissions`.`genres`.`id`=?

If it is is there some other way to still benefit from autoloading with another similar query without doing everything on my own?

EDIT: My solution right now has been to just modify the core of phpactiverecord's SQLBuilder class to not append the name of the table if there's a dot in the key...

EDIT2:

I tried your solution Nanne, and it iddn't work out. Phpactiverecord ends up prepending the table name to everything. :(

$genre = 1
    if($genre)
       $data['conditions']['genres.id'] = $genre;
     $data['conditions']['contest_submissions.still_in_contest'] = 1;
     $data['conditions']['contest_submissions.contest_month'] = $month;

It worked out previous because I kept my hack in. Did I format the condition incorrectly?

EDIT3:

yes.. yes I did format it incorrectly.

도움이 되었습니까?

해결책

You can do something like this. I've not tested it because I didn't have time to recreate your tables, but it is what I currently use: it is just a different syntax to use in your conditions.

$join   = "LEFT JOIN songs ON (songs.id = contest_submissions.song_id) 
           LEFT JOIN genres ON (genres.id = songs.genre_id)";
$models = \Models\YourModel::all(array( 
                  'joins'     => $join,
                 'conditions' => array('contest_submissions.still_in_contest = ? 
                                    AND contest_submissions.contest_month = ? 
                                    AND genres.id = ?',
                                 1,
                                 $month,
                                 $genre)));

(I hope I got the tablenames and the amount of arrays/brackets right :)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top