Why am I getting an undefined property error when

2019-09-15 20:18发布

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条回答
相关推荐>>
2楼-- · 2019-09-15 20:54

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.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-09-15 21:15

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.

查看更多
登录 后发表回答