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 - 05-08-2009

[eluser]camporter1[/eluser]
Hrm. That doesn't seem to work the correct way. It's returning the wrong ids. Maybe I can tinker around with it so that it does work right...

EDIT:

Ah, I think the second one won't work, because while it will check the one-way of our user, it doesn't check for that user's one-way-ness of other users. This is why I had in my previous post the script just grabbing all the associates so that it can check to make sure that the user's id wasn't being used as an associate for another user (which is essentially the way to check if a user is unconfirmed).


DataMapper 1.6.0 - El Forum - 05-08-2009

[eluser]camporter1[/eluser]
Here we go, this works much better Smile
Code:
$user->associate->where_related('associate', 'id', $user->id)->select('id')->get();
        $ids = array();
        foreach($user->associate->all as $a) {
              $ids[] = $a->id;
        }
        
$associate->where_related('associate', 'id', $user->id)->get();
        foreach ($associate->all as $a) {
            if (in_array($a->id, $ids)) {
                //do nothing
            } else {
                echo $a->id;
            }
        }



DataMapper 1.6.0 - El Forum - 05-18-2009

[eluser]oddman[/eluser]
Does anyone know how to do a mass-assigning of data in order to save a model? Ie. I want to assign all user data to the model and then just call save:

Code:
$u = new User($data_to_be_saved);
$u->save();

Is this possible?


DataMapper 1.6.0 - El Forum - 05-19-2009

[eluser]OverZealous[/eluser]
Nope. You can, however, do something simple like:
Code:
foreach($data as $k => $v) {
    $u->{$k} = $v;
}
$u->save();



DataMapper 1.6.0 - El Forum - 05-19-2009

[eluser]Matthew Lanham[/eluser]
Nice one Phil,

I have been looking for something similar as we used to always create forms with the names as the DB fields, so now we could have something like:

Code:
foreach($_POST as $k => $v) {
    $u->{$k} = $v;
}
$u->save();



DataMapper 1.6.0 - El Forum - 05-19-2009

[eluser]OverZealous[/eluser]
What ever you do, don't EVER use the code above!

Otherwise I could fabricate an HTTP POST that changes the id before a save, or something else, and edit the data on an item I might not have access to.

Always validate your $_POST input. If nothing else, do something like this:
Code:
$foreach($_POST as $k => $v) {
    if(in_array(array('name', 'value1', 'value2'), $k)) { // may be backwards
        $u->{$k} = $v;
    }
}



DataMapper 1.6.0 - El Forum - 05-19-2009

[eluser]Matthew Lanham[/eluser]
Well yes, sorry i didn't think my post through, it was a short example of how we actually do it, which involves some encryption and session based checks before any data is stored.


DataMapper 1.6.0 - El Forum - 05-19-2009

[eluser]oddman[/eluser]
Hi stensi,

I think datamapper is one of the best ORMs available for CI atm, and I'd like to be able to contribute to the project, as I believe there's a lot I could add (and forking it, I do not believe is beneficial for anyone Tongue). If not, that's fine - I can just do it for myself Tongue


DataMapper 1.6.0 - El Forum - 05-20-2009

[eluser]IamPrototype[/eluser]
I don't get it. What's so good about datamappers? What's its pros (...and cons)?


DataMapper 1.6.0 - El Forum - 05-20-2009

[eluser]OverZealous[/eluser]
@IamPrototype
First, I recommend reading stensi's original introduction. Look through the code examples, and you can see how an ORM can eliminate hard-coded SQL (a big mistake in any manageable application), and make your code easier to read.

It also automatically handles relationships between objects, including automatically deleting complex relationships. Some (maybe even much) of what DM can do can be duplicated with RDBMS configuration, but then you are splitting your application configuration between multiple sources.

Finally, DataMapper really increases the speed of development, by allowing you to look at your data as a series of inter-related objects, instead of unrelated tabular data.

If you are uncomfortable with OOP development, it may seem strange at first. After awhile, however, you will see how DM makes your code much easier to read, and it really helps when separating your application into a proper Model-View-Controller architecture.

Beyond that, read through this forum (and my extended DMZ forum) for examples and information.

(FYI: The only reason my version of DataMapper exists is that stensi has not been around since February [last I checked], and I have been trying to provide support, enhancements, and bugfixes. DMZ is fully backwards compatible with DataMapper.)