Datamapper: Get related and where clauses |
[eluser]scherman[/eluser]
Hi people, i have a problem, and i hope you can help me. I'm using the Datamapper, i've read the documentation but i can't figure out how to get the objects only when theirs related object is a string. And it's prepared to the pagination. So, i have two classes: - Alert (has one user) - User (has many alerts) Here is my code: Code: $alert->get(10, $offset); And i want something like: Code: $alert->where_user('name', 'Peter')->get(10, $offset); Do you know how can i do that?
[eluser]WanWizard[/eluser]
Code: $alert->where_related_user('name', 'Peter')->get(10, $offset); It is really in the docs: http://datamapper.wanwizard.eu/pages/get...ated_model
[eluser]scherman[/eluser]
I'm sorry, i'v not seen it! Thank you so much. One more question, can i do the same but with the "like" clause instead of "where"
[eluser]scherman[/eluser]
[quote author="WanWizard" date="1328569266"] Code: $alert->where_related_user('name', 'Peter')->get(10, $offset); It is really in the docs: http://datamapper.wanwizard.eu/pages/get...ated_model[/quote] Another thing: I'm doing what you say: Code: $alert->where_related_user('name', $name)->get(10, $offset); and it returns me the next error: Quote:A Database Error Occurred what am i doing wrong?
[eluser]WanWizard[/eluser]
It doesn't add the JOIN, which is probably because it can't resolve the relation. Are your model definitions ok (both from alert -> user and from user -> alert)? Can you do Code: $alert->where('id', 1)->user->get();
[eluser]scherman[/eluser]
[quote author="WanWizard" date="1328611293"]It doesn't add the JOIN, which is probably because it can't resolve the relation. Are your model definitions ok (both from alert -> user and from user -> alert)? Can you do Code: $alert->where('id', 1)->user->get(); I can't do it on this way, but i can do: Code: $alert->where('id', 1)->get();
[eluser]WanWizard[/eluser]
You're right, you need to get the parent before you can get children. But, just tried it here, and I don't have an issue: Code: $parent = new Parent(); So I remain with my original conclusion that you must have a configuration error in one of your models.
[eluser]scherman[/eluser]
[quote author="WanWizard" date="1328634361"]You're right, you need to get the parent before you can get children. But, just tried it here, and I don't have an issue: Code: $parent = new Parent(); So I remain with my original conclusion that you must have a configuration error in one of your models.[/quote] I've figured out that my problem is when i use the $offset. Because, this line works fine: Code: $alert->where_related_user('name', $name)->count(); And this line doesn't work (it shows me the error i've posted before): Code: $alert->where_related_user('name', $name)->get(10, $offset);
[eluser]WanWizard[/eluser]
I find that very odd. Specifying a limit and an offset doesn't do anything, it gets passed straight on to CI: Code: $this->db->get($this->table, $limit, $offset); Back to my test, this works fine here too: Code: $parent = new Parent(); p.s. I test this on CI 2.1.0.
[eluser]scherman[/eluser]
I've found my stupid mistake: I was trying to get the count and then get the object's list, but i had to clear the object between this two methods. Sorry! Thank you so much WanWizard! |
Welcome Guest, Not a member yet? Register Sign In |