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

[eluser]OverZealous[/eluser]
@happydude
http://www.overzealous.com/dmz/pages/tro...Controller

Also, please try doing a little troubleshooting. There are tips at the top of that page.

[eluser]sqwk[/eluser]
Is there away to set up a relationship between two tables in two databases? (on the same server, with the same login credentials)

[eluser]OverZealous[/eluser]
@squawk
Nope. DMZ needs both objects on the same database.

However, you could manually simulate the connection. It's not officially supported, but DMZ has the ability to talk to multiple databases. There's just no way for it to understand the relationship.

[eluser]Robert Liljedahl[/eluser]
Hi people, and hi OverZealous. I belive this is my very first post. Beeing a front end developer who the last year or so have started to pay interest for php, oop and CI I can't tell you enough how much this forum has helped me. Yet I have coded very little, mostly been reading, and it's a great feeling for someone at my (low) level to actually feel that something is being learnt. It feels like I'm slowly getting a grip on things.

Your forum posts, OverZealous, and the documentation for DMZ, has helped me so much. So I just want to give you a big thank you. Two other users from which contributions I also learned a great deal is wiredesignz and dexcell. So big thanks to these guys as well. I guess this probably should be in another thread, but from the beginning I was just about to thank you OverZealous for DMZ Smile

Take care, and keep up the fantastic work! I might be back with some silly questions later on!

[eluser]sqwk[/eluser]
I've got a bit of trouble with relationships:
Code:
$b = new Buyer();
$b->order_by('updated', 'desc')->get($limit, $offset);
$b->user->get();
$b->place->get();
$b->objtype->get();

Foreaching through $b works fine and I can echo all the fields from the buyers table, however, ALL of the relationship fields (like $buyer->user->username or $buyer->objtype->name) are empty. I checked that all the relationships are set up right in the models

All the fields from the joined tables are set, it is just that they are empty. Or am I accessing them wrong?

The other thing that I noticed is that all the queries from the joined tables seem to have an additional WHERE clause. (What are they for?)

Code:
WHERE `ksv_buyers`.`id` = 353

353 is the id of the first record that is being returned. The relationship fields are also not filled for this record.

[eluser]TheJim[/eluser]
squawk, when you access a related model from your object and call get(), you're accessing only those records related to your current object. That is $b->user->get() is not going to be all User records, but only those related to whatever Buyer is stored in $b (after a call to get(), this would be the first Buyer record loaded). So that's why the WHERE clause exists in those generated queries. Your sentence "the relationship fields are also not filled for this record [your first result: Buyer 353]" explains why nothing was loaded after your calls to get() for the related objects.

It sounds like you might need to read through the manual more thoroughly to make sure you're clear on relationships. And if you're wanting those relationships included for each Buyer object, I'd especially look at the include_related function for efficiency's sake.

[eluser]anaxamaxan[/eluser]
To add to what TheJim has already accurately pointed out, your code could be explained like this:
Code:
$b = new Buyer();
$b->order_by('updated', 'desc')->get($limit, $offset);
$b->user->get(); //gets the user for only the first record returned by the get() above
$b->place->get(); // gets the the place for only the first record,
$b->objtype->get();  // etc

Using a simple include_related will likely do the trick in this case, e.g.:

Code:
$b = new Buyer();
$b->order_by('updated', 'desc')
  ->include_related('user',null,true,true)
  ->include_related('place',null,true,true)
  ->include_related('objtype',null,true,true)
  ->get($limit, $offset);

See also the docs section about auto-populating related objects, Setting Up Relationships in the DMZ docs.

[eluser]sqwk[/eluser]
So if some of the relationships are has-many relationships, I would have to use get_iterated(), loop over the result set and add the related object like I did before?

[eluser]TheJim[/eluser]
Yes, include_related won't pull in has_many relationships. However, there are other ways to consider setting it up if looping over your main result and querying related objects slows you down with more queries than you'd like. It depends on on your circumstances, but you could go the other direction: run the query on your has_many object and use include_related for your main has_one. Or you could run separate queries and from the has_many side, include only your has_one's ID to relate to an already-loaded record. It really all depends on your data and how many queries will be involved in loading a typical page.

So, the short answer is yes, but consider alternatives if necessary.

[eluser]sqwk[/eluser]
Both objtype and place are has-many relationships and I allow up to three relationships per one buyer. A single page can display up to 100 records, so looping over the object would give me an additional 600 queries, which is why I am slightly hesitant to use it.

I was thinking about condensing all the ids from the buyer query into a comma separated list, to then only load all the places/objtypes that I need with one query. (Where_in) To join them I would need to loop over the buyer object a second time. This gives me less queries, but more logic. Whether this is the fastest option I do not know…




Theme © iAndrew 2016 - Forum software by © MyBB