使用私有属性迭代对象

时间:2012-10-29 17:34:25

标签: php phpactiverecord

我正在使用带有php活动记录的PHP。当我从数据库中检索记录时,属性被列为私有。我需要迭代属性并检索key =>价值对。如何才能做到这一点?

$row = \Models\Locations::find(2);

Models\Locations Object
(
    [errors] => 
    [attributes:ActiveRecord\Model:private] => Array
        (
            [id] => 2
            [customer_id] => 6
            [name] => test location
            [address_line1] => 123 test Drive
            [address_line2] => 
            [city] => Moon Township
            [state] => AZ
            [zip] => 12345
            [country] => USA
            [primary_phone_number] => 123.456.7890
            [latitude] => 0
            [longitude] => 0
            [coordinate_precision] => 
        )

    [__dirty:ActiveRecord\Model:private] => Array
        (
        )

    [__readonly:ActiveRecord\Model:private] => 
    [__relationships:ActiveRecord\Model:private] => Array
        (
        )

    [__new_record:ActiveRecord\Model:private] => 
)

3 个答案:

答案 0 :(得分:2)

只需使用模型的方法attributes$row->attributes()

答案 1 :(得分:1)

如果你想循环所有内容,那么你需要实现Iterator接口。

class ModelExtended extends Model implements Iterator
{
   // implement the Iterator interface within - then extend this class instead of Model 
}

答案 2 :(得分:1)

你不能,除非你有一个公共可访问的getter函数,允许你获取数组。