gpt4 book ai didi

php - Zend Framework select() 给出意外的 SQL

转载 作者:行者123 更新时间:2023-11-29 08:50:31 25 4
gpt4 key购买 nike

对于我的应用程序,我的消息模型中有一个方法,它返回当前用户 ($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 Framework 返回的 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 文档告诉我星号仅在未给出列时使用。

感谢您的帮助!

最佳答案

Join 具有三个参数:

在每个 Join 子句末尾添加一个空白值..

join(array('s' => 'users'),'m.from = s.id','')

关于php - Zend Framework select() 给出意外的 SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11375302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com