Zend Framework Sql 找不到列

问题描述 投票:0回答:1

我在使用 Zend Framework 2 的 SQL 类时遇到问题。它说它无法在表中找到特定列,但该列在那里。我以前遇到过这个问题,不得不使用实际的 SQL 语法而不是构建查询。我宁愿不必求助于实际的语法,而是弄清楚为什么会发生这种情况,这样我就可以避免将来遇到这个问题。

这是我的代码:

namespace Members\Model;

use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Sql;
use Zend\Db\Sql\Select;
use Zend\Db\Adapter\Adapter;
use Zend\Db\Sql\Predicate;

use Members\Model\Interfaces\FeedInterface;
use Members\Model\Exceptions\FeedException;



class FeedModel implements FeedInterface
{
    /**
     * @var TableGateway
     */
    public $gateway;

    /**
     * @var string
     */
    public $user;

    /**
     * @var Sql
     */
    public $sql;

    /**
     * @var Select
     */
    public $select;


    /**
     * Constructor method for FeedModel class
     * @param TableGateway $gateway
     * @param string $user
     */
    public function __construct(TableGateway $gateway, $user)
    {
        $this->gateway = $gateway instanceof TableGateway ? $gateway : null;

        $this->select = new Select();

        $this->sql = new Sql($this->gateway->getAdapter());

        $this->user = $user;
    }


    /**
     * {@inheritDoc}
     * @see \Members\Model\Interfaces\FeedInterface::listFriendsStatus()
     */
    public function listFriendsStatus()
    {
        // get the friend ids based on user id
        // and then compare the friend id to the id in status table
        $friend_query = $this->select->columns(array('*'))
        ->from('friends')
        ->where(array('user_id' => $this->getUserId()['id']));  // the issue

        file_put_contents(getcwd() . '/public/query.txt', $this->sql->buildSqlString($friend_query));

        $query = $this->sql->getAdapter()->query(
            $this->sql->buildSqlString($friend_query),
            Adapter::QUERY_MODE_EXECUTE
        );

        if ($query->count() > 0) {
            $friend_id = array();

            foreach ($query as $result) {
                $friend_id[] = $result;
            }

            $this->select->columns(array('status'))
            ->from('status')
            ->where(array('id' => array_values($friend_id), new Predicate\IsNotNull('id')));

            $status_query = $this->sql->getAdapter()->query(
                $this->sql->buildSqlString($this->select),
                Adapter::QUERY_MODE_EXECUTE
            );

            if ($status_query->count() > 0) {
                // check if a image was used
                $this->select->columns('username')
                ->from('members')
                ->where(array('id' => array_values($friend_id), new Predicate\IsNotNull('id')));

                $image_query = $this->sql->getAdapter()->query(
                    $this->sql->buildSqlString($this->select),
                    Adapter::QUERY_MODE_EXECUTE
                );

                if ($image_query->count() > 0) {
                    $status_dir = array();

                    foreach ($image_query as $value) {
                        if (@!is_dir(getcwd() . '/public/images/profile/' . $value['username'] . '/status/')) {
                            continue; 
                        }

                        $status_dir[] = getcwd() . '/public/images/profile/' . $value['username'] . '/status/';
                    }

                    $images = array();

                    // retrieve the image inside the status directory
                    foreach (array_diff(scandir($status_dir, 1), array('.', '..')) as $values) {
                        $images[] = $values;    
                    }
                } else {
                    throw new FeedException("The user does not exist in the user table.");
                }

                $status = array();

                // get all the statuses
                foreach ($status_query as $rows) {
                    $status[] = $rows;    
                }

                return array('status' => $status, 'images' => $images);
            } else {
                throw new FeedException("No status was found for your friends.");
            }
         } else {
            throw new FeedException(sprintf("Could not locate any friends for %s", $this->user));
         }
    }


    /**
     * {@inheritDoc}
     * @see \Members\Model\Interfaces\FeedInterface::hideFriendsStatus()
     */
    public function hideFriendsStatus($friend_id)
    {

    }



    /**
     * Grabs the user id for the user
     * 
     * @return int|boolean
     */
    public function getUserId()
    {
        $this->select->columns(array('*'))
        ->from('members')
        ->where(array('username' => $this->user));

        $query = $this->sql->getAdapter()->query(
            $this->sql->buildSqlString($this->select),
            Adapter::QUERY_MODE_EXECUTE
        );

        if ($query->count() > 0) {

            foreach ($query as $result) {
                $row = $result;
            }

            return $row;
        }

        return false;
    }
}

这是我收到的异常消息:

PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user_id' in 'where clause' 

但是正如您在此屏幕截图中看到的,user_id 列存在于我现有的好友表中:

所以我的问题是为什么要这样做以及我将来如何避免这个问题?

谢谢!

php zend-framework2
1个回答
1
投票

看起来是选择导致了问题。

由于您的代码首先调用

->from('friends')
,然后由于此函数调用
$this->getUserId()
而将其覆盖,由于
friends
,它将
members
表覆盖为
->from('members')

尝试将您的代码更改为。

$userId = $this->getUserId()['id'];
$friend_query = $this->select->columns(array('*'))
        ->from('friends')
        ->where(array('user_id' => $userId));

这应该可行,但如果不行,请尝试在两个函数中创建新的选择对象

$select = new Select();
,而不是
$this->select = new Select();

© www.soinside.com 2019 - 2024. All rights reserved.