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]Mike Chojnacki[/eluser]
Having a bit of trouble understanding the relationships using datamapper.

As an example, let's say we have this structure:

Table 'user':
id | username

Table 'group':
id | group_name

Table 'group_user:
id | user_id | group_id

Table 'group_method':
id | group_id | method_id

Table 'method'
id | method_name | object_id

Table 'object'
id | object_name

Imagine that every user is a part of a group, but can be part of many groups.

Every group has access to certain methods which are children of a object.

If I understand relationships correctly;
'user' has many 'group'
'group' has many 'user', 'method'
'method' has many 'group', has one 'object'
'object' has many 'method'
(If I'm mistaken let me know)

I've been studying the manual on relationships and advanced get functions, but there is one thing I just can't get to work on my own;

Code:
$u = new user();
$u->where('username', 'foo')->get(); // gets the id of the user where username equals 'foo'

With autoloading related objects on, one can do something like;
Code:
$u->group->method->get(); // returns a method which matches the user id in a group the user is a part of and also returns the object id of that method

This is all fine and dandy, until you get to a point where it looks like this;
Code:
echo $u->username.'<br/>';
echo $u->group_name.'<br/>';
echo $u->group->method->method_name.'<br/>';
echo $u->group->method->object_id.'<br/>';

Not only is the syntax very long, it also loads a new datamapper object for every table you access, var_dumping $u results in a few hundred lines of code that just repeats itself.

I've been trying all sorts of combinations of advanced get query builds, but can't seem to get a result like this;
Code:
$u->where('username', 'foo')->get_all_related_tables_and_fields_of_those_in_the_current_relationship_structure;

The code above would, put simply, merge:
Code:
echo $u->username.'<br/>';
echo $u->group_name.'<br/>';
echo $u->group->method->method_name.'<br/>';
echo $u->group->method->object_id.'<br/>';
into $u, making it possible to do:
Code:
echo $u->username.'<br/>';
echo $u->group_name.'<br/>'; // array if several are found
echo $u->method_name.'<br/>'; // array if several are found
echo $u->object_id.'<br/>';

I guess what I really want to know is:
Is this somehow supported in the library with a combination of where_related or other functions which I just don't understand fully, or should I write my own helper that does this automatically for me?


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

[eluser]Madmartigan1[/eluser]
[quote author="WanWizard" date="1300463041"]@Madmartigan1,

Thanks for the kudos. Feedback like this makes me forget all the long nights I've worked on it... Smile

(and don't forget all the work my predecessors have spend on it as well. Thanks go to them too!)[/quote]

That's why I carefully chose the word "maintaining" Smile

I remember seeing DMZ a while back but it always looked intimidating, in the sense that I didn't want to rewrite all my models. FWIW, I'm also using DM with wiredesignz's MX package - I had noticed some concerns from other users about this combo but I haven't really had an issue. The only issue I did notice was that it does not autoload the validation label lang files from module paths.

Hey since I'm error reporting now, I also noticed that "get" rules are not used if you assign them in the model __construct.

Once again, great job *everyone*!


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

[eluser]IgnitedCoder[/eluser]
@WanWizard,

As always thanks for saving me tons of SQL coding time... Smile

Can't see why this is... both request/connections are returning the same results, even though the sql is

Code:
SELECT `users`.*, `profiles`.`id` AS profile_id, `profiles`.`job_title` AS profile_job_title, `profiles`.`company` AS profile_company, `profiles`.`country` AS profile_country, `profiles`.`region` AS profile_region, `profiles`.`postal_code` AS profile_postal_code, `profiles`.`industry` AS profile_industry, `profiles`.`current_status` AS profile_current_status, `user_relatedusers_users`.`id` AS join_id, `user_relatedusers_users`.`type` AS join_type, `user_relatedusers_users`.`approved` AS join_approved, `user_relatedusers_users`.`subtype` AS join_subtype, `user_relatedusers_users`.`sent_on` AS join_sent_on
FROM (`users`)
LEFT OUTER JOIN `profiles` profiles ON `users`.`id` = `profiles`.`user_id`
LEFT OUTER JOIN `relatedusers_users` user_relatedusers_users ON `users`.`id` = `user_relatedusers_users`.`relateduser_id`
WHERE (
`approved` = 0
)
AND `user_relatedusers_users`.`user_id` = 4

SELECT `users`.*, `profiles`.`id` AS profile_id, `profiles`.`job_title` AS profile_job_title, `profiles`.`company` AS profile_company, `profiles`.`country` AS profile_country, `profiles`.`region` AS profile_region, `profiles`.`postal_code` AS profile_postal_code, `profiles`.`industry` AS profile_industry, `profiles`.`current_status` AS profile_current_status, `user_relatedusers_users`.`id` AS join_id, `user_relatedusers_users`.`type` AS join_type, `user_relatedusers_users`.`approved` AS join_approved, `user_relatedusers_users`.`subtype` AS join_subtype, `user_relatedusers_users`.`sent_on` AS join_sent_on
FROM (`users`)
LEFT OUTER JOIN `profiles` profiles ON `users`.`id` = `profiles`.`user_id`
LEFT OUTER JOIN `relatedusers_users` user_relatedusers_users ON `users`.`id` = `user_relatedusers_users`.`relateduser_id`
WHERE (
`approved` = 1
)
AND `user_relatedusers_users`.`user_id` = 4



Code:
$user = new User($this->user_id);
$this->page_data['request'] = $user->getRelationships(0, 0);
$this->page_data['connections'] = $user->getRelationships(1, 0);

function getRelationships($approved = TRUE, $offset = 0)
    {
        return $this->relateduser
                ->include_join_fields()
                ->where('approved', $approved)
                ->include_related('profile', $this->related_fields)
                ->get();
    }



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

[eluser]Kestutis S.[/eluser]
Hay,
I try to integrate CI (with DataMapper ORM) with WordPress. Wordpress have a table ‘wp_users’ where the primary id is - ‘ID’ (upper case). I want to have my ‘profiles’ table with one-to-one connection with ‘wp_users’, but it doesn’t work because the ‘wp_users’.‘ID’ is upper case. Any suggestions to solve this problem?


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

[eluser]JonoB[/eluser]
Does this lib play nicely with Form Gen lib (http://ellislab.com/forums/viewthread/107861/)? Specifically, the passing of validation errors from the model back to the form.


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

[eluser]WanWizard[/eluser]
[quote author="Madmartigan1" date="1300465867"]FWIW, I'm also using DM with wiredesignz's MX package - I had noticed some concerns from other users about this combo but I haven't really had an issue.[/quote]
Wiredesigz has been so friendly to add support for CI's loader class model_paths array, which Datamapper uses to locate the models.

[quote author="Madmartigan1" date="1300465867"]The only issue I did notice was that it does not autoload the validation label lang files from module paths.[/quote]
Problem is that MX is a 'bolt-on' type of solution that uses it's own syntax to load stuff. Datamapper doesn't have access to that, and CI doesn't provide multi-path support for language files, like for models and libraries.

[quote author="Madmartigan1" date="1300465867"]Hey since I'm error reporting now, I also noticed that "get" rules are not used if you assign them in the model __construct.[/quote]
They should be if you define them BEFORE you call parent::__construct(). Because it's the Datamapper constructor that processes the config and all model definition variables.


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

[eluser]WanWizard[/eluser]
@BrendanRehman,

You're passing a boolean as $approved, which can make MySQL behave funny. Make sure you're using the correct type for the column used, either integer or string.


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

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


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

[eluser]WanWizard[/eluser]
@JonoB,

Have you tried it? Tell me your experiences, error messages, ways you solved things. And then I might be able to help you, or adapt Datamapper.


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

[eluser]IgnitedCoder[/eluser]
@WanWizard,

Changed it to use int as the col type and test it got the same results... but when I do this

Code:
$user = new User($this->user_id);
$user1 = new User($this->user_id);
$this->page_data['request'] = $user1->getRelationships(0, 0);
$this->page_data['connections'] = $user->getRelationships(1, 0);

It works just fine...

Its almost as if the second call to getRelationships is over-writing the existing result set for requests.