将查询转换为使用 Zend_Db_Select

发布于 2024-07-12 13:42:47 字数 931 浏览 5 评论 0原文

我在将此查询转换为使用 ZF 的 Zend_Db_Select 时遇到一些问题:(

SELECT b.id, b.title, b.description 
FROM memberships AS m
JOIN blogs AS b ON b.id = m.blog_id 
WHERE m.user_id = ? 
ORDER BY m.created
LIMIT 0, 30

此查询有效并返回结果)

Memberships博客 之间的链接表和用户。 这是一个简单的 | 编号 | 博客 ID | user_id | 事务。

这是我到目前为止所得到的:

// $table = Zend_Db_Table instance, $id = a user id
$select = $table->select()
->from(array('m' => 'memberships'), array('b.id', 'b.title', 'b.description'))
->join(array('b' => 'blogs'), 'b.id = m.blog_id')
->where('m.user_id = ?', (int) $id)
->order('m.created DESC')
->limit(0, 30);

这是我收到的(对我来说很奇怪)错误:

#0:选择查询无法与另一个表连接

发生在 D:\...\library\Zend\Db\Table\Select.php 的第 211 行。

感谢您的帮助。

I'm having some problems translating this query to use ZF's Zend_Db_Select:

SELECT b.id, b.title, b.description 
FROM memberships AS m
JOIN blogs AS b ON b.id = m.blog_id 
WHERE m.user_id = ? 
ORDER BY m.created
LIMIT 0, 30

(this query works and returns results)

Memberships is a link table between blogs and users. It's a simple | id | blog_id | user_id | affair.

Here's what I have so far:

// $table = Zend_Db_Table instance, $id = a user id
$select = $table->select()
->from(array('m' => 'memberships'), array('b.id', 'b.title', 'b.description'))
->join(array('b' => 'blogs'), 'b.id = m.blog_id')
->where('m.user_id = ?', (int) $id)
->order('m.created DESC')
->limit(0, 30);

This is the (strange (to me)) error I'm getting:

#0: Select query cannot join with another table

Occurred on line 211 of D:\...\library\Zend\Db\Table\Select.php.

Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七禾 2024-07-19 13:42:47

您还可以通过添加 setIntegrityCheck(false) 来使用传统的 $model->select() 对象,如下所示。

$select = $table->select()
->setIntegrityCheck(false)
->from(array('m' => 'memberships'), array('b.id', 'b.title', 'b.description'))
->join(array('b' => 'blogs'), 'b.id = m.blog_id')
->where('m.user_id = ?', (int) $id)
->order('m.created DESC')
->limit(0, 30);

这将禁用引发异常的检查:

#0: Select query cannot join with another table

You could also still use the traditional $model->select() object by adding setIntegrityCheck(false), like so.

$select = $table->select()
->setIntegrityCheck(false)
->from(array('m' => 'memberships'), array('b.id', 'b.title', 'b.description'))
->join(array('b' => 'blogs'), 'b.id = m.blog_id')
->where('m.user_id = ?', (int) $id)
->order('m.created DESC')
->limit(0, 30);

This disables the check that is throwing the exception:

#0: Select query cannot join with another table
遥远的她 2024-07-19 13:42:47

当从表对象中检索时,该语句将仅限于我认为的该表。 Zend_Db_Table::select() 方法返回一个 Zend_Db_Table_Select 对象,该对象是 Zend_Db_Select 的子类并施加此限制。 试试这个:

$db = Zend_Db::factory( ...options... );
$select = new Zend_Db_Select($adapter);
$select->from( 'my_table_name' )->join( ...

如果您愿意,以下内容应该是等效的:

$db = Zend_Db::factory( ...options... );
$db->select()->from( 'my_table_name' )->join( ...

When retrieved from your table object, the statement will be limited to that table I think. The Zend_Db_Table::select() methods returns a Zend_Db_Table_Select object which is a subclass of Zend_Db_Select and imposes this restriction. Try this instead:

$db = Zend_Db::factory( ...options... );
$select = new Zend_Db_Select($adapter);
$select->from( 'my_table_name' )->join( ...

If you prefer, the following should be equivalent:

$db = Zend_Db::factory( ...options... );
$db->select()->from( 'my_table_name' )->join( ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文