当我的关系看起来正确时,为什么会出现未定义的属性错误?

发布于 2024-09-09 08:45:55 字数 657 浏览 9 评论 0原文

我遇到了一个我无法解决的小问题,但应该非常简单。
我的 cakePHP (1.3) 应用程序中有以下模型结构:

ProspectiveQuote [hasMany] QuoteUnit [belongsTo] Unit

但在我的 ProspectiveQuotesController 中,该行:

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

给我以下错误:

Undefined property: AppModel::$Unit

当然,它不应该查看 AppModel ,它应该查看 QuoteUnit。
如果我这样做 $this->ProspectiveQuote->QuoteUnit->find('all') 它似乎得到了结果(尽管没有任何相关的模型数据......)所以它显然找到了QuoteUnit 足够好,我已经仔细检查了它与 Unit 的关系,一切看起来都很好......

似乎是一个足够简单的问题。据我所知,有这个问题的人通常会弄错他们的型号名称(或复数),但这里的情况并非如此......
我可能做错了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

〃温暖了心ぐ 2024-09-16 08:45:55

我想说要仔细检查模型关联的语法,以确保它们是正确的。或者支持它们,并烘烤一些新模型进行测试,只是为了确保它符合您的期望。

另一件很棒的事情是获取 DebugKit http://www.ohloh.net/p/cakephp- debugkit 它将帮助您查看变量和 sql 查询。

正如 Leo 的评论中提到的,我会尽量避免使用 uses() ,或者确实在 1.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.

天生の放荡 2024-09-16 08:45:55

您是否在控制器中设置了 var $uses = array('ProspectiveQuote','QuoteUnit','Unit'); ? (尽管有一些更有效的方法) - 请参阅 http: //book.cakephp.org/2.0/en/controllers.html#controller-attributes

如果您这样做,您可以访问关联的模型,例如:

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

或者

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

我知道我更喜欢哪个。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文