Welcome Guest, Not a member yet? Register   Sign In
Datamapper: Get related and where clauses
#1

[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?
#2

[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
#3

[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"
#4

[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
Error Number: 1054

Unknown column 'users.name' in 'where clause'

SELECT `alerts`.* FROM (`alerts`) WHERE `users`.`name` = 'Peter' LIMIT 5

Filename: XXXXXXXXXXX\system\database\DB_driver.php

Line Number: 330

what am i doing wrong?
#5

[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();

$user->where('id', 1)->alert->get();
#6

[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();

$user->where('id', 1)->alert->get();
[/quote]

I can't do it on this way, but i can do:
Code:
$alert->where('id', 1)->get();
$alert->user->get();

$user->where('id', 1)->alert->get();
$user->alert->get();
#7

[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();
var_dump($parent->where_related_child('name','kid 2')->get()->all_to_array());
This just works.

So I remain with my original conclusion that you must have a configuration error in one of your models.
#8

[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();
var_dump($parent->where_related_child('name','kid 2')->get()->all_to_array());
This just works.

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);
#9

[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();
var_dump($parent->where_related_child('name','kid 2')->get(10,5)->all_to_array());

p.s. I test this on CI 2.1.0.
#10

[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!




Theme © iAndrew 2016 - Forum software by © MyBB