0

I'm having a slight problem that I can't figure out, but should be really simple.
I have the following model structure in my cakePHP (1.3) app:

ProspectiveQuote [hasMany] QuoteUnit [belongsTo] Unit

but in my ProspectiveQuotesController the line:

$this->ProspectiveQuote->QuoteUnit->Unit->find('list');

gives me the following error:

Undefined property: AppModel::$Unit

Of course, it shouldn't be looking at AppModel, it should be looking at QuoteUnit.
If I do $this->ProspectiveQuote->QuoteUnit->find('all') it seems to get results (allbeit without any related model data...) so it obviously finds the QuoteUnit well enough, and I have double-checked its relationship with Unit, and it all seems fine...

Seems like a simple enough problem. From what I can see people with this problem usually have their model names wrong (or plural) but this is not the case here...
What could I be doing wrong?

2 Answers 2

2

I would say to double check over the syntax of your model associations to make sure they are correct. Or back them up, and bake out some new models to test with, just to ensure that it's how you expect it.

Another great thing is to grab the DebugKit http://www.ohloh.net/p/cakephp-debugkit Which will help you to see your variables and your sql queries.

As mentioned in Leo's comment I would try and avoid uses() as it puts, or did put in 1.2 a bit of a big overhead onto your stack.

1
  • Double checked the relationships, and all was fine, except my Unit model didn't have a relationship with QuoteUnit. I didn't think this would matter, as I'm not using the relationship in that direction, but apparently it does matter. Thanks for the tip on DebugKit, I'll be sure to check it out.
    – death_au
    Jul 13, 2010 at 23:20
0

Have you set var $uses = array('ProspectiveQuote','QuoteUnit','Unit'); in your controller? (although there are slightly more efficient ways of doing this) - see http://book.cakephp.org/2.0/en/controllers.html#controller-attributes

If you do this you can access your associated models like:

$this->Unit->find('list');

or

$this->ProspectiveQuote->QuoteUnit->Unit->find('list');

I know which I prefer.

1
  • As far as I know, using $uses like that is frowned upon due to the overhead it introduces. Sure, I would've had to have done that in the case that I couldn't find a solution, but I shouldn't have to. In any case, I worked out my problem: my relationships were one sided. Check out my comment on DavidYell's answer for more info.
    – death_au
    Jul 13, 2010 at 23:22

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.