开发者

Why am I getting an undefined property error when my relationships seem correct?

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 lik开发者_如何学编程e 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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜