dongtan7351 2012-11-13 09:29
浏览 65
已采纳

使用带有zend_db_table对象的表别名

Suppose I have a table:

$someTable = new Zend_Db_Table ('sometable');

I know I can build a query using the table's literal name.

$sel = $someTable -> select () 
                  -> from ('sometable', array ('col_foo', 'col_bar')) 
                  -> where ('some_condition')

and that I can alias the table.

$sel = $someTable -> select () 
                  -> from (array ('alias' => 'sometable'), array ('col_foo', 'col_bar')) 
                  -> where ('some_condition')

I also know I can use the Zend_Db_Table directly in the from() call:

$sel = $someTable -> select () 
                  -> from ($someTable, array ('col_foo', 'col_bar')) 
                  -> where ('some_condition')

but when I try aliasing the table object like below, I get a fatal error.

$sel = $someTable -> select () 
                  -> from (array ('alias' => $someTable), array ('col_foo', 'col_bar')) 
                  -> where ('some_condition')

Catchable fatal error: Object of class Zend_Db_Table could not be converted to string in ...

It seems to me that this is buggy behavior because the from(), join(), etc methods can handle having a Zend_Db_Table object passed to them, but not when you want to alias it!

The example above is a little contrived and simplified to illustrate the problem. The real code is doing joins between tables but as the table objects are being passed in I don't know what their names are ahead of time. Of course I can work around the above problem using info() to get the table names and inject them as strings, but this means extra code which to me looks messy. Besides, this is a circumstance that Zend_Db should be able to cope with without such workarounds.

I'm using Zend Framework 1.7.6. and am subclassing Zend_Db_Table_Abstract to make my table objects. Unfortunately I don't have the authority to get an upgraded version of Zend installed.

  • 写回答

1条回答 默认 最新

  • dongyi1159 2012-11-14 11:06
    关注

    an upgraded version of ZF won't help, but you should argue the security angle for current versions of ZF and PHP (never hurts to try ;) ):

    $sel = $someTable -> select () 
                      -> from (array ('alias' => $someTable), array ('col_foo', 'col_bar')) 
                      -> where ('some_condition')
    

    alias wants to be a string not an object and $someTable is currently an object. I think you want some thing like $someTable->getConfig()->name; should do what you want.

    excerpt from Zend_Db_Table_Abstract for 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.
     */
    

    the first parameter can also be an instance ofZend_Db_Select() but you can't mix the object with the array.

    Think on this, you could write a simple entity class that would allow you to built an instance of Zend_Db_Table for any table name.

    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
    
    }
    

    This would allow you to just pass in the table name and the primary id of that table. You would still have the whole Zend_Db_Table api at your disposal as well as any custom methods you design.

    Good Luck.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址