对于我的应用程序,我的消息模型中有一个方法,它返回当前用户($id)发送的所有消息。它使用 3 个连接从 3 个表返回信息。由于发件人和收件人都来自同一个“用户”表,因此我必须为这些列设置别名。
我的消息模型中的方法:
public function getSentMessages($id)
{
$messageModel = new self();
$select = $messageModel->select()
->from(array('m' => 'messages'),
array('m.id', 'm.title', 'm.send',
's.id AS s_id', 's.firstname AS s_fn', 's.lastname AS s_ln', 's.email AS s_e',
'r.id AS r_id', 'r.firstname AS r_fn', 'r.lastname AS r_ln', 'r.email AS r_e'))
->join(array('s' => 'users'),
'm.from = s.id')
->join(array('mu' => 'messages_users'),
'm.id = mu.message_id')
->join(array('r' => 'users'),
'r.id = mu.user_id')
->where('m.from = ?', $id);
$select->setIntegrityCheck(false);
$messages = $messageModel->fetchAll($select);
return $messages;
}
发生了奇怪的事情:结果行是由我请求的所有列组成的......但不知何故,所有列(包括我没有请求的列)都在之后添加。像这样:
id title send s_id s_firstname s_lastname s_email r_id r_firstname r_lastname r_email id username password firstname lastname email role id message_id user_id status id username password firstname lastname email role
我发现 Zend 框架返回的 SQL 查询不是我期望的那样。这里是(为了更好的可读性而进行了清理):
SELECT m.id, m.title, m.send,
s.id AS s_id, s.firstname AS s_fn, s.lastname AS s_ln, s.email AS s_e,
r.id AS r_id, r.firstname AS r_fn, r.lastname AS r_ln, r.email AS r_e,
s. * , mu. * , r. *
FROM messages AS m
INNER JOIN users AS s ON m.from = s.id
INNER JOIN messages_users AS mu ON m.id = mu.message_id
INNER JOIN users AS r ON r.id = mu.user_id
WHERE (
m.from = '12345'
)
在我的 SELECT 语句末尾检查这些星号 (*)。这些是如何到达那里的?Zend 文档告诉我只有在没有给出列时才使用星号。
谢谢你的帮助!