翻译查询以使用 Zend_Db_Select [英] Translating a query to use Zend_Db_Select
问题描述
我在翻译此查询以使用 ZF 的 Zend_Db_Select
时遇到一些问题:
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
是blogs
和users
之间的链接表.这是一个简单的 |身份证 |blog_id |user_id |
事情.
Memberships
is a link table between blogs
and users
. It's a simple | id | blog_id | user_id |
affair.
这是我目前所拥有的:
// $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: 选择查询不能加入另一个表
发生在 D:\...\library\Zend\Db\Table\Select.php
的第 211 行.
Occurred on line 211 of D:\...\library\Zend\Db\Table\Select.php
.
感谢您的帮助.
推荐答案
当从您的表对象中检索时,我认为该语句将仅限于该表.Zend_Db_Table::select()
方法返回一个 Zend_Db_Table_Select
对象,它是 Zend_Db_Select
的子类,并强加了这个限制.试试这个:
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( ...
这篇关于翻译查询以使用 Zend_Db_Select的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!