CodeIgniter Forums
DataMapper ORM v1.8.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 ORM v1.8.0 (/showthread.php?tid=37531)



DataMapper ORM v1.8.0 - El Forum - 03-18-2011

[eluser]WanWizard[/eluser]
Just for my understanding, this is in the controller
Code:
$user = new User($this->user_id);
$this->page_data['request'] = $user->getRelationships(0, 0);
$this->page_data['connections'] = $user->getRelationships(1, 0);

and this is in the model?
Code:
function getRelationships($approved = TRUE, $offset = 0)
    {
        return $this->relateduser
                ->include_join_fields()
                ->where('approved', $approved)
                ->include_related('profile', $this->related_fields)
                ->get();
    }

You're running the method both times on the same object ($user), and your method is using $this, so yes, the second call on the same object will modify the contents of that object.


DataMapper ORM v1.8.0 - El Forum - 03-18-2011

[eluser]Jean Brodeur[/eluser]
Hi all!
I'm taking back a project that i start last year with CI. I look at the new CI version to keep the project ruunning with a fresh release of the framework and found Datamapper.

My project models are using active record right now, so i want to upgrade the project to use the Objects approach instead.

To test datamapper, i setup CI 2.0 with DM 1.8, create 2 tables(customers and notes) with a joint-table(customers_notes) and add some entry.

When i use this, i cant get the notes related to customer(3) :

Code:
$cus = new Customer();
$cus->where('id',3)->get();
$cus->note->get();

What i've done is delete all entry made by hand in the joint-table and then write this code :
Code:
$cus = new Customer();
$cus->where('id',3)->get();
$not = new Note();
$not->get();
$cus->save($not->all);

Now the relationship between customer(3) and all notes work and the joint-table has been populated. Now the first code work perfectly.

What magic does DataMapper did?


DataMapper ORM v1.8.0 - El Forum - 03-19-2011

[eluser]WanWizard[/eluser]
I have to guess what your models look like, but from the information given, you have created a many-to-many relationship between customers and notes. You create such a relation by using a junction or relationship table (customers_notes), which contains the primary keys of both tables in the relationship (which in Datamapper must be called 'id').

The line
Code:
$cus->save($not->all);
saves the relationship between the currently loaded customer (in the $cus object) and all retrieved notes (all objects in the $not->all array). It does that by inserting records in the relationship table, with $cus->id, and all id values of the retrieved notes.

Nothing magical, standard database design, and how you whould have to implement it by hand if you use active record.

When you now use
Code:
$cus->note->get();
Datamapper will generate a join on the relationship table and the notes table, selecting the records in the relationship table whose customer_id equals $cus->id.

If you want to know what happens behind the scenes, use check_last_query() after a Datamapper command, and analyse the SQL to see what exactly happens.


DataMapper ORM v1.8.0 - El Forum - 03-19-2011

[eluser]Madmartigan1[/eluser]
I have a question about this:

"Unlike CI models, you should never load them explicitly."

I ran into an issue when constructing DM objects from within a library (using libraries path), in a module (using wiredesignz's MX). The issue was resolved when I moved the library into APPPATH/libraries.

My question is: What kind of penalties are involved with loading a model explicitly?
And also: Is there a preferred workaround?

I know the module paths stuff is a pain. I was severely disappointed when I learned that CI 2.0 would not provide support for modules. Instead we got Package paths. But anyways...

I have NOT checked out WanWizard's Modular CI. From the intro, it looks like it is designed to be friendly with Datamapper. Can someone confirm?

EDIT: Not sure that moving the lib into APPPATH actually helped.. which kind of changes the tone of this post. I think my app was tricking me!

EDIT: Same situation with helpers, how do you access a DM model without loading the model explicitly?


DataMapper ORM v1.8.0 - El Forum - 03-19-2011

[eluser]WanWizard[/eluser]
Datamapper uses CI's $_model_paths array in the loader class (originally introduced for package support) to locate models to load.

Both Modular CI and MX add a path to this array for every module loaded. In Modular CI you can load modules manually, without calling them (and thus add the path). In MX you will have to manually call $this->load->_add_module_paths('modulename') to add a module without calling it (which according to the underscore is not supposed to be public). After this, Datamapper can find and load models from the module's model directory.

With CI 2.x (and Reactor), there is no penalty anymore, since all CI object assignments are now assigned by magic methods in CI_Model. With CI 1.x, using CI's method of loading a model would destroy Datamappers database connection (due to $this->db being overwritten). You are only left with a $this->modelname, which is useless in a Datamapper environment.


DataMapper ORM v1.8.0 - El Forum - 03-19-2011

[eluser]Madmartigan1[/eluser]
[quote author="WanWizard" date="1300570289"]
With CI 2.x (and Reactor), there is no penalty anymore, since all CI object assignments are now assigned by magic methods in CI_Model. With CI 1.x, using CI's method of loading a model would destroy Datamappers database connection (due to $this->db being overwritten). You are only left with a $this->modelname, which is useless in a Datamapper environment.[/quote]

Well, this is good enough for me.
I didn't notice any funny business from ->loading the models, but I just wasn't sure if there might be something uncatchable that could actually affect the data.

Thanks a lot for the response. Smile


DataMapper ORM v1.8.0 - El Forum - 03-19-2011

[eluser]Basketcasesoftware[/eluser]
[quote author="WanWizard" date="1300502700"]@Kestutis S.,

Sorry, currently not. Datamapper used 'id' hardcoded to identify the primary key. It is on the roadmap to change this, but that's not going to happen in the immediate future due to other commitments.[/quote]

Actually, you can go in by hand and change all references in DataMapper from 'id' to 'whateveryou' need. At least least that would work in theory. Like WanWizard, I'm neck deep in current projects or I'd take his kind offer and do a code fork on the framework. Smile Add a config item. Add an additional element to the models. Update the docs. Much fun. :-P


DataMapper ORM v1.8.0 - El Forum - 03-19-2011

[eluser]WanWizard[/eluser]
The change to make the primary key configurable is already in the works, although it might not make the next version...


DataMapper ORM v1.8.0 - El Forum - 03-19-2011

[eluser]Basketcasesoftware[/eluser]
Kind of figured that from your previous reply to this thread. Of course now you have me curious what the next version will contain. Getting ready to use DM in this current paying project. Makes me wonder if there will be any additions that might be useful to me. Of course my goal is get the site up and running by the end of the month so I doubt I'll get the chance. Client is offering me a new PC as part of the payment package. Yaaaaaay! <insert picture of Kermit the Frog doing his trademark yell here>


DataMapper ORM v1.8.0 - El Forum - 03-19-2011

[eluser]WanWizard[/eluser]
Next version will be a minor upgrade (v1.8.1), mainly bugfixes and some minor new features.

You can check the progress on http://bitbucket.org/wanwizard/datamapper, I try to have the docs updated at the same time I commit the code.