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-26-2009

[eluser]OverZealous[/eluser]
@MeanStudios
You can actually chain objects out, as far as you want:
Code:
$c = new Client();
    $c->where('id', $id)->get();
        
    $address = $c->address->get();
    $state = $address->state->get();
    $country = $state->country->get();

Also, if you wanted to try DMZ, a drop-in replacement for DataMapper that adds a lot of features (if you are already using it, sorry, I get confused this late at night ;-) ), you can embed data one-level deep into a query:
Code:
// embed the state into the address
$address = $c->address->join_related('state', array('id', 'name'))->get();
$state_name = $address->state_name;

Another option would be to relate the country directly to the address as well, which would make it possible to join both objects.


DataMapper 1.6.0 - El Forum - 04-26-2009

[eluser]MeanStudios[/eluser]
Nope, haven't heard of it till now Smile. Thanks for the tips!
Also, I'm going to give DMZ a try Wink.


DataMapper 1.6.0 - El Forum - 04-27-2009

[eluser]nmac[/eluser]
Hi, thanks for the help so far, making good progress with DMZ.
I have a question though about getting a resultset based on a relation.
Code:
$t = new Tag();
$t->where('tag',"sometag" )->get();
$t->article->order_by($queryopts['order'], $queryopts['sort']);
$t->article->limit($row_count,$offset);
$data['articles'] = $t->article->get();
$data['articleq'] = $this->db->last_query();
I am getting a resultset in $data[articles] but it is not limited or offset
Code:
<!-- last query is showing me this... -->
SELECT `articles`.*
FROM (`articles`)
LEFT JOIN `articles_tags` as articles_tags ON `articles`.`id` = `articles_tags`.`article_id`
LEFT JOIN `tags` as tags ON `tags`.`id` = `articles_tags`.`tag_id`
WHERE `tags`.`id` = '41'
ORDER BY `articles`.`created` asc
So the question is, can I use LIMIT (with OFFSET) on a relation using DMZ?
Am I missing something or doing something wrong?

Thanks for any help.
Cheers
N.


DataMapper 1.6.0 - El Forum - 04-27-2009

[eluser]OverZealous[/eluser]
It should work just fine. A relationship query is really just a shortcut to looking up an item based on the parent item.

I usually use get($limit, $offset), but I was going through my code to see if I used it exactly like you use it, and I find that in almost all cases I use:
Code:
$article = new Article();
$article->where_related($t)->get($limit, $offset);
I did this to keep a little better control over the select part of the query (since relationship queries do some magic ;-) ).

However, I can't see why it would matter. Calling the query the way you are actually uses where_related under the surface, anyway.

I suppose there could be a bug in using limit() instead of get($limit, $offset), but that doesn't make too much sense, since get($limit, $offset) calls limit() when looking up relationships. There just isn't anything I can see in the code to cause it.


DataMapper 1.6.0 - El Forum - 04-27-2009

[eluser]OES[/eluser]
Hi OverZealous

Geetting a wiered SQL error and not sure on this one but hope you can advise. Im just doing a basic relationship call using your datamapper file so I have removed the relationship table and added the model_id within the first table.

A simple call ie.

Code:
// Create new Object
$c = new Category();
$c->where('id', 1)->get();

// Now Get One to One Relationsip
$c->stadium->get();

Very simple all models setup with correct one one and names etc.

But the SQL error Im getting it.
Code:
Table 'tablename.stadia' doesn't exist
SELECT * FROM `stadia` LIMIT 1

Why is it trying to acces table name stadia ??

Just to confirm I have corrent has one relationships ie
Code:
// category model
var $has_one = array('stadium');

// stadium model
var $has_one = array('category');


Any ideas ??

Hope you can advise


DataMapper 1.6.0 - El Forum - 04-27-2009

[eluser]OES[/eluser]
Ok I just found my issue.

Ive added var $table = "stadiums";

Strange as I thought it added the s ??

Oh Well !


DataMapper 1.6.0 - El Forum - 04-27-2009

[eluser]OverZealous[/eluser]
DataMapper uses a modified Inflector helper, which sometimes is a little too smart (ie: stadia is the greek-suffix pluralization of stadium), and sometimes not smart enough (ie: addres is not the singular form of addresses).

That's the joy of English: it is amazingly flexible, and able to absorb words from languages around the world, but the rules are just as flexible!


DataMapper 1.6.0 - El Forum - 04-27-2009

[eluser]NachoF[/eluser]
[quote author="OverZealous.com" date="1240874349"]DataMapper uses a modified Inflector helper, which sometimes is a little too smart (ie: stadia is the greek-suffix pluralization of stadium), and sometimes not smart enough (ie: addres is not the singular form of addresses).

That's the joy of English: it is amazingly flexible, and able to absorb words from languages around the world, but the rules are just as flexible![/quote]

Thats weird, i thought it just added an 's' at the end.... especially considering my tables are actually in spanish (instead of events Im using eventos, instead of products Im using productos) and so far I've had not problems with table and model names.


DataMapper 1.6.0 - El Forum - 04-27-2009

[eluser]nmac[/eluser]
@phil (OverZealous),
Thanks man, that worked a treat!
N.


DataMapper 1.6.0 - El Forum - 04-28-2009

[eluser]apersaud[/eluser]
Hi @OverZealous,

Great work on DMZ 1.2 . Is there a standard location for downloading the latest version of DMZ (maybe a CI wiki page?). It is getting a bit 'annoying' trying to find the right post on this thread with the latest DMZ everytime. :-)

Thanks!