ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

DMZ 1.7.1 (DataMapper OverZealous Edition)

March 14, 2010 11:43pm

Subscribe [104]
  • #271 / May 20, 2010 7:37pm

    TheJim

    35 posts

    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

    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

    $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.

  • #272 / May 20, 2010 7:40pm

    GregX999

    39 posts

    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

    $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.

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

    Greg

  • #273 / May 21, 2010 9:46am

    j0nxiest

    9 posts

    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!

  • #274 / May 21, 2010 9:51am

    OverZealous

    1030 posts

    @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.  😊

  • #275 / May 21, 2010 3:02pm

    GregX999

    39 posts

    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

    $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.

    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

  • #276 / May 21, 2010 3:13pm

    OverZealous

    1030 posts

    @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.

  • #277 / May 21, 2010 3:13pm

    TheJim

    35 posts

    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

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

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

    which should work as you’re proposing

  • #278 / May 22, 2010 7:55pm

    NachoF

    171 posts

    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.

  • #279 / May 22, 2010 7:59pm

    OverZealous

    1030 posts

    @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.

    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.

  • #280 / May 23, 2010 12:14am

    NachoF

    171 posts

    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.

    $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?

  • #281 / May 23, 2010 12:39am

    OverZealous

    1030 posts

    @NachoF

    I don’t know what to tell you.  If you set the field to an empty string, then DMZ has to assume you want to save it.  An empty string is not a NULL, and when inserting new data, DMZ only excludes NULLs from the query.

    The best solution is to not set fields you want to be NULL to an empty string.

    Also, if a field is EMPTY (not necessarily NULL), but not required, the validation routine will not run for that field.  There is a workaround for this in the latest DMZ: the always_validate rule.  It works like required, but doesn’t through an error on empty strings.

    You could add a rule that sets the default value if the string is empty, and then add the always_validate rule to ensure it runs.  (Or you could add a rule that sets the value to NULL if empty, but the numeric rule will probably barf on NULL, so you’d have to override that one as well.)

  • #282 / May 24, 2010 4:02pm

    Enorog

    13 posts

    Hello Phil & fellow DMZ-ers. I’m migrating from Doctrine 1.2 to DMZ and what I’ve seen so far is wonderful! Doctrine has a lot of power, but it’s hidden deeply and not really logical - after seeing DMZ in action I’m willing to give up my painfully achieved Doctrine veteran status and just take a clean start. Thank you for your efforts!

    My first question - I’m trying a little “hello world” and I cannot find the SQL code for the tables you use in your examples (http://www.overzealous.com/dmz/pages/gettingstarted.html). Am I supposed to reverse-engineer the table structure? It would certainly be nice if you could provide this code to eliminate errors when starting up.

    The second thing is - I’m using HMVC (Modular Extensions - http://codeigniter.com/wiki/Modular_Extensions_-_HMVC/). Do you know if it will interfere with DMZ?  I’m thinking about putting all my DMZ models in /application/models and everything else (the M of MVC) in HMVC triads under /application/modules.

    Thanks!

  • #283 / May 24, 2010 4:30pm

    NachoF

    171 posts

    I know Phil doesnt want to support the html form plugin anymore but will anyone else help me out?? I recently uploaded my app to a server which has php 5.1.6 and suddenly the magic method __toString doesnt seem to be working.. on my dropdown options I get a bunch of Object id #40…
    after some googling the only thing I could find was this
    http://www.jcinacio.com/2007/04/19/phps-__tostring-magic-method-not-so-magic-before-520/

    so, any idea on how can I fix it?? upgrading is not an option.

  • #284 / May 24, 2010 9:46pm

    Daniel H

    197 posts

    I’m really worried that I’m doing something really stupid here but I can’t get a complex relationship to work.

    An Event can be related to many Images, and also can related to one Thumbnail (which is an Image).

    In my database, an Event has a thumbnail_id field.

    In my Event model:

    var $has_one = array(
            'created_by' => array(
                'class' => 'user',
                'other_field' => 'created_event'
            ),
            'edited_by' => array(
                'class' => 'user',
                'other_field' => 'edited_event'
            ),
            'thumbnail' => array(
                'class' => 'image',
                'other_field' => 'event_thumbnail'
            ),
            'venue'
        );

    and in my Image model:

    var $has_one = array('event',
            'event_thumbnail' => array(
                'class' => 'event',
                'other_field' => 'thumbnail'
            ));


    Yet when I save the thumbnail relationship, I’m getting, “Unable to relate image with thumbnail.”. Not sure where I’m going wrong here… Anything obvious?

  • #285 / May 24, 2010 10:10pm

    OverZealous

    1030 posts

    I’m trying a little “hello world” and I cannot find the SQL code for the tables you use in your examples (http://www.overzealous.com/dmz/pages/gettingstarted.html).

    That page is a leftover piece from the original datamapper.  While I don’t have exact pieces for those samples, if you look at the example app, included with the full download, nearly the same code was used for that app.  That’s a more complete application.  😊

    I’m using HMVC ... Do you know if it will interfere with DMZ?

    I can’t answer this, but there have been discussions about it in the past.  Use the search in the manual, it will also search the forums!

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases