4

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

$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] => 
)
4

3 回答 3

2

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

于 2012-10-29T23:41:34.167 回答
1

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

class ModelExtended extends Model implements Iterator
{
   // implement the Iterator interface within - then extend this class instead of Model 
}
于 2012-10-29T17:41:57.673 回答
1

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

于 2012-10-29T18:39:40.830 回答