Welcome Guest, Not a member yet? Register   Sign In
DataMapper stumping me - almost works
#1

[eluser]BrianDHall[/eluser]
Hi all,

I'm using DataMapper (OverZealous Extension) and am running into something that I'm just not getting.

I have a table for listings, and a table for garagetypes, with a relationship of has_one from listings to garagetypes, and has_many the other way around.

Here's the code in question:

Code:
$li = new Listing();
$li->where('id', 1)->get();
$li->garagetype->get();

So I get the listing with an ID of 1 - this works as expected. What I can't get to work is the last line.

If I build my table with a join table of garagetypes_listings, DataMapper will correctly update that table correctly referencing the listing and the garage type that corresponds to it. If I build my table without a join table and instead use an in-table foreign key of garagetype_id, that works as far as the saving of the relationship as well.

What doesn't work at all, no matter what I do, is the 3rd line of code. I turned on profiling and I get this for SQL queries:

Code:
DATABASE:  rentlist   QUERIES: 3
SELECT * FROM `listings` LIMIT 1  
SELECT * FROM (`listings`) WHERE `id` = 1
SELECT * FROM `garagetypes` LIMIT 1

OK, so each query relates to each line of code, right?

What I'm not getting is why the third line of code is what it is, or acts like it does.

According to my debugger after that third line of code $li has a garagetype object, which has values of null as far as id and name and display name go. Garagetype table has 6 entries, and the join table (when used) shows id 0, garagetype_id 2, and listing_id 1.

I don't get it - what am I missing? Where to look next?

There seems to be something fundamental I'm missing here, I just have no idea what it is.
#2

[eluser]BrianDHall[/eluser]
On further testing I find the same problem, but what I did find was that when I update a listing it correctly updates the relationship in the join table.

In the manual it seems to use just exactly this technique and it 'just works', but here it just...well, isn't Sad

Is there some other way to load relationship info? Perhaps another method to make the listing contain the member values of the garage type, so I can just pull a listing and access the display name of it's associated garage type?
#3

[eluser]bretticus[/eluser]
Never used it (yet.) However, looks liek OverZealous follows a forum thread at

http://ellislab.com/forums/viewthread/112904/

Might get some feedback straight from the developer.
#4

[eluser]BrianDHall[/eluser]
Thanks bretticus, you actually helped me solve my problem.

Just goes to show sometimes its best to leave something and come back later, and just the process of asking a question helps to answer it.

I went to create the post and found what I had done wrong. It was so utterly simple and basic I looked right past it. The ORM equivalent of using = where you meant to use ==, I think.

Incase others have a similar problem, here's what happened.
Code:
class Garagetype extends DataMapper {
    // Insert related models that Garagetype can have just one of.
    var $has_one = array();

    // Insert related models that Garagetype can have more than one of.
    var $has_many = array('listings');

And in the other model:

Code:
class Listing extends DataMapper {
    // Insert related models that Listing can have just one of.
    var $has_one = array('user', 'garagetype');

    // Insert related models that Listing can have more than one of.
    var $has_many = array();

Can you see the problem? I sure couldn't.

It was a real pain in the S...it was in Garagetype. It said it has many "listings", but notice how in Listing the reference is singular, not plural?

That's what it was. Ugh, now it works perfectly! And beautifully, really, so easy.

It's the one downside to magic systems - a small error can make things break without noticable error, something just stops working. Its always made me leary of them, but the truth is I've spent far more time hunting my own bugs than someone else's, so I shouldn't be so repelled by other peoples code Wink




Theme © iAndrew 2016 - Forum software by © MyBB