【问题标题】:LEFT JOIN Association returns unknown columnLEFT JOIN 关联返回未知列
【发布时间】:2013-03-19 09:21:01
【问题描述】:

我正在使用 PHPActiveRecord 和 CodeIgniter。我正在尝试创建一个博客 CMS,一个博客有很多类别,并且它引用另一个名为 blogrefs 的表。所以基本上

blogs => blogrefs => blogcats

blogrefs 通过 blogrefs.blog_id 引用博客

blogrefs 通过 blogrefs.blogcat_id 引用 blogcats

    function get_blogs($params = array()){

    $CI =& get_instance();

    $joins  = 'LEFT JOIN blogrefs ON blogs.id = blogrefs.blog_id';
    $joins  .= 'LEFT JOIN blogcats ON blogrefs.blogcat_id = blogcats.id';
    $category = (isset($params['category']))?$params['category']:'';
    $keyword = (isset($params['keyword']))?$params['keyword']:'';
    $status = (!isset($params['status']) || $params['status'] == '')?'':$params['status'];
    $order_by = (isset($params['order_by']))?$params['order_by']:'created_on';
    $direction = (isset($params['direction']))?$params['direction']:'DESC';
    $limit = (isset($params['limit']))?$params['limit']:'';
    $offset = (isset($params['offset']))?$params['offset']:'';
    $st = '';
    if($status == ''){
        $st = '!';
    }

        if(!empty($category))
        {
            $conditions = array(
                "((blogcats.id = ?) OR (blogcats.parent_id = ?))
                 AND blogs.status $st= ?
                 AND (blogs.title LIKE '%$keyword%' OR blogs.content LIKE '%$keyword%')",$category,$category,$status
            );

        }else{
            $conditions = array(
                "blogs.status $st= ?
                 AND (blogs.title LIKE '%$keyword%' OR blogs.content LIKE '%$keyword%')",$status
            );
        }

    $result = Blog::find('all',array(
        'include'       => array('blogcat','blogref','user'),
        'joins'         => $joins,
        'conditions'    => $conditions,
        'limit'         => $limit,
        'offset'        => $offset,
        'order'         => "$order_by $direction",
        'group'         => 'blogs.id'
    ));

    $count =  Blog::find('all',array(
        'conditions'    => $conditions
    ));

    $object = new stdClass;
    $object->posts = $result;
    $object->total = count($count);
    return $object;

}

一切正常,不仅我使用

((blogcats.id = ?) OR (blogcats.parent_id = ?))

获取特定类别的博客。任何答案将不胜感激。

【问题讨论】:

  • 如果您使用的是phpactiverrecord,难道没有更快的方法来获取所有这些相关对象吗?喜欢使用关联?

标签: php mysql codeigniter phpactiverecord


【解决方案1】:

我觉得这不是很清楚。为什么不使用模型和 CI 的 Active Record 类? (http://ellislab.com/codeigniter/user-guide/database/active_record.html),喜欢

    $this->db->where('blogrefs.blogcat_id', $catid);
    $this->db->join('blogrefs', 'blogs.id = blogrefs.blog_id');
    ...

【讨论】:

    【解决方案2】:

    您的错误是在您定义连接的第一部分的末尾缺少空格。

    $joins  = 'LEFT JOIN blogrefs ON blogs.id = blogrefs.blog_id';
    $joins  .= 'LEFT JOIN blogcats ON blogrefs.blogcat_id = blogcats.id';
    

    评估为:

    LEFT JOIN blogrefs ON blogs.id = blogrefs.blog_idLEFT JOIN blogcats ON blogrefs.blogcat_id = blogcats.id
    

    您会注意到第一个联接因查找名为“blog_idLEFT”的列而搞砸了,而第二个联接现在将是内部联接,而不是外部左联接。

    我建议要么采用 davedriesmans 的建议,要么找到某种方式来输出您生成的最终 mySQL 查询。 (或两者兼有)

    【讨论】:

      猜你喜欢
      • 2019-04-23
      • 2020-08-02
      • 2019-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-27
      • 1970-01-01
      相关资源
      最近更新 更多