Skip to content

How do you work with complex documents returned as arrays? #78

Closed
@crazycodr

Description

@crazycodr

I'm trying out your library with a simple model that contains arrays of values such as:

public function __construct()
{
    $this['night'] = null;
    $this['received'] = array(
        'date' => date('Y-m-d G:i'),
        'by' => null
    );
    $this['client'] = array(
        'name' => null,
        'givenname' => null
    );
}

Which works fine in itself, if i can place values in the object, i'm able to save them correctly to Mongodb and it looks something like the following and it's perfect.

{
    "_id" : ObjectId("529f905b45f3b0203f90b053"),
    "night" : "529f8a6645f3b0293f979d28",
    "received" : {
        "date" : "2013-12-04 21:22",
        "by" : "Mathieu Dumoulin"
    },
    "client" : {
        "name" : "Champoux",
        "givenname" : "Valérie"
    }
}

But the problem is, when i comes back, i have arrays instead of objects. This forces me to redraw the code to use arrays instead of objects and the resulting issue is i keep getting "Indirect modification of overloaded element of Transport has no effect" exceptions because i'm trying to change a value of a returned array with is not a reference type, so the assign just ends up in the void and doesn't change anything.

Used to be and worked fine in the beginning:

$transport = new Transport();
$transport->received->date = Input::get('received_date');
$transport->received->by = Input::get('received_by');
$transport->client->name = Input::get('client_name');
$transport->client->givenname = Input::get('client_givenname');

Currently needs to be because mongo returns arrays in eloquent model:

$transport = new Transport();
$transport['received']['date'] = Input::get('received_date');
$transport['received']['by'] = Input::get('received_by');
$transport['client']['name'] = Input::get('client_name');
$transport['client']['givenname'] = Input::get('client_givenname');

Needs to be if i want to not get the exception:

$transport = new Transport();
$transport['received'] = array(
    'date' => Input::get('received_date'),
    'by' => Input::get('received_by'),
);
$transport['client'] = array(
    'name' => Input::get('client_name'),
    'givenname' => Input::get('client_givenname'),
);

I hope you agree that this last code i pasted smells a lot, it's ugly, hard to manage and hard to upgrade in the future. I'm used to doing my own repository classes and hydrators, but i want to take advantage of Eloquent in this project even though i'm using MongoDB.

So my question is, what is your approach to this problem?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions