假设我有一张桌子:
$someTable = new Zend_Db_Table ('sometable');
我知道我可以使用表的文字名来构建查询。
$sel = $someTable -> select ()
-> from ('sometable', array ('col_foo', 'col_bar'))
-> where ('some_condition')
我可以为表格添加别名。
$sel = $someTable -> select ()
-> from (array ('alias' => 'sometable'), array ('col_foo', 'col_bar'))
-> where ('some_condition')
我也知道我可以直接在from()调用中使用Zend_Db_Table:
$sel = $someTable -> select ()
-> from ($someTable, array ('col_foo', 'col_bar'))
-> where ('some_condition')
但是当我尝试像下面那样对表对象进行别名时,我会收到一个致命的错误。
$sel = $someTable -> select ()
-> from (array ('alias' => $someTable), array ('col_foo', 'col_bar'))
-> where ('some_condition')
捕获致命错误:Zend_Db_Table类的对象无法转换为字符串...
在我看来,这是有缺陷的行为,因为from(),join()等方法可以处理传递给它们的Zend_Db_Table对象,但是当你想要别名时它不会!
上面的例子有点人为和简化,以说明问题。真正的代码是在表之间进行连接,但是当表对象被传入时,我不知道它们的名字是提前的。当然我可以解决上面的问题,使用info()来获取表名并将它们作为字符串注入,但这意味着额外的代码对我来说看起来很混乱。此外,这是Zend_Db应该能够在没有这些变通方法的情况下应对的情况。
我正在使用Zend Framework 1.7.6。并且我将Zend_Db_Table_Abstract子类化以生成我的表对象。不幸的是,我无权安装Zend的升级版本。
答案 0 :(得分:2)
ZF的升级版本无济于事,但您应该争论当前版本的ZF和PHP的安全角度(从不伤害尝试;)):
$sel = $someTable -> select ()
-> from (array ('alias' => $someTable), array ('col_foo', 'col_bar'))
-> where ('some_condition')
别名想要成为字符串而不是对象,而$someTable
当前是对象。我想你想要像$someTable->getConfig()->name;
这样的东西做你想做的事。
摘自Zend_Db_Table_Abstract
的{{1}}:
from()
第一个参数也可以是/**
* Adds a FROM table and optional columns to the query.
*
* The first parameter $name can be a simple string, in which case the
* correlation name is generated automatically. If you want to specify
* the correlation name, the first parameter must be an associative
* array in which the key is the correlation name, and the value is
* the physical table name. For example, array('alias' => 'table').
* The correlation name is prepended to all columns fetched for this
* table.
*/
的实例,但不能将对象与数组混合。
想想看,您可以编写一个简单的实体类,允许您为任何表名构建Zend_Db_Table实例。
Zend_Db_Select()
这将允许您只传入表名和该表的主ID。您仍然可以使用整个Zend_Db_Table api以及您设计的任何自定义方法。
祝你好运。