【问题标题】:Loading conditions that refer to another table - Phpactiverecord加载引用另一个表的条件 - Phpactiverecord
【发布时间】:2012-03-04 21:51:53
【问题描述】:

我认为我遇到了 phpactiverecord 的限制,但需要澄清:

$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)',
     ),

难道不能简单地在查询中添加genres.id 之类的内容吗?

phpactiverecord 将查询设置为:

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

如果有其他方法仍然可以从自动加载另一个类似查询中受益,而无需我自己做所有事情?

编辑: 我现在的解决方案是只修改 phpactiverecord 的 SQLBuilder 类的核心,如果键中有一个点,则不附加表的名称......

编辑2:

我尝试了您的解决方案 Nanne,但没有成功。 Phpactiverecord 最终将表名添加到所有内容中。 :(

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

之前成功了,因为我保留了我的 hack。我是否错误地格式化了条件?

EDIT3:

是的..是的,我确实格式不正确。

【问题讨论】:

    标签: php phpactiverecord


    【解决方案1】:

    你可以做这样的事情。 我没有测试它,因为我没有时间重新创建你的表,但它是我目前使用的:它只是在你的条件下使用的不同语法。

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

    (我希望我得到了正确的表名和数组/括号的数量:)

    【讨论】:

    • 天才;]!我很高兴你能在这里为 phpactiverecord 提供这么多帮助 =P 你是开发人员之一吗?我会尝试一下并删除我对核心类的更改,看看会发生什么
    • 不是,而是活跃用户(和活跃的 SO 用户)。我希望在这个主题上得到一些关于 SO 的讨论,因为 phpAR 的论坛并没有被太多人访问:)(我更喜欢 SO 作为一个 QA 平台)
    • 我看到了需要进行的更改,但我会固执地对待我的 hack,因为我确实希望能够使用数组轻松地将动态需要的字段添加到条件中,而不是字符串连接和数组推送。不过,这确实是问题的正确答案!
    猜你喜欢
    • 1970-01-01
    • 2014-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-11
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    相关资源
    最近更新 更多