CodeIgniter Forums
DataMapper 1.6.0 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: DataMapper 1.6.0 (/showthread.php?tid=11358)



DataMapper 1.6.0 - El Forum - 04-20-2009

[eluser]warrennz[/eluser]
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


DataMapper 1.6.0 - El Forum - 04-20-2009

[eluser]OverZealous[/eluser]
@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:
Code:
// 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:
Code:
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.)


DataMapper 1.6.0 - El Forum - 04-20-2009

[eluser]warrennz[/eluser]
Hello

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


DataMapper 1.6.0 - El Forum - 04-20-2009

[eluser]cberk[/eluser]
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:
Code:
if ($c->save()) // Save budget
{
    echo $c->budget() // Shows the new value, correctly
    $c->set_activation();
}

From the model:
Code:
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



DataMapper 1.6.0 - El Forum - 04-20-2009

[eluser]warrennz[/eluser]
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


DataMapper 1.6.0 - El Forum - 04-20-2009

[eluser]cberk[/eluser]
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


DataMapper 1.6.0 - El Forum - 04-20-2009

[eluser]naren_nag[/eluser]
@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()

Code:
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.

Code:
$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


DataMapper 1.6.0 - El Forum - 04-21-2009

[eluser]warrennz[/eluser]
Cheers guys I'll see how I get on. Smile


DataMapper 1.6.0 - El Forum - 04-21-2009

[eluser]OverZealous[/eluser]
@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.


DataMapper 1.6.0 - El Forum - 04-21-2009

[eluser]cberk[/eluser]
That makes sense. Thanks.