Zend Framework Sql 找不到列
Zend Framework Sql not finding column
我遇到 Zend Framework 2 的 SQL class 问题。它说它无法在 table 中找到特定的列,但该列在那里。我之前 运行 遇到过这个问题,不得不使用实际的 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 列存在于朋友 table 我已经到位:
所以我的问题是它为什么要这样做以及我将来如何避免这个问题?
谢谢!
问题似乎是 select 引起的。
由于您的代码 ->from('friends')
首先被调用,然后由于此函数调用 $this->getUserId()
而被覆盖,它将 friends
table 覆盖为 members
由于 ->from('members')
。
尝试将您的代码更改为。
$userId = $this->getUserId()['id'];
$friend_query = $this->select->columns(array('*'))
->from('friends')
->where(array('user_id' => $userId));
这应该有效,但如果无效,请尝试在两个函数中创建新的 select 对象 $select = new Select();
而不是 $this->select = new Select();
.
我遇到 Zend Framework 2 的 SQL class 问题。它说它无法在 table 中找到特定的列,但该列在那里。我之前 运行 遇到过这个问题,不得不使用实际的 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 列存在于朋友 table 我已经到位:
所以我的问题是它为什么要这样做以及我将来如何避免这个问题?
谢谢!
问题似乎是 select 引起的。
由于您的代码 ->from('friends')
首先被调用,然后由于此函数调用 $this->getUserId()
而被覆盖,它将 friends
table 覆盖为 members
由于 ->from('members')
。
尝试将您的代码更改为。
$userId = $this->getUserId()['id'];
$friend_query = $this->select->columns(array('*'))
->from('friends')
->where(array('user_id' => $userId));
这应该有效,但如果无效,请尝试在两个函数中创建新的 select 对象 $select = new Select();
而不是 $this->select = new Select();
.