2

I'm having some problems with my associations. I have 3 Models: User, Membership, Company. A user may (or may not) have a company/membership. Here's a pastebin of my models:

http://pastebin.com/xTCQeytn

2 questions I have:

  1. Why is Company/Membership returnning an array full of null values if one does not exist.
  2. Do my associates look correct for what I'm trying to accomplish?

1 Answer 1

5

This is normal for BelongsTo/HasOne Relations. As they are on the same level as your primary record. Only HasMany/Habtm relations will result in an empty sub array (since those would be an array of arrays). That is mainly due to the way the array structure is returned.

And yes, your model relations look fine to me.

Sign up to request clarification or add additional context in comments.

3 Comments

I was looking for this in the CakePHP documentation. I also thought I was doing something wrong, because ORMs that I worked with never create object for something non-existent. Do you happen to know if it's documented, and where?
This is not CakePHP or ORM specific, but DB specific, so the wrappers (PDO) of PHP would add/return that, as well, afaik. Simply because any such query in MYSQL would return NULL values for joined relations. Try it out in PHPMyAdmin
I understand this, but the ORMs I worked with had the intelligence not to create objects with all values being null. It is a design decision tough if you want this intelligence, or not. I used to throw poop against CakePHP for these archaic visions, but now I totally accept that this is just a matter of taste. +1 for the correct answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.