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.

[Deprecated] DMZ 1.6.2 (DataMapper OverZealous Edition)

November 23, 2009 11:54pm

Subscribe [46]
  • #31 / Dec 10, 2009 2:35pm

    BrianDHall

    760 posts

    Pardon me if this is a silly question, but consider this:

    $user->usergroup->get();

    So you get the usergroup that the user belongs to - simple. But where the hell is the usergroup object?

    If I do:

    $usergroup = $user->usergroup;

    ...that works, and $usergroup is now my usergroup object. When I use my debugger to look into the $user object, there is no sign of usergroup anywhere in it, nor in any other variable I can seem to locate.

    If I want to peek into the structure of such a related object, do I need to assign it to a variable before I’ll be able to see it, or am I just missing where it might be hiding?

    EDIT: The point of this is mainly for debugging purposes, and technically I’m trying to hand off one of my Datamapper objects in a way friendly to the CI Template parser, since it isn’t capable of understanding object properties or array elements. So I want to use my debugger to peek and make sure I’m doing this in the most logical way, and it’s hard when the object in question can’t be found.

  • #32 / Dec 10, 2009 2:40pm

    OverZealous

    1030 posts

    @BrianDHall

    All of the related objects in DMZ are created via PHP magic methods.  Actually, a lot of the stuff is magically generated.  This has several very important benefits:

    1) You don’t waste memory and time creating unused objects.
    2) You avoid infinite recursion ($user->usergroup->user->usergroup->user->...)
    3) Autoloading is more intelligent - the objects are only auto-loaded the first time you access them.

    The side effect is that many of the properties of a DMZ model are not visible to debuggers until the first time they are used.

    Also, I haven’t actually gotten a proper PHP debugger set up yet (I know - shameful), so I don’t know if there are limitations [in the debugger].

    Edited last sentence.

  • #33 / Dec 10, 2009 3:00pm

    BrianDHall

    760 posts

    Aha, thanks Phil - that makes sense now.

    For those interested I worked out how to make it work with a single function either in the datamapper object, or you can make it into an extension to use in all of them if you are so inclined:

    Extremely easy:

    function get_parsable_array()
        {
            $i = 0;
            foreach ($this->all as $res)
            {
                foreach ($res->stored as $key => $value)
                {
                    $return[$i][$key] = $value;
                }
                $i++;
            }
    
            return $return;
        }

    So now you can easily do something like this:

    $forum = new Forum(1);
    
    $forum->threads->get();
    
    $threads = $forum->threads->get_parsable_array();
    
    $this->parser->parse->('view_here', array ('threads' => $threads))

    And then you can do something like this in the view:

    {threads}
    {title}, last post {last_post_time}
    {/threads}

    Playing with the template class to minimize code mixing with HTML - purely because I hate all the <? ?> crap, reminds me of spaghetti code too much.


    EDIT: I’ve noticed something highly annoying to me - while the parser does understand the previous code in a sort of foreach style with less coding, you can’t follow it along with php code mixed in when necessary. So you can’t stick in an if() to, say, turn a flag of whether or not a post is new into something other than its raw variable inside the {tag}{/tag} execution.

    Seems like trading a case of dry scalp for crabs. No thanks.

    LOL

  • #34 / Dec 10, 2009 9:44pm

    tdktank59

    322 posts

    Hey Pill having some issues with the extensions.

    $n = new Note();
    // It is highly recommended you load the note before saving.
    $n->get_by_id($this->input->post('id'));
    
    $related = $n->from_array($_POST, array('message', 'date', 'category'));
    // $related includes any new categories that need to be saved.  At this point, $n may have had some old categories deleted.
    
    // add a related editor
    $related['editor'] = $logged_in_user;
    
    // save with the related objects
    if($n->save($related))
    {
        // redirect after save
    }

    For some reason the $related is not being set as if the from_array is not passing off the data when set to a variable. The other method where I save with from_array($_POST,’‘,TRUE); works tho.

  • #35 / Dec 10, 2009 9:51pm

    OverZealous

    1030 posts

    @tdktank59
    That has nothing to do with the extensions.  That’s a normal related save.  You probably have a misconfiguration in your relationships, or $logged_in_user is invalid.

  • #36 / Dec 10, 2009 9:58pm

    tdktank59

    322 posts

    @tdktank59
    That has nothing to do with the extensions.  That’s a normal related save.  You probably have a misconfiguration in your relationships, or $logged_in_user is invalid.

    That was your demo
    http://www.overzealous.com/dmz/pages/extensions/array.html

    Heres what I am using and it dosnt work (following your examples btw)

    $c = new Customer();
            
            if ($_POST)
            {        
                $rel = $c->from_array($_POST, array(
                    'first_name',
                    'last_name',
                    'address',
                    'city',
                    'state',
                    'zip',
                    'country',
                    'phone_number'
                ));
    
                $rel['custom_id'] = 'C'.random_string('alnum', 11);
                if ($c->save($rel))
                {
                    $this->session->set_flashdata('success', 'The Customer has been created');
                    $this->session->set_flashdata('customer_id',$c->id);
                    redirect('customers');
                }    
            }
            
            echo $c->render_form(array(
                    'Customer' => 'section',
                    'first_name',
                    'last_name',
                    'address',
                    'city',
                    'state',
                    'zip',
                    'country',
                    'phone_number'
                ),
                'customers/create',
                array(
                    'save_button' => 'Save',
                    'reset_button' => 'Clear'
                )
            );


    EDIT: Never Mind it works… My relations were screwed up since I hadnt setup one of the models yet.

  • #37 / Dec 11, 2009 12:16am

    Mirage

    273 posts

    Hi there - new challenge:

    How to I save additional fields into a join table?

    I’ve looked at the bugs example application (though not a most recent version). The bugs_users join table has two extra fields: is_owner and is_complete.

    However the example app makes no use of these fields, so I can’t peek. 😊 Say I created a bug, which would make me the owner, then how would I set the is_owner field in the join table?

    My particular need is that I have plants and collections of plants. In plantcollections_plants join table, I’d like to assign a specific ‘display_title’ to the plant, only used for that collection. How do I do that?

    Thanks,
    m

  • #38 / Dec 11, 2009 12:28am

    OverZealous

    1030 posts

    @Mirage
    Look in the docs, under Working with Join Fields

  • #39 / Dec 11, 2009 2:41am

    OverZealous

    1030 posts

    Status Update:

    For all those interested, I started an RSS feed for DMZ updates.

    Just visit any of the doc pages and click the RSS feed icon in your browser.  If the link doesn’t show up, you probably need to refresh or force-refresh (SHIFT+F5) the page.

    Alternatively, the feed is explicitly linked on the changelog or download pages.

    I also added a few items to the manual (specifically in the troubleshooting section).  Nothing major, but hopefully they will help with debugging.

  • #40 / Dec 11, 2009 11:11am

    Mirage

    273 posts

    @Mirage
    Look in the docs, under Working with Join Fields

    There it is! Awesome, thanks!
    -m

  • #41 / Dec 11, 2009 12:49pm

    Mirage

    273 posts

    On the include_join_fields() method:

    Wouldn’t it be possible to keep the name of the join field (without the ‘join_’ prefix) if there is no ambiguity with a field from the relation? Seems to me that the relation table is queried once anyway to get it’s column names. So the query builder could thus actually be smart about adding the ‘join_’ prefix.

    Alternatively, perhaps let the prefix be passed as a parameter and allow that to be ‘’ ?

    Thanks,
    -m

  • #42 / Dec 11, 2009 12:56pm

    OverZealous

    1030 posts

    @Mirage

    I really don’t think it is worth the time and extra code for that.  A join field is specifically one that does not exist on either object in the relationship, but on the relationship itself.  It needs to have something that distinguishes it from the object it is attached to.

    You might be able to put something together using a Get Rule that copies a join field into a normal field for your application, however.

  • #43 / Dec 11, 2009 1:40pm

    BrianDHall

    760 posts

    OK, here’s my issue and how I think I’ll handle it:

    User can have many cart_items, and each cart_item can have many listings. I want to get all listings related to all cart items, and tried this:

    $user->cart_item->get();
    $user->cart_item->listing->get()

    Now this works if I wanted to get all listings related to one cart_item, but I want all listings for all cart items.

    The complication here is I can’t simply ask for all listings related to one user, as there are listings related to a given user that are not cart_items.

    Now what I intend to do to fix this is get the cart items related to a user, and then use a foreach on $user->cart_item->all, and then inside that loop I’ll get the related listings for a given cart_item, and then I’ll add references to those listings into an array I’ll build up so that when the loop is done I have all the listings I’m interested in collected in one place.

    So that should work, but it just seems I’ve either made things overly hard on myself with design (this application wasn’t originally going to have a shopping cart, just is getting tacked on in the last few days), or I’m missing a whizbang feature of DMZ and don’t know it.

  • #44 / Dec 11, 2009 1:55pm

    cube1893

    18 posts

    Hi guys,

    i have a method which adds a new “course” to my database. A course has a start date and an end date. The end date can be NULL. Both fields are defined as DATE in MySQL, end_date has the ‘is_null’ option checked.

    Everything works fine, but if the user doesn’t enter an end_date, the system writes an empty DATE string(0000-00-00) into the row. What do I have to change in order to get the NULL into the column?

    function add()
    {
            $c = new Course();
            $related = $c->from_array($_POST, array(
                'course_type',
                'start_date',
                'end_date',
                'season'
            ));
    
            if($c->save($related))
            {
                $this->index();
            }
            else
            {
                echo $c->error->string;            
            }
    
    }

    I appriciate any solutions!

  • #45 / Dec 11, 2009 2:02pm

    Mirage

    273 posts

    I’d say you need to check the end_date before you use the from_array method. If the ‘end_date’ field is empty, set it to NULL instead of the empty string or remove the key altogether.

    If setting to NULL works you could probably also add a rule (empty_to_null) in the course model that does this for you transparently.

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

ExpressionEngine News!

#eecms, #events, #releases