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 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程