数据库查询未正确检索

时间:2013-11-22 04:58:21

标签: php mysql zend-framework

我正在使用zend框架和我的sql来显示一些数据。

查询如下:

    $table = new Timesheets_Table_Tasks();
    $select = $table->select(true)
                ->setIntegrityCheck(false)
                ->join('timesheets_projects', 'timesheets_projects.ID=timesheets_tasks.ProjectID', array())
                ->join('timesheets_clients', 'timesheets_clients.ID=timesheets_projects.ClientID', array())


    return $this->filterSelect($select);

函数filterSelect如下:

protected function filterSelect($select)
    {
        $select->where("timesheets_tasks.UserID=?", Zend_Auth::getInstance()->getIdentity()->id);
        foreach($this->_filters as $filter => $value)
        {
            if(($value == "" || $value == "none") && $filter != "status")
            {
                continue;
            }

            switch($filter)
            {
                case 'q':
                    $select->where($this->_db->quoteInto('timesheets_tasks.Comment LIKE ?', '%' . $value . '%'));
                    break;
                case 'after':
                    $select->where($this->_db->quoteInto('timesheets_tasks.Date >= UNIX_TIMESTAMP(STR_TO_DATE(?, "%d/%m/%Y"))', $value));
                    break;
                case 'before':
                    $select->where($this->_db->quoteInto('timesheets_tasks.Date <= UNIX_TIMESTAMP(STR_TO_DATE(?, "%d/%m/%Y"))', $value));
                    break;
                case 'client':
                    $select->where($this->_db->quoteInto('timesheets_clients.ID = ?', $value));
                    break;
                case 'project':
                    $select->where($this->_db->quoteInto('timesheets_tasks.ProjectID = ?', $value));
                    break;
                case 'status':
                    $select->where($this->_db->quoteInto('timesheets_tasks.Status = ?', $value));
                    break;
                case 'jobtype':
                    $select->where($this->_db->quoteInto('timesheets_tasks.TypeID = ?', $value));
                    break;
            }
        }

        return $select;
    }

我显示了查询,它显示为:

SELECT timesheets_tasks。* FROM timesheets_tasks INNER JOIN timesheets_projects ON timesheets_projects.ID = timesheets_tasks.ProjectID INNER JOIN timesheets_clients ON timesheets_clients.ID = timesheets_projects.ClientID WHERE(timesheets_tasks.UserID ='33')AND(timesheets_tasks.Status = 0)ORDER BY date DESC

但是查询没有提供实际数据。

我认为这是连接表的问题。当我从查询中删除“ - &gt; join('timesheets_clients','timesheets_clients.ID = timesheets_projects.ClientID',array())”行时,输出显示正确。但我必须从其他两个表中选择一些字段。所以我需要一个连接属性。我可以使用任何其他连接从其他两个表中获取列值并获得正确的输出吗?

我是新手加入的。任何帮助都应该受到赞赏!

1 个答案:

答案 0 :(得分:1)

可以帮助您解决问题

  $table = new Timesheets_Table_Tasks();
    $select = $table->select(true)
                ->setIntegrityCheck(false)
                ->joinLeft('timesheets_projects', 'timesheets_projects.ID=timesheets_tasks.ProjectID', array())
                ->joinLeft('timesheets_clients', 'timesheets_clients.ID=timesheets_projects.ClientID', array())


    return $this->filterSelect($select)

另请参阅echo您的select声明并加入

希望这对你有用。