例如,我用框架的Model执行了一下mysql 数据库查询之后,返回结果为:
Array
(
[0] => app\common\model\walmart\WalmartListing Object
(
[query:protected] => erp\ErpQuery
[virtualFields:protected] => Array
(
)
[connection:protected] => Array
(
)
[name:protected] => WalmartListing
[table:protected] =>
[class:protected] => app\common\model\walmart\WalmartListing
[error:protected] =>
[validate:protected] =>
[pk:protected] =>
[field:protected] => Array
(
)
[readonly:protected] => Array
(
)
[visible:protected] => Array
(
)
[hidden:protected] => Array
(
)
[append:protected] => Array
(
)
[data:protected] => Array
(
[id] => 11
[account_id] => 37
[vid] => 15
[sku_id] => 100529
[variation_sku] => LA0Q3C9FBN99MF2601
[price] => 23121.00
[inventory] => 5
)
[change:protected] => Array
(
)
[auto:protected] => Array
(
)
[insert:protected] => Array
(
)
[update:protected] => Array
(
)
[autoWriteTimestamp:protected] =>
[createTime:protected] => create_time
[updateTime:protected] => update_time
[dateFormat:protected] =>
[type:protected] => Array
(
)
[isUpdate:protected] => 1
[updateWhere:protected] =>
[relation:protected] =>
[failException:protected] =>
[useGlobalScope:protected] => 1
[batchValidate:protected] =>
[resultSetType:protected] => array
[relationWrite:protected] =>
[filterClass:protected] =>
)
)
为什么不是返回简简单单的数组呢?用惯了旧框架,和原生,早就习惯了用数组处理。
这个把结果转成对象的形式返回,有什么好处吗?目的是啥?
求详细解答下,谢谢啦
返回对象是因为他们里面基本都是封装的类,有时候需要这些结果这些类和这些类中的方法,去再次的进行一些扩展,数组会方便读取,但是不方便扩展。
返回对象后,你可以继续在这个对象上加一些其它的链式操作,比如create,update,delete,如果想要数组,可以用toArray就能得到你想要的数组了,其实是更方便了的