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]
  • #721 / Apr 21, 2009 12:00am

    warrennz

    56 posts

    Firstly, I apologize if this has already been addressed.

    I have an issue where data mapper is assuming my ‘address’ model name is plural and thusly using the inflector helper to determine the joining field name as ‘addres_id’.

    Can someone give some recommendations on solving this? I’m a little stumped. addres isnt a word and don’t really wanna go naming fields like that.

    Upload to show what I mean
    http://yfrog.com/02addresnotfoundj


    Cheers

  • #722 / Apr 21, 2009 12:28am

    OverZealous

    1030 posts

    @jscott
    Nope!  However, it’s trivial to write your own.  (Everyone’s application is going to be slightly different, enough so that any method used by DM is most likely going to be incorrect for your app.)

    One of the things I highly recommend is to create a subclass of DataMapper (e.g.: DataMapperExt), and have all of your classes extend that instead.  This way you can “extend” DataMapper without worrying about the original code.

    Then just include something like this:

    // warning, possibly very insecure, since anything can be passed in!
    function load_from_form() {
        foreach($this->fields as $field) {
            $CI =& get_instance();
            $v = $CI->input->post($field);
            if($v !== FALSE) {
                $this->{$field} = $v;
            }
        }
    }

    @warrennz
    To work around the pluralizer, you very simply define $table and (if necessary) $model in your class:

    class Address extends DataMapper {
        $model = 'address';
        $table = 'addresses';
        ... // rest of class
    }

    I think there is one other place it tries to automatically pluralize or make something singular, but I’ve never had a problem with it.

    (Also, I thought that got addressed 😉 in the latest version of DataMapper’s inflector helper.  Make sure you install that, as well.)

  • #723 / Apr 21, 2009 1:17am

    warrennz

    56 posts

    Hello

    @OverZealous.com
    Thanks for that seems to be working well. I believe I am using the latest version but will check again shortly.

  • #724 / Apr 21, 2009 1:24am

    cberk

    15 posts

    I’m using the original DataMapper (1.6.0), and have come across what appears to be a bug.

    I am updating an object, saving it, and then calling a custom function.  The function is designed to work with multiple objects, so it loops through the $object->all array.  The problem is: the $object->all array doesn’t contain the updated values; it still contains the old values from before I saved the object.  So it appears that the save function doesn’t update the $object->all array, and unfortunately it is a significant annoyance.

    I’m curious if you guys have any suggestions.  Here is an example of what I’m doing:

    From the controller:

    if ($c->save()) // Save budget
    {
        echo $c->budget() // Shows the new value, correctly
        $c->set_activation();
    }

    From the model:

    function set_activation()
        {
            foreach ($this->all as $client) // Loop through each of the client objects, if there is more than one.
            {
                echo $client->budget; // Shows the old value, which is wrong
  • #725 / Apr 21, 2009 1:52am

    warrennz

    56 posts

    Sorry if this is right in front of me, I’ve read through most of the documentation and searched the data mapper files and nothing stands out at me

    I was wondering there there’s a way ( a good way ) to manipulate the properties of a record as they’re created through the model.

    Eg, I have a users table with first and last name. I want to be able to call
    $u->fullname as doing $u->first_name.$u->last_name is a bit long winded. (example doest give full credit to the long winded-ness as I have title, first_name, middle_name, last_name etc etc that need to be used.)

    Is there any way/thing that could sit in the model called for example run_after_query() and manipulate the record, or each record if there are multiple, as they’re called/created etc

  • #726 / Apr 21, 2009 2:06am

    cberk

    15 posts

    I tend to resort to the query method when this comes up.  If you can get away with not having your calculated field all the time, you can get it when you need it with a SQL query.  It’s probably not any more concise to do it this way, however.

    http://stensi.com/datamapper/pages/query.html

  • #727 / Apr 21, 2009 2:12am

    naren_nag

    81 posts

    @warrennz

    For things like getting the fullname etc, I typically create a function in the model. For example, I have a table users with firstname, middlename and lastname.

    In my model user I have a method called name()

    function name()
    {
       $name = $this->firstname;
       if(strlen($this->middlename) > 0)
          $name .= " " . $this->middlename;
       $name = " " . $this->lastname;
    
       return $name;
    }

    So I just call this everytime I need the fullname.

    $u = new User();
    $u->get_by_id(1);
    echo $u->name(); // This gives me the full name of the person with user id 1

    cheers,

    Nag

  • #728 / Apr 21, 2009 8:19am

    warrennz

    56 posts

    Cheers guys I’ll see how I get on. 😊

  • #729 / Apr 21, 2009 11:19am

    OverZealous

    1030 posts

    @cberk
    ->save() only works on the current object.  It doesn’t have anything to do with the ->all array.  It also doesn’t modify any values, so I don’t understand what you are expecting it to do.

    If you need to update the ->all array, you’ll have to re-run your query.  There’s no way around that.  If it is a relationship, calling $parent->object->get() will reload the child objects.

  • #730 / Apr 21, 2009 2:39pm

    cberk

    15 posts

    That makes sense.  Thanks.

  • #731 / Apr 21, 2009 7:14pm

    warrennz

    56 posts

    Heya

    I have a strange problem

    With my current db design I have a ‘comments’ tables. This table stores the user_id (the user this comment is FOR) and the posted_id (this user who posted that comment). Both users originate from the same ‘users’ table and are done via the users model.

    Moving that to a linking table for datamapper I now have comments_users which I use to get a users comments ($u->comments->get()->all)

    How, if possible, can I define that 2nd relationship.. to the same table.. I have no idea how it can be done if at all.

    I would prefer to avoid making a 2nd ‘users2’ model if possible.

    Currently theres the users : comments relationship which is 1:M.  Then the posted user would be a 1:1 relationship with the same user table, e.g. comments->posteduser->get()

    Any ideas would be very appreciated 😊 Thanks so much. Im totally lost on this one haha

  • #732 / Apr 21, 2009 7:16pm

    OverZealous

    1030 posts

    Please look into my extended version of DataMapper, because the current version does not support multiple relationships with the same object.  I’m in the middle of something, but it is linked on this board, just a few pages back.  Look for “DMZ”.

    It also supports keeping 1-N relationships in the table as “user_id” and “posted_id”, which is convenient.

  • #733 / Apr 21, 2009 8:02pm

    warrennz

    56 posts

    I take it 1:N relationships with an individual id linking table will no longer work with DMZ?

  • #734 / Apr 21, 2009 8:05pm

    OverZealous

    1030 posts

    Nope.  Everything still works as is.  It’s a drop-in replacement.

  • #735 / Apr 21, 2009 8:31pm

    warrennz

    56 posts

    Hmm well I have no idea what was going on there but I renamed my model to comments from notes and everything works as expected now. Still using the same db tables naming etc.

    Oh well 😊 beats me

    Thanks

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

ExpressionEngine News!

#eecms, #events, #releases