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]
  • #421 / Mar 03, 2010 7:41am

    Oblique

    32 posts

    Once again with validation logic
    As long as I remember, this subject had being (? or had been… or smth else… whatever) discussed some time earlier, but wouldn’t it be nice for dmz user to have option only to validate those fields that are submitted?

    I have model, not all of which fields are needed to be validated at the moment of creation of new instance of it.

    According to my application’s logic, different classes of users work with this model, and one of this classes is creating instances of model and is required to set couple of fields, but not all of them.

    When it goes further - other class of users has to set third requred field.
    But if I mark this third field as required in model, dmz validation messes up creation of new instance.

    Sorry for bugging.

  • #422 / Mar 03, 2010 7:55am

    OverZealous

    1030 posts

    @Conerck
    Thanks for the info.

    I’m going to keep testing.  The first change I made still has a benefit.  The register_shutdown_function may not (although it has little to no performance impact).

    I have determined that part of my issue looked like connection issues, but turns out to have been some bizarre DNS issue.  The error message was getting swallowed because CodeIgniter hides all errors on the {db}_connect function.  I’ll continue testing, and provide a follow up later.


    @Oblique
    Once again: DMZ only checks fields that have been changed, or related rules.  (There is no way for it to know if a related field has been changed or not.)

    Besides, you simply are doing it wrong.  You have a field marked as required, yet you aren’t providing it.  DataMapper is therefore telling you that you aren’t providing a required field!  How else should it work?  Not setting a field is not setting a field.  There is no way for DataMapper to know the difference.

    I know I already wrote this: If you want something that to be required some of the time, but not all of the time, then set a custom _required method on the Model itself.  Check the field and the model, and you can return TRUE even when empty as necessary.

    class Whatever extends DataMapper {
    function _required($field) {
        // item1 and item2 are fields that are only check on existing objects.
        if($this->exists() || !in_array($field, array('item1', 'item2'))) {
            return !empty($this->{field});
        }
        return TRUE;
    }

    You can also use always_validate combined with a custom rule instead of overwriting the required rule.

    The same basic concept works for _related_required — just, instead of using empty() use parent::_related_required($field)

  • #423 / Mar 03, 2010 8:07am

    OverZealous

    1030 posts

    @Conerck
    I’m still pretty sure that if you use a Database connection pool, it is important to be sure that each connection is closed.  I say this because I can watch the pgpool status, and see the difference from before I added in the code to ensure a clean shutdown.

    I think I’m going to leave in the connection hooks, because they are harmless, and cleaning up after yourself is always good design.  😊

    (FYI: in my tests, switching to PGPOOL instead of direct connections, combined with the changes I mentioned a few posts ago, has almost cut some pages processing time by 1/3.)

  • #424 / Mar 03, 2010 3:49pm

    ciGR

    63 posts

    Hello! First of all, thank you about dmz, it’s very very useful.
    I have a question about a Self-Relationship. I have the category_mdl model
    with relationships as

    var $has_one = array(
                'parent' => array(
                    'class' =>'category_mdl',
                    'other_field' => 'sub_category'
                )
        );
        
        // Insert related models that Template can have more than one of.
        var $has_many = array(
            'sub_category' => array(
                                'class' => 'category_mdl',
                                'other_field' => 'parent')
        );

    All is ok, I can create new objects relationship etc..
    but when I try to get a category with include the parent_title, and parent_id
    with

    $category = new Category_mdl();
    $category->include_related('parent', array('id','title'));

    but i get the error:

    Fatal error: Cannot use object of type Category_mdl as array in /opt/lampp/htdocs/thcomer/system/application/libraries/datamapper.php on line 2556

    when I after the include try the get method

    $category->get();


    Another question is when for example I have the below category hierarchy

    |category-1
    |—category-2
    |——category-3

    if exists an easy way with DMZ to get from category-3 the parents category-1/category-2 like an array parent_tree(‘category-1’,‘category-2’)
    or I should create a loop like

    $cat = new Category_mdl();
    $cat->get_by_title('category-3');
    $cat->parent->get();//here get category-2
    $cur_parent = $cat->parent;
    $parents = Array();
    $parents[] = $cur_parent;
    while ($cur_parent->parent->get()->exists())
    {
         $cur_parent = $cur_parent->parent;
         $parents[] = $cur_parent;
    }
  • #425 / Mar 03, 2010 6:56pm

    OverZealous

    1030 posts

    @ciGR
    The error you are seeing is a subtle one.  You cannot use reserved names, such as parent for relationships or fields.  See the red box on that page.

    By naming a relationship parent, it sets a special variable that DMZ uses to track parent relationships internally, which is where the error is happening.  You could rename the relationship (and database columns!) to parentcat or something similar.

    (The parent keyword is leftover from DataMapper.  I have been thinking about changing it to something less common for DMZ 2.0, but that will probably break some existing code, so it will have to wait.)

    ——————————

    You can easily include the parent’s parent by doing this:

    $cat = new Category_mdl();
    $cat->include_related('parentcat/parentcat', array('id', 'title'));
    $cat->get();
    echo $cat->parentcat_parentcat_title;
  • #426 / Mar 03, 2010 7:13pm

    ciGR

    63 posts

    OverZealous, thank you very much.It’s all ok now!

    For the second part of my question,I see the deep Relationships, very useful, but I show you the three level hierarchy for example, I really mean if exist some easy way to get the hierarchy for an arbitrary number of levels, or I must to create my own function like the one I present(not tested,just for show you what I mean) above.

    thank you again.

  • #427 / Mar 03, 2010 7:22pm

    OverZealous

    1030 posts

    @ciGR
    Sorry, I misunderstood on the second question.

    There’s nothing in DMZ to handle recursive relationships explicitly, so I’m guessing you’ll need your own function(s).

    Someone (I can’t remember who, I apologize) was working on a slick extension to work with Modified Preorder Trees.  Apparently they either were not able to release it, or have chosen not to.

  • #428 / Mar 03, 2010 7:25pm

    ciGR

    63 posts

    You didn’t misunderstood,
    Was my fault,I forgot to write that I ask for an arbitrary numbers levels.

    Thank you.

  • #429 / Mar 05, 2010 5:13am

    rideearthtom

    21 posts

    I love this library, it makes me feel like I don’t even have to write code any more, just think about what I want it to do… 😊

    A couple of inflector issues:

    ‘photograph’ is pluralised to ‘photographes’, should be ‘photographs’
    ‘inventory’ is pluralised to ‘inventorys’, should be ‘inventories’

    Keep up the good work!

  • #430 / Mar 05, 2010 2:22pm

    OverZealous

    1030 posts

    @rideearthtom
    I see the issue with photographs, but inventories works fine here.

    Are you sure you are using the inflector included with DMZ, and not the built-in one?

    If you want to correct photographs, change line 136:

    elseif (in_array($end1, array('h', 'o', 'x')))

    to this:

    elseif (in_array($end1, array('o', 'x')) || in_array($end2, array('ch', 'sh')) )

    I’ll include it with the next release.  Thanks for finding the one!

    Also, while looking at the Wikipedia article on pluralizing:
    • I fixed the incorrect pluralization of words like toy or play, which would have been made into toies and plaies, respectively.  Now [vowel]y is correctly pluralized by just adding an ‘s’.
    • I decided that I should add in the less usual case of a word ending in [vowel]o, which is pluralized with just an ‘s’, instead of ‘es’.
    • I also added double-S words (miss, pass) to the ‘es’ rule above.  You can add it to the second array in the corrected rule above:

    elseif (in_array($end1, array('o', 'x')) || in_array($end2, array('ss', 'ch', 'sh')) )

    Any other odd pluralizations will need to be fixed the manual way 😉

  • #431 / Mar 08, 2010 1:04pm

    PoetaWD

    97 posts

    Hey Phil!

    I see that you still working very hard on this project, and version 1.7 is on its way!

    Thank you VERY much for this!

    I am planning to release a Auth Library based on DMZ to contribute with the community.

    I also have another sugestion to DMZ:

    A validation rule to check if there is already a related object with the same property.

    I had to create my own function to check for this:

    function check_relationship($field_id, $otherfield)
        {
            $o = new Gene_relationship;
            $o->where('gene_section_id', $field_id);
            $o->where('stNome', $otherfield);
            $o->get();
            
            if($o->exists())
            {
                return TRUE;
            }
            else
            {
                return FALSE;
            }
        }

    But it would be much easier if we had a validation rule for this:

    I tried the unique_pair validation rule but it didnt work:

    var $validation = array(
                                
            'stName' => array(
                'rules' => array('required', 'max_length' => 200, 'unique_pair' => 'gene_section_id'),                        
                'label' => 'Nome'
            )

    The way I suggest:

    var $validation = array(
                                
            'stName' => array(
                'rules' => array('required', 'max_length' => 200, 'unique_relationship' => 'gene_section'),                        
                'label' => 'Nome'
            )

    That would check if there is a record related to “gene_section” with that property, in this case, “stName”.

  • #432 / Mar 08, 2010 2:17pm

    OverZealous

    1030 posts

    @PoetaWD

    That’s an interesting validation rule.  Did you know you can easily add new validation rules via an extension?

    The rule you are suggesting would be a perfect example of an extension validation rule!

    As soon as you get your extension ready, please add it to the 3rd-party extensions page.  That link is to a document I’ve written that hopefully will help get you started how.

  • #433 / Mar 08, 2010 4:16pm

    GregX999

    39 posts

    Doh! I posted this in the wrong thread at first…

    Is there a way to write a trigger? (I think it’s called a trigger.) I want to put a method in a model that gets called when an object of that type gets deleted. It’s an “Image” model, and if an Image object gets deleted, I want the model to delete all the appropriate image files.

    Greg

  • #434 / Mar 08, 2010 4:21pm

    OverZealous

    1030 posts

    @GregX999

    Not specifically, but there are a couple of options.  The best way, IMO, is to simply overload the delete method, like so:

    class Image extends DataMapper {
    
        ...
    
        function delete($object = '', $related_field = '') {
            // capture any important information here (id, etc)
    
            // delete the item
            $success = parent::delete($object, $related_field;
    
            // if the item was successfully deleted, then cleanup
            if($success && empty($object) && !is_array($object)) {
                // do the cleanup
            }
        }
    
        ...
    
    }
  • #435 / Mar 08, 2010 4:23pm

    GregX999

    39 posts

    Ok, looks straight-forward. Thanks!

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

ExpressionEngine News!

#eecms, #events, #releases