Welcome Guest, Not a member yet? Register   Sign In
DMZ 1.7.1 (DataMapper OverZealous Edition)

[eluser]cahva[/eluser]
Ok, I think cross database joins are not supported Sad :

http://stackoverflow.com/questions/77246...base-joins

[eluser]BrainFeeder[/eluser]
The same SO question I stumbled on yesterday Big Grin ...
I've been lucky this time, because the user table has a foreign key company_id, I can just do

Code:
$c = new Company($u->company_id);

and go on from there... I hope this was the only situation where I needed cross database joins :|

[eluser]WanWizard[/eluser]
That is a limitation of MySQL (actually, of the driver).

You can only do cross-database joins when both databases are accessed via the same connection, and then prefix the table names (of the non-default database) in the query with the database name.

This is a not-supported setup in CI (the CI query builder doesn't prefix the table names), and therefore also not in Datamapper (which uses the CI query builder to construct the queries).

[eluser]BrainFeeder[/eluser]
Saving cross database relations does work. Getting and deleting however fails. Strange. I'll have to move everything to the same database I guess.

[eluser]WanWizard[/eluser]
What do you mean by "saving cross database relations"?

Depending on your configuration, Datamapper will use a shared connection for all models, or a unique connection for every model.

So it's no problem so save a parent and it's children in one go, where parent and child are in different databases. This is done with several INSERT or UPDATE queries, per model, so they are always limited to a single database.

[eluser]wietse[/eluser]
As posted here:



I use this to count all active users in a database, works like it should:

Code:
$ouser = new User;
$data['users_active'] = $ouser->where(array('active'=>1))->count();

Now I also want to use the (same) object to count all inactive users, therefore I want to use this:

Code:
$ouser = new User;
$data['users_active'] = $ouser->where(array('active'=>1))->count();
$data['users_inactive'] = $ouser->where(array('active'=>0))->count();

But this doesn't seem to work. Clearing the object first doesn't work either:

Code:
$ouser = new User;
$data['users_active'] = $ouser->where(array('active'=>1))->count();
$ouser->clear();
$data['users_inactive'] = $ouser->where(array('active'=>0))->count();

How can I resuse the same object, in this case for counting?

[eluser]WanWizard[/eluser]
What is the definition of "not work"? Error messages? What is the result you do get? What does check_last_query() return?

[eluser]wietse[/eluser]
Thanks for your reply.

I am trying to do this (little different, but same idea):
Code:
$ouser = new User;
$data['users_active'] = $ouser->get_where(array('active'=>1));
$data['users_inactive'] = $ouser->get_where(array('active'=>0));
$this->load->view('users', $data);
I have 2 active users, and 1 inactive.

Now when I loop them in my view, either the active or inactive give the same result (in this case only the inactive users, because this was the last line in the above code):
Code:
foreach($users_active AS $user) {
    echo $user->name; // shows 1 user, the inactive one
}
foreach($users_inactive AS $user) {
    echo $user->name; // also shows 1 user, the same inactive one
}
As stated in the previous post, a $ouser->clear(); doesn't solve this...
Any ideas?

[eluser]WanWizard[/eluser]
You have to remember that you work with objects, and objects are (in recent PHP versions) passed around by reference.

Datamapper methods return the current object (so you can chain methods off it), unless it has a specific return value, like count(), which returns a number (which is why I didn't understand your question).

So after this code, $data['users_active'], $data['users_inactive'] and $ouser all contain a reference to the same object. You can see that when you dump the variables, the object # will be the same for all three.

This means you can't just re-use a model object like this. Either create a new one, or clone the existing one.

[eluser]wietse[/eluser]
[quote author="WanWizard" date="1370966550"]Datamapper methods return the current object (so you can chain methods off it), unless it has a specific return value, like count(), which returns a number (which is why I didn't understand your question).[/quote]
Well, that was probably my fault Smile

But thanks again for your reply... it's working now!




Theme © iAndrew 2016 - Forum software by © MyBB