문제

I'm using PHP with php active record. When i retrieve a record from the database the attributes are listed as private. I need to iterate over the attributes and retrieve the key => value pair. How can this be done?

$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] => 
)
도움이 되었습니까?

해결책

Just use the model's method attributes: $row->attributes()

다른 팁

If you want to loop over everything then youll need to implment the Iterator interface.

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

You can not, unless you have a public accessible getter function that allows you to get the array.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top