简体   繁体   English

PHP Zend Framework:使用Zend_Db_Select返回对象

[英]Php Zend Framework : Using Zend_Db_Select to return objects

Currently right now I'am using Zend_DB_Select, I would like to return the rows as objects so that I can use methods like save(), delete() 目前,我正在使用Zend_DB_Select,我想将这些行作为对象返回,以便可以使用save(),delete()之类的方法

Function includes: 功能包括:

    $table = self::instance();
    $select = $table->getAdapter()->select();
    $select->from('table1');
    if($where != '')
    {
        $select->where($where);
    }
    $select->limit($count);
    $select->order('id DESC');

    $rs = $select->query()->fetchAll();

So right now Iam passing an array instead of object types. 因此,现在Iam传递数组而不是对象类型。

Try this: 尝试这个:

$rs = $select->query()->fetchAll(Zend_Db::FETCH_OBJ);

With Zend_Db::FETCH_OBJ , zend will return objects. 使用Zend_Db::FETCH_OBJ ,zend将返回对象。 It's all about fetch modes . 一切都与获取模式有关

If you want to operate on your resulting rows as objects (to call save(), delete() on them and so on) you need to use Zend_Db_Table_* and not just Zend_Db_* for your requests. 如果您想将结果行作为对象进行操作(以调用save(),delete()等),则需要使用Zend_Db_Table_ *而不是Zend_Db_ *进行请求。

That way your resultset will be Zend_Db_Table_Row objects (instead of arrays or stdClass objects) and those objects have methods such as save() and delete() that you can call to manipulate and update individual rows in your code. 这样,结果集将是Zend_Db_Table_Row对象(而不是数组或stdClass对象),并且这些对象具有诸如save()和delete()之类的方法,可以调用这些方法来操纵和更新代码中的各个行。

Start reading here: 在这里开始阅读:

http://framework.zend.com/manual/en/zend.db.table.html http://framework.zend.com/manual/en/zend.db.table.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM