CodeIgniter Forums
DataMapper ORM v1.8.2 - 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.2 (/showthread.php?tid=47243)



DataMapper ORM v1.8.2 - El Forum - 08-10-2012

[eluser]Mark Price[/eluser]
[quote author="WanWizard" date="1344589781"]Thanks for the feedback. I've never used MX, but I try to support it as many people do.

I find this a bit of an odd fix.

The purpose of this line is to see if MX_Lang is loaded. The "false" just makes sure the DM autoloader isn't triggered. CI itself doesn't have an autoloader, so either MX is loaded before Datamapper, and then this line works, or MX is loaded after Datamapper, which will never work.

My guess is that you haven't loaded MX before you load Datamapper, and that removing the parameter will activate the DM autoloader, which will now load the MX_Lang library for you (as it only filters CI and EE libraries).

So the proper solution is: make sure you have MX active before you load Datamapper.[/quote]

I did some more digging on this and discovered that MX loads the MX_Lang class with the CI_Loader. Unfortunately, CI_Lang and DM_Lang get loaded right before this.

The fix I came up with for now is to create a MY_Lang class in my core directory that that includes MX_Lang so that it will load when the CI_Lang class loads:
Code:
<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

/* load the MX_Lang class */
require APPPATH."third_party/MX/Lang.php";

class MY_Lang extends MX_Lang {}



DataMapper ORM v1.8.2 - El Forum - 08-10-2012

[eluser]WanWizard[/eluser]
Hmm... That doesn't deserve a prize either.

Maybe (mis)using the DM autoloader like you first suggested is a better/cleaner workaround.


DataMapper ORM v1.8.2 - El Forum - 08-10-2012

[eluser]Mark Price[/eluser]
Yes, but I think that was just a fluke. I'm not even sure where that autoloader is that was including the MX_Lang. I think the main reason the developer of HMVC included the MX_Lang in the MX_Loader was to cut down on the number of MY_ core files that you had to have.


DataMapper ORM v1.8.2 - El Forum - 08-10-2012

[eluser]Mark Price[/eluser]
What is the reasoning for adding $object->clear(); in the from_array() function? I am seeing that it clears the object's id and is inserting a new record rather than updating it.

Thanks WanWizard for all your help.


DataMapper ORM v1.8.2 - El Forum - 08-11-2012

[eluser]WanWizard[/eluser]
Datamapper's autoloader can autoload any class in the libraries or models folders, in system, application and 3rd party, as long as they don't start with CI_ or EE_. The autoloader is in the Datamapper class, a method called autoload().

Not sure why from_array() would clear the object, I'm pretty sure that wasn't in there before (as I used it to update objects from $_POST). Perhaps it sneaked in with the all_from_array() changes, I'll have a look.


DataMapper ORM v1.8.2 - El Forum - 08-12-2012

[eluser]Unknown[/eluser]
It is really a special one. I like the way you have presented the information. Interesting post. Thanks for sharing.

[url="http://www.wiks.com.au/breathalyser"]breath test[/url]


DataMapper ORM v1.8.2 - El Forum - 08-14-2012

[eluser]Maglok[/eluser]
@WanWizard: Thank you that totally worked. Smile I now understand that sort of relationship a lot better.

At work here we have been actually using the library in some of our projects, but I just ran across another thing I don't quite know how to model with datamapper.

Object Person has 0 or more Object Visit. A visit has one Person.

I think this should turn into a database schema sorta like this

table Person
- id
- etc.

table Visit
- id
- person_id
- date_of_visit
- etc.

Now I need person to have a collection of visits so I can do $person->visits and just access the Visit objects. For some gawd aweful reason this seems to elude our entire team (3 people). I have a feeling we are very close here.


DataMapper ORM v1.8.2 - El Forum - 08-14-2012

[eluser]WanWizard[/eluser]
So what exactly is the issue, because your approach is spot on?


DataMapper ORM v1.8.2 - El Forum - 08-14-2012

[eluser]Jacob F.[/eluser]
I've got a table materials with fields named publisher_id & admin_id. These fields both reference `users`.`id` (so I can't name them both user_id). DataMapper seems to be convinced there is a relational table (there isn't).

My models look like this:
Code:
class Material extends DataMapper {

var $has_one = array(
  "user" => array(
   "class" => "user",
   "join_self_as" => "admin"
  )
);

}//Material{}
Code:
class User extends DataMapper {

var $has_many = array(
  "material" => array(
   "class" => "material"
  )
);

}//User{}

I've tried every combination of join_self_as, join_field, and join_other_as and I can't get it to accept there is a 1:many relationship :/ How do I tell DataMapper that user_id is named admin_id & publisher_id and that there is a 1:many (I would have expected using $has_many and $has_one to be the end-all-be-all in dictating the relationship type).

On a related note, in the documentation, if you could provide examples of the tables to which you reference (like on the Advanced Relationships page) that would be immensely helpful so we don't have to imagine what tables looks like). Also, it gets a little confusing when 3-4 different things are named "book".

Thanks!


DataMapper ORM v1.8.2 - El Forum - 08-15-2012

[eluser]WanWizard[/eluser]
There's a bit more info on advanced relations here, with a discussion I think is relevant in this case.

One of the issues is that Datamapper always needs both sides of the relation, they have to be symmetrical. So the user model needs two has_many definitions, that have to point back to the admin and publisher definitions on the materials model. If those are missing several types of query will fail.

So instead of the current 'material' relation you need the relation 'publishes' (which links to the publisher_id relation) and 'administers' (which links to the admin_id relation).

I'll see if I can come of with a more elaborate example for the docs.