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]
  • #106 / Mar 29, 2010 12:14am

    OverZealous

    1030 posts

    I want to save it all at once cause its all coming from the same form.

    You have to save each new object one-by-one.  You also have to save relationships on the object as they exist.

    However, if you wrap them all in a transaction, you can rollback the saves if something goes wrong.  (Even when DMZ saves multiple relationships, it doesn’t automatically wrap them in a transaction — if something failed, it would not automatically roll back the first items saved.)

    $project->trans_begin();
    // if new: save $region, $function, or $organization, too
    $success = $stage->save(array($region, $function, $organization));
    if($success) {
        $success = $project->save($stage);
    }
    if($success) {
        $project->trans_commit();
        $this->session->set_flashdata('success', 'Project Created');
        redirect("Welcome");
    } else {
        $project->trans_rollback();
        // show error, etc.
    }

    * Note: you need a transactionable table type if you are using MySQL.

  • #107 / Mar 29, 2010 12:49am

    OverZealous

    1030 posts

    @Everyone

    For the other recent questions, I’m sorry I don’t have time to fully respond to you all directly.

    I’m accepting a new job in a new city in a different state as I write this.  :cheese:  This is actually great news in the long run!  I’ll explain in a bit.

    In the short term, this means that I won’t be able to provide much support or feedback for DMZ.  I have a lot to do with preparing a house for being sold, packing, etc.  I’ll do my best, but the more in-depth answers I have been giving in the past are probably not going to be seen for a while.  It will also probably be days—not hours—before I can respond to questions.

    I hope that my current users will do their best to help provide answers during my transition.  😉

    The company I am going to work for uses CodeIgniter.  They have been interested in the work I’ve done on DMZ, and I believe (although I have no guarantee of this) that DMZ will become an integral part of the work I do there.

    What this may mean is that I effectively will get paid to ensure that DMZ is kept up-to-date.  Even if this isn’t my primary job, it hopefully will be good for the project.  :coolsmile:

    I’ll now try to provide some feedback for the other questions. 😊

    —————

    @tomdelonge
    Self-relationships should work perfectly.  For some example of how this might look, check out the bug model, and see the dependent-dependency relationship.  It’s not actually used in the application, but that is how it might look.  You can easily set it up with one side under $has_one, and the other stays under $has_many.

    Also, in the manual, on the Advanced Relationships page, the post-related_post relationship is another good example.

    Note that to save or delete a self-relationship, you’ll need to specify the relationship key, like so:

    $bug->save($other_bug, 'dependent');

    —————

    @santiagofs
    For questions like this, please take the time to explain your actual problem.  Otherwise we’re just guessing.  As it stands, I have not heard of this issue.

    —————

    @Henry Merriam
    It would be fairly easy to write a custom magic __call or __get function.  I wouldn’t make it part of the core, but you could make it part of a custom class.  See the second part of Using Extensions for ideas on how to do this.  Make sure you call parent::__call/__get after your own customizations.

    If you use this sporadically on different models, you could write an extension to provide the methods.  However, extensions cannot handle magic methods, so you’d probably need to have a method more like localize_value($field, $value = NULL)

    —————

    @Cord
    I’m not going to say it’s impossible, but DMZ just isn’t design to work this way.  If you get it working, great!

    Some tips:
    • DMZ only connects to a database when it first needs it (ie: the first time $object->db is referenced).
    • You can force a model to reload it’s configuration for the remainder of the request (and for any new models) with the new reinitialize_model method.
    • Remember that you’d have to go through this for every request.

    It’s too bad that you are being asked to develop it this way.  As long as the access to this information is available through the same application, there is absolutely no benefit to breaking it up into different databases.  It’s a waste of developer time, since it provides no extra security to the users that isn’t already available through normal authorization.  :down:

    Good Luck!

    —————

    Talk to you all soon!  😊

  • #108 / Mar 30, 2010 1:17am

    PoetaWD

    97 posts

    Hello Phil,

    Those are GREAT news man! I am very very happy for your success! You really deserve it!

    I am sure this company (can you tell us who they are?) is going to improve their prodution because they are hiring one of the best CI developers available in the market!

    I hope that they use DMZ in their products and you get paid to improve DMZ. I know that we dont give you anything back but please keep your awesome script free for the community to use.

    I know that I am new at PHP, but I do understand a little of the DMZ usage and I will try my bast to answer some of the questions sent here in the forum !

    PLEASE DONT ABANDON US ! We love you man ! (I sure do and I am not a homo !)

    DMZ made me really enjoy programming !

    Thanks for everything you´ve done !

  • #109 / Mar 30, 2010 1:38am

    OverZealous

    1030 posts

    @PoetaWD

    There’s no obvious reason not to tell you the company name, but knowing the company name isn’t really important, and it’s probably best (since I haven’t technically started yet) to keep that private for now.

    Thank you for your kind words!

    And to be clear, I have absolutely zero intention of abandoning the DMZ users, or the project at all.  (I’m actually planning on continuing work on ZestyJobs, as well.)  I probably will not have much access to the source code repository for my own projects for the first couple of weeks.

    For now, I have less than two weeks to do some significant work to my home and prepare for moving to the other city!  (It’s a 7-8 hour drive between cities, so every time I switch back and forth I’m exhausted.)  Once we are able to sell the house, and officially move, well, then things get interesting!

  • #110 / Mar 30, 2010 3:10am

    CEmy

    1 posts

    Hello,
    I have this error and i have upgraded mysql_driver.php to the new version from here: http://svn.ellislab.com/CodeIgniter/trunk/system/database/drivers/mysql/ ( the link from http://www.overzealous.com/dmz/pages/troubleshooting.html#Database.UnknownColumn dont work )

    A Database Error Occurred
    
    Error Number: 1054
    
    Unknown column 'ladder_games_players.dota_score_kd' in 'order clause'
    
    SELECT * FROM (`ladder_games`) ORDER BY `ladder_games_players`.`dota_score_kd` desc


    I have a table : ladder_games_players that have player_id, game_id and allot of more rows
    I have set a has_many relationship because every player have more games and every game have max 10 players.

    Here is the code :

    $this->games->order_by_join_field('player','dota_score_kd', "desc")->get();

    Sorry for my english and thx for your time.

  • #111 / Mar 30, 2010 12:46pm

    OverZealous

    1030 posts

    @CEmy

    I think the DB error is pretty clear: you are trying to sort on a column that does not exist.  Is your column mislabeled in your code or in the database?

    DMZ is generating exactly the code you requested.

  • #112 / Mar 30, 2010 3:58pm

    PoetaWD

    97 posts

    :D

    Great !  Please tell us when you got some news !

  • #113 / Mar 31, 2010 12:46am

    tomdelonge

    60 posts

    For some reason, I honestly can’t get my head around “self relationships”. I looked at the bug model in the example application, along with the model in the advanced relationships section of the DMZ userguide.

    I imagine that each category “has one” parent and “has many” children. I honestly don’t know what to setup for a table and how I would setup a query to find each category and it’s children.

    I don’t understand what to put for “other_field” and such. Quite frankly, I don’t really know how to even start with this.

    Any tips? (Like I said, I looked over the user guide, I just can’t figure this out)

  • #114 / Mar 31, 2010 1:04am

    OverZealous

    1030 posts

    @tomdelonge
    A self relationship is just a named relationship.  Do you understand how multiple relationships to the same object work?  IE: the creater/editor relationship from Bug to User?

    For self relationships, you are just relating one object to itself.

    Remember, all relationships have to be described from both sides.  other_field literally is just the name of the other (opposite) field in the relationship.  Notice in the example below how the relationship key and other_field are just swapped between the two definitions.

    Here’s some (untested) code:

    class Category extends DataMapper {
        $has_one = array(
            // NOTE: can't use the keyword 'parent' here, because it has meaning to DMZ.
            'parentcategory' => array(
                'class' => 'category',
                'other_field' => 'childcategory'
            )
        );
    
        $has_many = array(
            'childcategory' => array(
                'class' => 'category',
                'other_field' => 'parentcategory'
            )
        );
    }

    You only need the categories table:

    categories
    ---+-----------+------------------
    id | name      | parentcategory_id
    ---+-----------+------------------
    1  | ROOT      | NULL
    2  | Games     | 1
    3  | Work      | 1
    4  | Board     | 2

    Usage:

    $root = new Category(1);
    // create new category
    $cat = new Category();
    $cat->name = 'Private';
    // there are actually 3 ways to save the relationship.  See Save in the manual for other examples.
    $cat->save(array('parentcategory' => $root));
    
    // get root categories
    $cats = $root->childcategory->get();
    foreach($cats as $c) {
        echo($c->name . "\n");
        // recurse
        foreach($c->childcategory->get() as $sub) {
            echo("    - " . $sub->name . "\n");
        }
    }
    
    // delete a child category
    $root->delete_childcategory($cat);

    It’s really that simple, especially for $has_one self relationships.  Feel free to choose better (shorter) names.

    —————

    One more option, you can use a simpler relationship name on one side of the relationship, like so:

    $has_one = array('category' => array('other_field' => 'child'));
    $has_many = array('child' => array('class' => 'category', 'other_field' => 'category'));
    
    // table
    id | name | category_id
    
    // usage example
    $cat->save_category($root);

    But that can be confusing.

    Who says I don’t have time to answer questions? 😛

  • #115 / Mar 31, 2010 3:44am

    BaRzO

    105 posts

    NO one can’t say anything 😊

  • #116 / Mar 31, 2010 12:54pm

    Alface

    41 posts

    I Extend the DataMapper Directly for fix the validation rule “file_required”, because DMZ use the CI function for to it, and it can’t handdle it, and then I used an “Form Validation extention with File for Code Igniter”:
    Forum: http://ellislab.com/forums/viewthread/123816/
    Site: http://devbro.com/testing/ci_form_validation/


    And to do this I I extended the function validate with is not possible extend on DMZ 1.71 because it is private.

    I just changed line 1508 of application/libraries/datamapper.php

    if ( ! $related && ! in_array('required', $rules))
    if ( ! $related && ! in_array('required', $rules)&& ! in_array('file_required', $rules)) // Mod file_required ::


    I think we should implement it on this version of DMZ, dont you think?
    If don’t think so, how could I make DMZ use this mod and not CI native?

    Thanks

  • #117 / Mar 31, 2010 12:57pm

    OverZealous

    1030 posts

    @Alface

    Why did you extend DataMapper directly??  There are half a dozen supported ways to write your own validation methods, and none of them require direct modification.

    The simplest method is to add your own _file_require method to your model, but you can also easily add it to an extension.  Custom validation methods always override Form_validation methods.

    Please see the validation page in the manual for explicit examples.

  • #118 / Mar 31, 2010 1:10pm

    Alface

    41 posts

    I tried but it only handle $_POST fields, I need to validate with $_FILES
    >.<

  • #119 / Mar 31, 2010 1:24pm

    OverZealous

    1030 posts

    @Alface
    ❓ Custom validation functions can validate anything you want.  They are just methods that return TRUE or FALSE.  You can write anything you want for the method.

  • #120 / Mar 31, 2010 1:42pm

    Alface

    41 posts

    I know but, the function validation don’t handle custom error messagens by this functions..

    take a look:

    // Add an error message if the rule returned FALSE
                        if ($result === FALSE)
                        {
                            if(is_string($result))
                            {
                                $line = $result;
                            }
                            else if (FALSE === ($line = $this->lang->line($rule)))
                            {
                                // Get corresponding error from language file
                                $line = 'Unable to access an error message corresponding to your rule name: '.$rule.'.';
                            }

    I changed to:

    // Add an error message if the rule returned FALSE
                        if ($result === FALSE)
                        {
                            if(is_string($result))
                            {
                                $line = $result;
                            }
                            else if (FALSE === ($line = $this->lang->line($rule)))
                            {
                                if(empty($this->error->{$rule}) ){ // Mod customErro :: Se foi setado um erro custon, não mostrar erro
                                    // Get corresponding error from language file
                                    $line = 'Unable to access an error message corresponding to your rule name: '.$rule.'.';
                                } // Mod customErro :: end
                            }

    Am I wrong?

    How could I set a error message for an “Custom Validation Rule” ?

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

ExpressionEngine News!

#eecms, #events, #releases