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]
  • #241 / May 13, 2010 7:54am

    OverZealous

    1030 posts

    @tomdelonge

    Are you still using the ->all array?  You can’t use that with get_paged_iterated.  Check the examples on that page to see how you should loop over the object directly.

    From 1.7 onward, the recommended way to iterate a DMZ result is to loop over the object directly (it will know what you want), and to get the number of results from a query is to use result_count().

    (This is noted in the big yellow box on the get_iterated page.)

  • #242 / May 13, 2010 11:46am

    Spir

    139 posts

    I was wondering if I can do this :

    $object->object->get_where(('field'=>'value'));

    I have done this :

    if ($object1->object2->get_where(('field'=>'value'))->count>0)
    {
        foreach ($object1->object2 as $object2)
        {
            ...
        }
    }

    But the if pass even if no object2 are in the case of the filed requested.
    But it doesn’t loop on those element tho.

    check_last_query says :

    SELECT COUNT(*) AS `numrows`
    FROM (`object2`)
    WHERE `object1_id` = <id>
  • #243 / May 13, 2010 12:35pm

    OverZealous

    1030 posts

    @Spir
    What are you trying to accomplish?  You’ve said what you’ve done, but not what the goal is.

  • #244 / May 13, 2010 12:58pm

    tomdelonge

    60 posts

    @OverZealous

    I can’t believe I missed that. Thanks, now it’s working.

  • #245 / May 13, 2010 1:06pm

    Spir

    139 posts

    @Spir
    What are you trying to accomplish?  You’ve said what you’ve done, but not what the goal is.

    I’m actually trying to filter on the relationship between object1 and object2.

    Object1 has many Object2. I’m trying to get Object1’s object2, and I only want to get specifi object2 that meet the filter I’ve set in get_where.

    Also I could have done this:

    $o1 = new Object1(123); // passing id
    
    $o2 = new Object2();
    $o2->where('field', $some_value);
    $o2->where('object1_id', $o1->id)->get();

    I would prefer to have a few line. And was thinking about this:

    $object1 = new Object1(123);
    $object1->object2->get_where(('field'=>'value'))
  • #246 / May 13, 2010 2:31pm

    OverZealous

    1030 posts

    @Spir
    I just noticed you had ->count() at the end of your query.  Check the manual: count() runs a different query!

    The last query you wrote above would work.  To see how many results, use result_count().  This is also in the manual.

  • #247 / May 13, 2010 5:09pm

    Spir

    139 posts

    @Spir
    I just noticed you had ->count() at the end of your query.  Check the manual: count() runs a different query!

    The last query you wrote above would work.  To see how many results, use result_count().  This is also in the manual.

    My bad. Thanks for your time.

  • #248 / May 14, 2010 12:17am

    Buso

    297 posts

    I have this simple problem but I have no idea how to deal with it:

    Im used to codeigniter’s template parser, so I need my results in an array of arrays, so I can do something like:

    {users}
    User name:{name}
    {/users}

    What can I do about this? Datamapper results are arrays of objects.

    I like objects, but cant use them in the templates. Do you know by chance an enhanced template parser which can deal with objects? Or a way to turn datamapper array of objects into array of arrays ?

  • #249 / May 14, 2010 2:12am

    NachoF

    171 posts

    Im trying to figure out what is the correct order of doing this when I want to save multiple things at the same time

    For example.

    I have Projects. Each project has many Phases. Each Phase has one State (Florida, Texas, etc)

    In my form I have the data for a New Project as well as the FIRST phase of the given project so.

    this definitely shouldnt work, right?

    if($project->save(array($phase, $state)))
                        {
                                redirect("Success Screen");
                        }

    How should I get this done?.. keep in my mind, Its ONE form so if something from validation fails it shouldnt save anything at all

  • #250 / May 14, 2010 7:49am

    OverZealous

    1030 posts

    @Buso
    Search for arrays in the manual.  The first result is your answer.  😉


    NachoF
    No, your example won’t work, because $phase and $state would need to be saved separately.  All new objects must be saved before the relationships are saved.  After that, it doesn’t matter what order things are saved in (it’s the same number of queries).  😊

    If you are saving multiple objects and/or multiple relationships, you should wrap everything in a transaction.  (Auto transactions will not help.)

  • #251 / May 14, 2010 9:51am

    housecor

    6 posts

    For a given datamapper model, how should fields that are required on some forms and not required/used on others be handled? My idea was to pass a validation array into the constructor, but would love to hear how others are solving this.

    For example, my User model has a required password field in the validation array. I’d like to use the same user model elsewhere on an abbreviated form that only requires a few user related fields and doesn’t require a password.

  • #252 / May 14, 2010 9:54am

    Spir

    139 posts

    For a given datamapper model, how should fields that are required on some forms and not required/used on others be handled? My idea was to pass a validation array into the constructor, but would love to hear how others are solving this.

    For example, my User model has a required password field in the validation array. I’d like to use the same user model elsewhere on an abbreviated form that only requires a few user related fields and doesn’t require a password.

    Maybe you can pass a function to your validation and in that function make some tests?

    Imagine you have a var in your class that tells whether to check or not the value. That function will test the var and if it’s set to TRUE for instance it will ask for a data.

    Not sure I’m clear.

    Here is an example (quick and dirty) :

    class Object extends DataMapper {
    
        var $validation = array('field' => Array('rules' => array('trim', '_check_value')));
        var $doWeCheck_field = TRUE;
    
        /**
         * Validation function. Checks field value is in the list
         * 
         * @access private
         * @param  string : field name
         * @return bool
         */
        function _check_value($field){
            if ($this->doWeCheck_field) {
                # check value
            } else {
                return TRUE;
            }
            return FALSE;
        }
    
        function set_check_field($bool=FALSE){
            $this->doWeCheck_field=$bool;
        }
    }

    in controller:

    $o = new Object();
    $o->set_check_field(TRUE); // field will be required
    $o->set_check_field(FALSE); // field will not be required
  • #253 / May 14, 2010 10:44am

    housecor

    6 posts

    Interesting approach, thanks for the reply! I’m currently going a different route. I’m setting all the “universal” fields in the standard datamapper fashion via $validation. Then, I’ve setup the constructor of my datamapper model to accept a single $form_type parameter. Based on that parameter, I know what additional fields should be validated.

    class User extends DataMapper {
        //Note: These are default validation rules. Additional rules are set based on the form type
        //passed into the constructor.
        var $validation = array(
            array('field' => 'prefix', 'label' => 'Prefix', 'rules' => array('required', 'trim', 'max_length' => 10)),
            array('field' => 'first_name', 'label' => 'First Name', 'rules' => array('required', 'trim', 'max_length' => 200)),
            array('field' => 'last_name', 'label' => 'Last Name', 'rules' => array('required', 'trim', 'max_length' => 200)),
            array('field' => 'account_id', 'label' => 'Account ID',    'rules' => array('integer', 'max_length' => 6)),
            array('field' => 'email', 'label' => 'Email Address', 'rules' => array('trim', 'unique', 'valid_email')),
            array('field' => 'company',    'label' => 'Company', 'rules' => array('required', 'trim', 'max_length' => 200)),
            array('field' => 'job_title', 'label' => 'Job Title', 'rules' => array('required', 'trim', 'max_length' => 200))
        );
    
        /**
         * Constructor
         * @param string form_type - Type of form being processed.
         */
        function __construct($form_type = null) {
            $this->set_custom_validation($form_type);
            parent::__construct();
        }
    
        /**
         * Sets custom validation fields based on the form_type passed.
         * Useful so fields that are only required/used on certain forms
         * aren't required on every form dealing with user data. 
         * NOTE: This doesn't overwrite the default validation
         * set in the $validation property above, it merely adds
         * addtional fields to validate.
         * @return 
         */    
        function set_custom_validation($form_type) {
            switch ($form_type) {
                case 'register':
                    $this->validation[] = array('field' => 'password', 'label' => 'Password',    'rules' => array('required', 'trim', 'min_length' => 6, 'max_length' => 40, 'hash'));
                    break;
                case 'password_reset':
                    $this->validation[] = array('field' => 'password_reset_token', 'label' => 'Password Reset Token',    'rules' => array('max_length' => 32));
                    break;
            }
        }

    So as you can see, the password is only required on the registration form. And the password reset token is only processed on the password reset page.

    I’m pretty happy with this setup but also considered simply passing additional validation fields directly to the constructor rather than passing a form type.

  • #254 / May 14, 2010 10:51am

    Spir

    139 posts

    Sounds good. You should consider a default to your switch in case $form_type is null (since you allow null in the constructor).

  • #255 / May 14, 2010 11:40am

    NachoF

    171 posts

    Ok, so I have changed it to do this.

    $project->trans_begin();
                    if($project->save())
                        {
                                if($phase->save(array($project,$state)))
                                {
                               $project->trans_commit();
                                redirect("Success!");
    
                                }
                                else
                                    $project->trans_rollback();
    
    
                        }
                        else
                            $phase->validate();//so that the form will also display validation errors from phase

    It seems to be working… any advice is welcome though.

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

ExpressionEngine News!

#eecms, #events, #releases