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.

DataMapper 1.6.0

September 05, 2008 12:32pm

Subscribe [115]
  • #616 / Feb 27, 2009 9:40pm

    OverZealous

    1030 posts

    If you don’t want to edit the pluralizing code, you can simply hard code both the model and the table:

    $model = 'status';
    $table = 'statuses';

    This should fix everything else, unless you have a self-reference between ‘statuses’ (although, I think that works as well).  That’s the only other time that plural() is called, and singular() is only used on declaring the model.

  • #617 / Feb 28, 2009 11:03am

    anonymous65551

    222 posts

    Can we get the DataMapper user_guide in downloadable format?  I am sometimes developing while away from an internet connection to maximize my time, which is one reason I love CodeIgniter so much, the documentation can be local on my machine.  Also why I like DX Auth, it has the documentation downloadable.

  • #618 / Feb 28, 2009 11:26am

    OverZealous

    1030 posts

    Every copy of DataMapper (stensi’s) includes the User Guide.  If you have DM, you have the User’s Guide.

  • #619 / Mar 02, 2009 4:53am

    stefanv

    9 posts

    While I like your idea, it would break a lot of existing code that expects a string on the fields.  Why are you getting multiple errors per field?  I’m pretty sure DataMapper’s validation routine stops as soon as any field has an error.

    When i add multiple check on a field (like minlength, maxlenght and uniqiue) 1 get 3 errors, but the $this->error->{fieldname} only shows the latest error that was found.
    I want to show all errors and not only the latest one found.

    I agree it would break a lot of code, but i found it worth changing i my code.. Perhaps i wil leave the datamapper lib intact and create an extend of it. I can overrule the error function there!

  • #620 / Mar 02, 2009 5:07am

    OverZealous

    1030 posts

    That’s probably best for the short term.  I wonder if it makes more sense to stop the error checks after the first error (which would mean adding a ‘break;’ after an error has occurred.  (If someone needs to have multiple checks, they could combine them into one custom error.)

    Also, it would prevent the errors from checking after a known problem occurs, and also prevent potential PHP errors (ie: a custom error expects a non-empty value, but gets called even after ‘required’).

    Any thoughts?

  • #621 / Mar 02, 2009 5:45am

    stefanv

    9 posts

    Any thoughts?

    Perhaps the code:

    // Build up the error message
            $message = $this->error_prefix . $error . $this->error_suffix;
    
            // Set field specific error
            $this->error->{$field}[] = $message;
    
            // Add field error to errors all list
            $this->error->all[] = $message;
    
            // Append field error to error message string
            $this->error->string .= $message;

    should be replaced with:

    // Build up the error message
            $message = $this->error_prefix . $error . $this->error_suffix;
    
            // Set field specific error
            $this->error->{$field} .= $message;
    
            // Add field error to errors all list
            $this->error->all[] = $message;
    
            // Append field error to error message string
            $this->error->string .= $message;

    But then the error string is going the be real long… Some intelligence should be added, since the ‘fieldname’ isn’t needed for all the messages added.

    So:

    Username is a required field.
    Username should be at least 3 character long.

    should become:

    Username is a required field and should be at least 3 character long
  • #622 / Mar 02, 2009 6:27am

    OverZealous

    1030 posts

    I just don’t see a good reason to return multiple errors per field.  Once one error occurs, that should be all that is really needed to convey the message.

    Since the order you set the validation rules matters, if you put ‘required’ at the end, the required rule wouldn’t ever run until after the min_length rule, therefore you’d always get:

    "Username must be at least 3 characters long."

    Which not only conveys all of the information, but makes more sense.

    Also, there is no practical way DataMapper could combine error messages and still be language-agnostic.  I still say, if you need that kind of compiled information (“Field should be this and that and this too.”), either create a custom validation method and error message, or provide that information inline.

    Anyway, this is probably something for stensi or someone else to decide.  It’s easy enough to customize if that is what is desired.

  • #623 / Mar 02, 2009 6:44am

    stefanv

    9 posts

    I just don’t see a good reason to return multiple errors per field.  Once one error occurs, that should be all that is really needed to convey the message.

    Since the order you set the validation rules matters, if you put ‘required’ at the end, the required rule wouldn’t ever run until after the min_length rule, therefore you’d always get:

    Looking at your reply and (re)thinking what I stated.. You are probably right. There should only be 1 error per field: the must important one (the first one in the validation rule)

    The statement ‘the validation rules order’ triggered me to look at my code again.. The order was wrong.. fixed it..

    This fix solved my problem.

    1 thing i would like to add.. ->string and ->all both represent all the errors found.. Wouldn’t it be nice to add something like ->current the get a list of the current errors (the limited list)

  • #624 / Mar 02, 2009 9:07am

    OverZealous

    1030 posts

    1 thing i would like to add.. ->string and ->all both represent all the errors found.. Wouldn’t it be nice to add something like ->current the get a list of the current errors (the limited list)

    I’m confused by this.  From the guide:

    $obj->error->all; // all errors from last validation as an array
    $obj->error->string; // all errors from last validation as a string
    $obj->error->{$field}; // the last validation error for $field.

    What else do you want?  The errors listed are all of the errors for this transaction.  If you see an error in this list, it happened now.  There is one minor known bug, that $obj->error->transaction doesn’t get initially set or cleared.  This only occurs if you are using auto transactions, AND you get a transaction error.

  • #625 / Mar 02, 2009 2:27pm

    Matthew Pennell

    221 posts

    Someone might have already pointed this out, but just in case - the download links on the DataMapper User Guide are broken.

  • #626 / Mar 02, 2009 6:09pm

    Billy Patel

    284 posts

    Yep, really would like to try this but the download links are broken.

  • #627 / Mar 02, 2009 6:13pm

    OverZealous

    1030 posts

    Check the first page of this thread.  The latest official version of DataMapper is there, including the help documentation.  You do not need to download it from stensi’s website.

  • #628 / Mar 03, 2009 7:34pm

    tdktank59

    322 posts

    I came up with a neat function for deleting single or multiple relationships while editing a record.

    For instance,

    Brainstorm
    -> id
    -> name
    -> description
    -> author_id
    -> data_source_1_id
    -> data_source_2_id

    DS1 and DS2 cannot be the same and can both be nulled if only 1 source is provided.

    However while updating I dont belive you can delete then save… (not sure here and havnt tried…)
    But this will search the master object, grab the id of the field you want to delete, create the object of the relation and delete it all for you!

    Anyways heres the fun little method I created (just tacked it into the brainstorm model
    Sorry for the somewhat confusing nature of the variables lol

    /**
     * Deleted the relation between the master object
     * and its children.
     *
     * @param varchar $master_obj the name master object loosing the relation(s)
     * @param varchar $master_key the column name to get the master object
     * @param varchar $master_value the value for the column name
     * @param array $remove_objs The relations to be deleted in format ID => array ($class => $coloum_name)
     */
    function delete_relation($master_obj,$master_key,$master_value,$remove_objs)
    {
        $mast_obj = new $master_obj();
        $mast_obj->where($master_key,$master_value)->get();
    
        // echo 'Master Object: '.$master_obj.' ( '.$master_key.' => '.$master_value.' )<br>';
    
        foreach ($remove_objs as $remove)
        {
            foreach ($remove as $key => $val)
            {
                //echo key($remove).' => '.$key.' => '.$val.'
    ';
    
                $mast_obj->$val->get();
    
                $obj = new $key();
                $obj->where('id',$mast_obj->$val->id)->get();
                $delete[$val] = $obj;
            }
        }
    
        $mast_obj->delete($delete);
    }

    Then in the controller to delete the records

    set an array such as (you need the same format)
    the $array[] makes it so you can have multiples of the same (so I could delete DS1 and DS2 at the same time) You can fill this in with a value it wont matter… just as long as the array under it has the $class => $field

    $delete[] = array ($class => $field);

    and then right before you save call the delete method as such

    // Delete any nulled relations
    if (isset($delete)) $b->delete_relation($master_class,$master_key,$master_value,$delete);

    Simple as that let me know if there are any questions.
    BTW I tested and this method works.

  • #629 / Mar 03, 2009 10:06pm

    OverZealous

    1030 posts

    I’m confused as to what this method does.  It seems like a lot of complexity, but all it does is delete a set of related objects?
    Why would you use this instead of:

    $master->delete(array(
        'data_source_1' => $ds1
        'data_source_1' => $ds2
    ));

    Or, if you know you are using the has_one fields on the same table:

    $master->data_source_1_id = NULL;
    $master->data_source_2_id = NULL;
    $master->save();

    I’m just not sure what the method does.  Also, you query $mast_obj in a way that might return multiple values, however, you only call $mast_obj->delete() on the first returned value, possibly leading to unexpected results.

    Finally, if you just want a simpler method to delete a set of related objects, why not this:

    function delete_related_fields($fields) {
        if(!is_array($fields)) {
            // allow for single fields
            $fields = array($fields);
        }
        $success = array();
        foreach($fields as $f) {
            $this->{$f}->get();
            // This should work, even if the field is different than the model, due to the way the delete method is designed.
            $success[] = $this->delete($this->{$f}->all, $f);
        }
        return !in_array(FALSE, $success);
    }

    Usage:

    $my_obj->delete_related_fields(array('data_source_1', 'data_source_2'));

    This also allows for deleting all related has_many ones.

    —-

    On a related note, I’m trying to get my project to a private beta by this weekend.  If everything goes as planned, I hope to set some time aside to build up some examples and better document my extended DataMapper.

  • #630 / Mar 06, 2009 9:33am

    Billy Patel

    284 posts

    I am new to datamapper and need to do this…

    I have three tables, regions, members and join_members_regions.

    After i have downloaded all data into a csv, i need to truncate the members and join_members_regions tables. From reading the help pages, i know i can do delete_all or delete but this requires a specific member record, ho do i delete all from both?

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

ExpressionEngine News!

#eecms, #events, #releases