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]
  • #286 / Feb 16, 2010 11:15pm

    OverZealous

    1030 posts

    If that is the functionality you want, then you should implement it in a validation rule, as I said before.

    A simple rule like this one (as an extension) works:

    class DMZ_NullOnEmpty {
        // If the $field is empty(), set it to NULL
        function rule_null_on_empty($object, $field) {
            if(empty($object->{$field})) {
                $object->{$field} = NULL;
            }
        }
    }

    Usage:

    class MyModel extends DataMapper {
        ...
        $extensions = array('nullonempty', ...);
    
        $validation = array(
            'foo' => array(
                ...
                'rules' => array('null_on_empty')
                ...
            )
        );
        ...
    }

    You can always change it to be more specific.  Or, integrate it into your existing rule.

    The save method isn’t overwriting anything, it is when you set the values from $_POST that something is being overwritten.

  • #287 / Feb 17, 2010 12:32am

    beemr

    160 posts

    That works.  I like that it doesn’t fork the save() function.  Pretty nifty having the rule extension classes.  Again, love the thought that you have put into this.  Looking forward to your update.

  • #288 / Feb 17, 2010 9:38am

    DominixZ

    23 posts

    I have some little performance problem with DMZ. Some time i only want Object with join table (not include related table) like this.


    // This take time 0.0700 - 0.0800 in my db per query
    SELECT DISTINCT `links`.*
    FROM (`links`)
    LEFT OUTER JOIN `keywords_links` as keywords_links ON
      `links`.`id` = `keywords_links`.`link_id`
    LEFT OUTER JOIN `keywords` as keywords ON `keywords`.`id`
      = `keywords_links`.`keyword_id`
    WHERE (
    `keywords`.`id` IN (3, 4)
    )
    AND `links`.`website_id` = 1
    ORDER BY
      `links`.`created_at` asc

    to be similar like this

    // This take time 0.0110 - 0.0250 in my db per query
    SELECT DISTINCT `links` . *
    FROM `links`
    JOIN keywords_links ON links.id = keywords_links.link_id
    WHERE links.website_id =1
    AND keywords_links.keyword_id
    IN ( 3, 4 )
    LIMIT 0 , 30

    Is it has a way to join link table with keywords_links table only without join keywords table ?

  • #289 / Feb 17, 2010 7:57pm

    Conerck

    15 posts

    Hi,

    I just noticed some pitfall / unexpected behavior on the exists() method.

    (Using objects similar to the ones in the user guide)

    $obj = new Object();
    $obj->select('title', 'description');
    $obj->get();
    
    if ($obj->exists())
    {
      // This will never be reached
      echo 'foo';
    }
    else
    {
      // Instead we always end up here
      echo 'bar';
    }

    Even if the query returns some records from the DB $obj->exists() will always return false in this particular case, because the method simply checks if $obj->id has some value or not. And because the select() method didn’t fetch the id it IS empty even though the object DOES exist.

    This is pretty counterintuitive and should probably be changed. Either include the id in every query, even if a select() filter was set or rework the exist() method to return sane values in such a case.
    At the very least the User Guide should be updated with a warning to reflect this behavior.

  • #290 / Feb 17, 2010 7:59pm

    OverZealous

    1030 posts

    @Conerck

    Edit: Scratch what I said, I realize the problem.

    I understand your concern.  If you want to make a query like that, you should be checking the result of:

    count($object->all)

    I’ll think about the alternative, but the real purpose of the exists method is checking the existance of a single object.

  • #291 / Feb 17, 2010 8:08pm

    Conerck

    15 posts

    @Conerck

    Edit: Scratch what I said, I realize the problem.

    I understand your concern.  If you want to make a query like that, you should be checking the result of:

    count($object->all)

    I’ll think about the alternative, but the real purpose of the exists method is checking the existance of a single object.

    Right, but if I have a Table with a composite key or some UNIQUE constraint that allows me to make a single row query without the use of the ID field I run into the same problem.

    Basically, if the method was called has_id(), I wouldn’t complain, but since it is called exists() it implies a certain semantic meaning, which in some conditions breaks.

    Edit: As I said above, in the short term I could live with a warning in the User Guide. I just took me a while to figure out why my code wasn’t doing the things I expected and this was the reason. I expected my uniquely identified record to exist and DMZ told me it didn’t ;p

  • #292 / Feb 17, 2010 8:27pm

    OverZealous

    1030 posts

    Honestly, that is a tiny bit outside the intended design of DMZ.  (When you work outside the design, you should expect some things to not work the same.)

    As I said, I’ll look into a change.  I think this would work pretty well:

    return !empty($this->id) || (count($this->all) > 0);

    Which should have not performance impact on existing code.  I also can’t see how it would break anything, unless someone is doing something weird with ->all.  The all array is always empty unless something was returned with a query.

    It’s a good find.

  • #293 / Feb 17, 2010 8:52pm

    Conerck

    15 posts

    I wouldn’t call it outside the intended design. Unless you’d say that if someone wants to use a String, a timestamp, a GUID or one of the many other options for keys as an alternate or replacement to the autoincrement id as outside the intended design, in which case I’d have to say that the design scope is too narrow.

    Oh and also exist() should break if you do a query with LIMIT 1 and don’t include ‘id’ in your select()... THAT at least should be an intended use case 😉

    Anyway, the fix looks good. Thanks for the quick response, once again. There might be other frameworks out there that have more blows and whistles and whatnot, but your dedication makes DMZ a top choice.

  • #294 / Feb 17, 2010 10:16pm

    tdktank59

    322 posts

    Hey

    So ive got a small issue, that ive noticed before… as well.

    When you try and save a single field it runs through the entire validation check and ends up error-ing out on fields that I don’t care about at that point.

    Is there a way we can either make this only check the passed in fields (best solution in my mind, and since then it only checks what we want it to.) Otherwise a way to define which validations to run for a specific insert.

    Because im having an issue where ive got a few fields, for some reason or another a field gets blanked out and I try and update it again and it says cannot update because some other field is required that is blanked out as well.

    Thanks.

    @Oblique
    @tdktank59
    Either you are not loading the object before you are saving it, or you are setting individual fields to NULL after loading it.  DMZ only checks and saves the columns that have not changed since the last get or save.


    @OverZealous

    Here is what im doing that is causing the issue.
    This is just a general ajax edit function
    Basically Id send it the $field and the $value and a $user_id and the $table (user/user_info).
    Based on all that it would load the $table, look up the user (id/user_id)
    and save the $field and $value.

    However it returns an error for a field that it should not be caring about, and clears the value out of $field.
    The other field is set to require and due to this problem I’m stating, it removes the value on a failed update and then causes more problems.

    Heres the code:

    $u = new $table();
    $u->where($column,$user_id)->get();
    $u->{$field} = $value;
    if ($u->save())
  • #295 / Feb 17, 2010 10:39pm

    OverZealous

    1030 posts

    @tdktank59

    The validation rules run if:
    1) It is a related rule, OR
    2) There is no stored value (ie: new, unsaved object), OR
    3) The current value does not match the stored value

    If it is a related rule, then it runs always, because it isn’t possible to check against a stored value (currently).

    Otherwise, as long as you get() before you save(), OR you don’t get() but don’t set the field, the validation rule is not run.

    I don’t know what else to tell you.  Try setting up your code outside the current setup, with hard-coded columns and values, and see if you get the issue.

    (Man, I hope all that user-submitted data is being checked thoroughly!)

  • #296 / Feb 17, 2010 11:23pm

    tdktank59

    322 posts

    @tdktank59

    The validation rules run if:
    1) It is a related rule, OR
    2) There is no stored value (ie: new, unsaved object), OR
    3) The current value does not match the stored value

    If it is a related rule, then it runs always, because it isn’t possible to check against a stored value (currently).

    Otherwise, as long as you get() before you save(), OR you don’t get() but don’t set the field, the validation rule is not run.

    I don’t know what else to tell you.  Try setting up your code outside the current setup, with hard-coded columns and values, and see if you get the issue.

    (Man, I hope all that user-submitted data is being checked thoroughly!)

    LOL Yes it is being checked… I removed all of the code that is doing that sort of stuff for this post.

  • #297 / Feb 18, 2010 1:51am

    NachoF

    171 posts

    Ok Im trying to use form generation library but im just getting a bunch of weird errors…

    heres my method

    EDit: I just realized that it is outputting php raw code in my html.. I think its because they are not using full <?php tag but instead just <? tag.. how can I fix this?

    heres some of the html code that shouldnt be there.

    <?
      if( ! isset($save_button))
      {
    $save_button = 'Save';
    }
      if( ! isset($reset_button))
      {
    $reset_button = FALSE;
    }
      else
      {
    if($reset_button === TRUE)
    {
    $reset_button = 'Reset';
    }
      }; ?>
    <? if( ! empty($object->error->all)):; ?>
    <div class=“error”>
      There was an error saving the form.
      <ul><? foreach($object->error->all as $k => $err):; ?>
          <li><div [removed]>

    A PHP Error was encountered

    Severity: Notice
    Message:  Undefined variable: err
    Filename: libraries/Loader.php(673) : eval()‘d code

    Line Number: 21

    </div></li>
          <? endforeach; ?>

     

    function Create()
            {
                $user = new User();
                $data['user']=$user;
                $user->load_extension('htmlform');        
            $form_fields = array(
                'name',
            );
                $data['form_fields']=$form_fields;
                $this->load->view('users/create',$data);
            }

    heres my view

    <?php
        echo $user->render_form($form_fields);
    ?>

    Here is part of the long list of errors

    error->all)):; ?>

    There was an error saving the form.

      * error->all as $k => $err):; ?>
        A PHP Error was encountered

        Severity: Notice

        Message: Undefined variable: err

        Filename: libraries/Loader.php(673) : eval()‘d code

        Line Number: 21

    A PHP Error was encountered

    Severity: Notice

    Message: Undefined variable: row_class

    Filename: libraries/Loader.php(673) : eval()‘d code

    Line Number: 52

    A PHP Error was encountered

    Severity: Notice

    Message: Undefined variable: row_id

    Filename: libraries/Loader.php(673) : eval()‘d code

    Line Number: 52
    >
    A PHP Error was encountered

    Severity: Notice

    Message: Undefined variable: label_for

    Filename: libraries/Loader.php(673) : eval()‘d code

    Line Number: 53
    >Name:   
    A PHP Error was encountered

    Severity: Notice

    Message: Undefined variable: save_button

  • #298 / Feb 18, 2010 3:11am

    OverZealous

    1030 posts

    @NachoF

    2 things:
    1) I no longer support the HTMLForm extension.  There’s some messages about this on this forum.

    2) If you want to change the HTMLForm templates (which have the short tags), feel free to do so.  They are just samples, and it was expected, from the beginning, that you would edit the templates.  Alternatively, enable short tags in php.ini.

    Most likely your errors are related to the short tags.

  • #299 / Feb 18, 2010 3:17am

    NachoF

    171 posts

    Yes, it was the short tags… Ive fixed it…
    What do you mean you no longer support them?.. You dont encourage its usage anymore?

  • #300 / Feb 18, 2010 3:27am

    OverZealous

    1030 posts

    I mean exactly that I don’t support it. 😉  You can use it, you can modify it, etc, but I am not planning on updating it anymore, nor will I provide technical support. 🙄

    The reason is, almost half of the problems I have had reported here were with the HTMLForm extension; it is almost as complex as DMZ itself.  I tried to get someone to take over, but I haven’t had any luck.

    (Another reason I can’t truly support it is that I don’t use it myself.  I have my own toolkit I developed long before I even took over DataMapper, and it has a completely different design.)

    Hamburgler created his own variation on the concept, but it is a lot different.

    So, I don’t exactly discourage it’s usage.  If you already use it, great!  But I’m probably not working on it anymore.  I’ll continue including it with DMZ for the foreseeable future, and it will still be part of the example app (‘cause I’m not rewriting that!).  The next release will have a disclaimer about it.

    Hopefully that makes sense.

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

ExpressionEngine News!

#eecms, #events, #releases