Welcome Guest, Not a member yet? Register   Sign In
DMZ 1.7.1 (DataMapper OverZealous Edition)

[eluser]TheJim[/eluser]
[quote author="GregX999" date="1274410685"]Hey all, hopefully a quick question...

Is there a way to access specific objects in the collection? Specifically, the first one. I can do a "foreach" on $thing, but I can't do $thing[0].

Greg[/quote]

Accessing the first item is easy, because the object itself takes on those field values, e.g. $thing->name is set (assuming a record was found, of course -- see $thing->exists())

Accessing an arbitrary item in the collection can be done using $thing->all, which is an array. The index depends on your configuration. If your configuration has

Code:
$config['all_array_uses_ids'] = TRUE;

then objects are indexed in that array by their ID. If FALSE, $thing->all is a standard array indexed starting with 0.

The foreach works as you've described because DataMapper objects are iterators, but you can't directly index them ($thing[0], as in your example) because they are not arrays.

[eluser]GregX999[/eluser]
[quote author="TheJim" date="1274413036"]
Accessing the first item is easy, because the object itself takes on those field values, e.g. $thing->name is set (assuming a record was found, of course -- see $thing->exists())

Accessing an arbitrary item in the collection can be done using $thing->all, which is an array. The index depends on your configuration. If your configuration has

Code:
$config['all_array_uses_ids'] = TRUE;

then objects are indexed in that array by their ID. If FALSE, $thing->all is a standard array indexed starting with 0.
[/quote]

Thanks! That's exactly what I needed to know!

Greg

[eluser]j0nxiest[/eluser]
Thanks for your answers OverZealous.

Then a question about best practises. If i have datastructure with $grannies->parent->child (all has_many), and in the Controller i'm fething all the grannies i want to show in the View, should i loop and fetch all the parents and children in the Controller, or should i just fetch the grannies in the controller, and then in the View fetch the related child items, where i would anyways loop through the collections? Small things, but i'd like to get on the right track when i'm learning a framework/layer.

You've done a great job with DMZ, thank you for that!

[eluser]OverZealous[/eluser]
@j0nxiest

That's a sticky question. MVC diehards will probably tell you that you should load everything in the controller.

However, I find it to be a bit muddy in that situation. Think about it in terms of maintenance. If you make a change to the model, would it be a problem that the loading and looping are in the view vs the controller?

With DMZ, the majority of the effort is hidden away by the model, so I don't see a real problem in doing a simple get() in the view. Once you start changing what you are loading based on request properties (search, category filtering, sorting, etc), then it might make sense to pre-load everything in the controller, where the logic should reside.

So, I guess that's a long way to say: if it's a simple $parent->child->get(), it's OK in the view. If it is anything more complex, leave it in the controller. Smile

[eluser]GregX999[/eluser]
[quote author="TheJim" date="1274413036"]
Accessing an arbitrary item in the collection can be done using $thing->all, which is an array. The index depends on your configuration. If your configuration has

Code:
$config['all_array_uses_ids'] = TRUE;

then objects are indexed in that array by their ID. If FALSE, $thing->all is a standard array indexed starting with 0.[/quote]

Is there a way to specify this on a get() by get() basis? Or by using "->all" vs "->xxx" (something else)?

So if I wanted it to default to using IDs beginning with 0, but for a particular instance, to use the actual IDs of the objects?

Greg

[eluser]OverZealous[/eluser]
@GregX999

He's referring to an old "feature" of DataMapper that I changed a while ago. Originally, the only way to access results was through the ->all array. This array was indexed using the rows id.

There were several problems here. First, the query had to include the id, so this led to odd errors if no id was selected. Second, you could never have a query that resulted in more than one copy of a particular item (such as a n-to-many query without DISTINCT).

So, I would personally make sure that all_array_uses_ids is always set to FALSE, and always assume the ->all array is a normal, sequentially indexed array.

Finally, remember that the all array only exists if you call the normal get method, not get_iterated.

That being said, you may be able to override this by setting all_array_uses_ids on the object just before calling get(). But this is not a supported feature, and there is no guarantee it will work on a future version.

[eluser]TheJim[/eluser]
[quote author="GregX999" date="1274482944"]
Is there a way to specify this on a get() by get() basis? Or by using "->all" vs "->xxx" (something else)?

So if I wanted it to default to using IDs beginning with 0, but for a particular instance, to use the actual IDs of the objects?

Greg[/quote]

It would be bit of a hack, but since all the configuration is stored as member variables of the object, you could do

Code:
$thing->all_array_uses_ids = TRUE;
$thing->get();

which should work as you're proposing

[eluser]NachoF[/eluser]
I know this has probably been answered many times before but I still dont know:

What is the best way to turn a set of datamapper objects into a form_dropdown()?? Keeping in mind that you have to (somewhere) specify that the value for each object must be its the column id and the name must be whatever you want... please help.

[eluser]OverZealous[/eluser]
@NachoF

First, you can search the manual AND the forums using the search from the manual.

Second, that's such a simple piece of code, just make it into an extension.
Code:
function get_dropdown_array($object, $label_field = 'name') {
    $ret = array();
    foreach($object as $o) {
        $ret[$o->id] = $o->{$label_field};
    }
    return $ret;
}

Throw that into an extension, and you can use it anywhere. Or, write your own helper function.

[eluser]NachoF[/eluser]
Thank you.. I turned it into an extension and it works perfectly..

I have another question though... I have set up my database to have a default Value of 0 for some fields... the problem is that in my form if I dont write anything for those fields it will add "" to the field and try to insert it to the database which gives me a database error.
Code:
$o->numeric_field=$this->input->post('numeric_field');

I have also set up my model to include the numeric validation rule for those fields.. and it seems to be passing that rule. I know the rule is working just fine cause when I type alphabetical characters it wont go through with the save method and return the validation error..

So how can I fix this problem without too much hacking?




Theme © iAndrew 2016 - Forum software by © MyBB