Php 对zend_db_表对象使用表别名

Php 对zend_db_表对象使用表别名,php,zend-framework,zend-db,zend-db-table,Php,Zend Framework,Zend Db,Zend Db Table,假设我有一张桌子: $someTable = new Zend_Db_Table ('sometable'); 我知道我可以使用表的文字名称构建查询 $sel = $someTable -> select () -> from ('sometable', array ('col_foo', 'col_bar')) -> where ('some_condition') 我可以给这个表加上别名 $se

假设我有一张桌子:

$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_表:

但是,当我尝试像下面这样对表对象使用别名时,我得到了一个致命的错误

$sel = $someTable -> select () 
                  -> from (array ('alias' => $someTable), array ('col_foo', 'col_bar')) 
                  -> where ('some_condition')
可捕获的致命错误:Zend_Db_表类的对象无法转换为

在我看来,这是一种错误行为,因为from()、join()等方法可以处理将Zend_Db_表对象传递给它们的问题,但当您想对其进行别名时就不行了

上面的例子有点做作和简化来说明这个问题。真正的代码是在表之间进行连接,但是随着表对象的传入,我不知道它们的名称。当然,我可以使用info()来解决上述问题,以获取表名并将它们作为字符串注入,但这意味着额外的代码在我看来很混乱。此外,Zend_Db应该能够在没有此类解决办法的情况下应对这种情况


我正在使用Zend Framework 1.7.6。我将Zend_Db_Table_抽象子类化,以生成我的表对象。不幸的是,我没有权限安装Zend的升级版本

升级版的ZF不会有什么帮助,但您应该为当前版本的ZF和PHP的安全角度进行争论(尝试永远不会有坏处;):

alias希望成为字符串而不是对象,
$someTable
当前是对象。我想你需要一些东西,比如
$someTable->getConfig()->name应该做你想做的事

摘自
from()
Zend\u Db\u表\u摘要

第一个参数也可以是
Zend\u Db\u Select()
的实例,但不能将对象与数组混合

考虑到这一点,您可以编写一个简单的实体类,它允许您为任何表名构建Zend_Db_表的实例

class Application_Model_DbTable extends Zend_Db_Table_Abstract
{
    protected $_name;
    protected $_primary;

    public function __construct($name, $primary = 'id') {
        $this->_name = $name;
        $this->_primary = 'something_other_then_id';

    }

    //implement __set and __get if you want

}
这将允许您只传入表名和该表的主id。您仍然可以使用整个Zend_Db_表api以及您设计的任何自定义方法


祝你好运。

我不认为使用(array('alias'=>$someTable->info('name'))
中的
->会有多混乱。您的代码看起来很好。@Salman谢谢。:)我喜欢保持东西整洁。因此,如果能够消除临时变量和->info()调用,我就可以删除几行并使其更整洁:)此外,我还需要在实际代码中使用其他一些表属性,包括schema。从外观上看,它们的疏忽令人恼火。这确实令人讨厌。
$sel = $someTable -> select () 
                  -> from (array ('alias' => $someTable), array ('col_foo', 'col_bar')) 
                  -> where ('some_condition')
/**
 * 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.
 */
class Application_Model_DbTable extends Zend_Db_Table_Abstract
{
    protected $_name;
    protected $_primary;

    public function __construct($name, $primary = 'id') {
        $this->_name = $name;
        $this->_primary = 'something_other_then_id';

    }

    //implement __set and __get if you want

}