Search code examples
phpphpactiverecord

Iterate over object with private attributes


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

Solution

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