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]
  • #151 / Jan 03, 2010 6:59pm

    tdktank59

    322 posts

    @rootman

    It looks like you are trying to compare an ID field to an email field. That would be your problem there…

    As far as saving goes.
    You need to have at least 1 side of the relationship saved before you can save more to it. The way I normally work relationship saves are as follows.
    This example happens to be a one to one relation however it would work in all cases if I am not mistaken.

    $u = new User();
    $u->username = $username;
    $u->password = $password;
    $u->save();
    
    $ui = new User_info();
    $u->first_name = $first_name;
    $u->last_name = $last_name;
    $ui->save($u);
  • #152 / Jan 03, 2010 8:10pm

    OverZealous

    1030 posts

    @silvergear99

    include_related only includes the field for output.  If you want to save contact_related, you need to manually load the contact, and save that field separately:

    $user->contact->get();
    $user->contact->name = ...
    $user->contact->save();
  • #153 / Jan 04, 2010 5:33am

    silvergear99

    7 posts

    @OverZealous

    Thanx, it works now. 😊

    $related = $user->from_array($_POST, array('username','group'));
                
    $user->contact->get();
    $user->contact->name = $this->input->post('contact_name');
    
    $user->save($related);
    $user->contact->save();

    I have like this now, but is their a better way to save ?

  • #154 / Jan 04, 2010 5:39am

    OverZealous

    1030 posts

    I’m not sure what these lines are for:

    $related = $user->from_array($_POST, array('username','group'));
    $user->save($related);

    I’m guessing that isn’t doing anything, because $related never has get() called, therefore there is no object to save.

    Also, you must check the results of every call to save().  Otherwise, you won’t be able to catch errors and report them.

    Finally, you should wrap multiple calls to save in a transaction, to ensure that you can rollback any changes if an error occurs:

    $user->trans_begin();
    
    $success = $user->save();
    if(!$success) {
        // report error
    }
    
    $success = $success && $user->contact->save();
    if(!$success) {
        // report error
    }
    
    if($success) {
        $user->trans_commit();
    } else {
        $user->trans_rollback();
    }
  • #155 / Jan 04, 2010 6:15am

    silvergear99

    7 posts

    I’m not sure what these lines are for:

    $related = $user->from_array($_POST, array('username','group'));
    $user->save($related);

    I’m guessing that isn’t doing anything, because $related never has get() called, therefore there is no object to save.
    </code></pre>

    But $related includes the group (Can be: Admin, Registred or Banned) the user has chosen from the dropdown. (At least I thought so). If I dont save($related) the group is never saved.

    I actually never used transactions before, but that was very easy. I will definitely use that in the future. Thanx for your tips.

  • #156 / Jan 04, 2010 9:33am

    silvergear99

    7 posts

    This is odd. If validation fails on contact_name and the error message is echod. Yet it still save the username.

    if($this->input->post('username'))
            {
                $related = $user->from_array($_POST,array('username','password','email','group'));
                $user->contact->get();
                $user->contact->name = $this->input->post('contact_name');
                $user->trans_begin();
                $user->contact->trans_begin();
                $success = $user->save($related);
                if(!$success) 
                {
                    $user->error_message('transaction', '$user:The transaction failed to save (insert)');
                }
                
                $success = $success && $user->contact->save();
                if(!$success) 
                {
                    $user->contact->error_message('transaction', '$user->contact: The transaction failed to save (insert)');
                }
                
                if($success)
                {
                    $user->trans_commit();
                    $user->contact->trans_commit();
                } 
                else 
                {
                    
                    echo $user->contact->error->string;
                    echo $user->error->string;
                
                    $user->trans_rollback();
                    $user->contact->trans_rollback();
                }    
            }
  • #157 / Jan 04, 2010 2:58pm

    TheJim

    35 posts

    @cube1893

    Thanks, include_related helps but it only works for one-to-one relationships (applicant). How can I achieve the same for one-to-many relationships? For example if I want to access all “note” objects for an application in the second foreach loop. If I call $applicationg->notes->get() in every loop, it causes bad performance, again.

    You’re looking at the problem the wrong way. Do your get on the “many” part of that relationship, and use include_related to pull in the “one” part (it doesn’t just work on one-to-one relationships; it’s that it only works for the “one” side of any relationship). Then you can grab everything in one query.  And of course follow the other tips to make sure you’re only pulling in fields you need and consider your logic so that you’re sure you really need to load everything you’re attempting to grab.  Finally, if you absolutely need to load a lot of objects, take a look at my earlier get_streaming post.  It makes a significant difference when loading a lot of objects.

  • #158 / Jan 06, 2010 3:09am

    OverZealous

    1030 posts

    This is odd. If validation fails on contact_name and the error message is echod. Yet it still save the username.

    Uh, that’s because I sent you a bad example.  😊  The second error message is printing whenever $success is FALSE.  The code should look more like this:

    // .. snip ...
        $user->trans_begin();
        $success = $user->save($related);
        if(!$success) 
        {
            $user->error_message('transaction', '$user:The transaction failed to save (insert)');
        }
        if($success) {    
            $success = $user->contact->save();
            if(!$success) 
            {
                $user->contact->error_message('transaction', '$user->contact: The transaction failed to save (insert)');
            }
        }    
        if($success)
        {
            $user->trans_commit();
        } 
        else 
        {
            echo $user->contact->error->string;
            echo $user->error->string;
    
            $user->trans_rollback();
        }    
    }

    Alternatively, you could not report an error, and just check the error properties on $user and $user->contact after the fact.  (These should be filled with the reason why the save failed.)

    Also, you shouldn’t need to have more than one transaction.  The PHP method to connect to the database should always return the same DB handle, so the transaction is global.  You can start and end the transaction on any object, and all internal transactions are committed or rolled back at once.  Because transactions can be expensive, you should only create the minimum needed.  (You can, however, nest transactions if you need.)

    Finally, I didn’t mention this before, but transactions require a specific table type if you are using MySQL.  Pretty much all other common databases support transactions out-of-the-box.

  • #159 / Jan 06, 2010 3:12pm

    silvergear99

    7 posts

    OverZealous
    Thanx it works much better now.

    But I have an other problem now 😊
    No matter what I’m doing I just can’t save when I’m using a radiobox. Get the error: Error Number: 1054
    Unknown column ‘Array’ in ‘field list’.
    But If I change type to ‘dropdown’ it saves.

    view:
    echo $user->render_form
    (
        array
        (
            'enabled' => array
            (
                'type' => 'radio'
            )
        ),
        "admin/users/edit/{$user->id}",
        array
        (
            'save_button' => 'Ändra'
        )
    );
    model user:
    var $validation = array
        (
            'enabled' => array
            (
                'label' => 'Enabled',
                'type' => 'radio',
                'values' => array(0=>'No',1 =>'Yes')
            )
  • #160 / Jan 06, 2010 3:23pm

    OverZealous

    1030 posts

    @silvergear99

    You’ll have to do some basic debugging.  Look at the generated HTML source, output whats being sent back with the CI profiler, or even just echo some code to see what is happening.

  • #161 / Jan 06, 2010 4:06pm

    silvergear99

    7 posts

    OverZealous

    Made som debugging and it seems like ‘radio’ allways returns an array. That’s way I got the error.

    So now I have to do like this. But it works

    $tmp = $this->input->post('enabled');
    $user->enabled = $tmp[0];
    $user->save()

    Would be much easier if it didn’t return an array but I’m sure you have a valid reason for this 😊

    Also I have to change to this (in the user model):

    'values' => array(1=>'No',2 =>'Yes')
    because
    'values' => array(0=>'No',1 =>'Yes')
    allways return the following array no matter what option you choose:
    Array
    (
        [0] => 1
    )
  • #162 / Jan 06, 2010 10:21pm

    coldKingdom

    30 posts

    Hello!

    Couldn’t delete my question so I will just say thank you instead 😊

  • #163 / Jan 07, 2010 9:15am

    Oblique

    32 posts

    +1 to the recently discussed “radio” problem. same s**t.
    don’t realy see the reason to render radios as “name=$blah[]”
    and “from_array()” does not work as well in that case
    Looks like some kind of “oops, forgot this” while writing htmlform, while it is really cool ext!

    btw DMZ is half of a CI for me or even more

    upd: a little workaround while Phil allmighty thinks how to make this gracefuly:

    htmlform.php
    697

    if(is_array($value) AND $type != 'radio')
  • #164 / Jan 07, 2010 6:04pm

    Brandon Jackson

    4 posts

    I thought I would share the solution to a problem that had been plaguing me of late. DataMapper was throwing errors for every request which read:

    Message: Invalid argument supplied for foreach()
    Filename: libraries/datamapper.php
    Line Number: 422

    Message: array_keys() [function.array-keys]: The first argument should be an array
    Filename: libraries/datamapper.php
    Line Number: 88

    In addition to annoying me, these errors prevented extensions from loading. As it turns out, I had set the datamapper config file to autoload. This was causing the problem. I cleared the autoload config array and everything worked fine. Hope this solves someone elses problem.

  • #165 / Jan 08, 2010 9:24am

    rideearthtom

    21 posts

    Just a quick one to say thanks for the most useful CI library I could imagine. It’s cut my dev time and the number of lines of code written by about 90%. You rule! 😊

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

ExpressionEngine News!

#eecms, #events, #releases