CakePHP - 尝试 $this->Restaurant->City->find('list'); 时未定义属性
我试图允许管理员编辑“餐厅” - 他们需要能够更改的事情之一是餐厅所在的城市。所以 - 我试图通过restaurants_controller.php将“城市”表中的城市列表传递给我的/restaurants/admin_edit.ctp
function admin_edit($id = null) {
$this->set('cities', $this->Restaurant->City->find(
'list', array('order' => 'City.name', 'fields' => 'City.id, City.name')));
...etc
我希望/假设这是一个愚蠢的问题,我只是做错了,因为我我是 CakePHP 的新手,但是经过一个半小时的搜索,我找不到如何从restaurants_controller.php 获取城市列表的解释
我已经在restaurant.php 模型中设置了hasOne,并且我的belongsToMany在我的 city.php 模型中(不确定这是否重要)。
I'm trying to allow an admin to edit "restaurants" - one of the things they need to be able to change is the city where the restaurant is located. So - I'm trying to pass the list of cities in my "cities" table to my /restaurants/admin_edit.ctp via the restaurants_controller.php
function admin_edit($id = null) {
$this->set('cities', $this->Restaurant->City->find(
'list', array('order' => 'City.name', 'fields' => 'City.id, City.name')));
...etc
I hope / assume this is a stupid issue and something I'm just doing wrong since I'm new to CakePHP, but after an hour and a half searching, I cannot find an explanation of how I can get the list of cities from the restaurants_controller.php
I have set up my hasOne in the restaurant.php model, and my belongsToMany in my city.php model (not sure if that matters).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以——问题是,我的联想消失了。我误解了这些关联,但是在阅读这篇非常有用的文章之后,我重写了我的关联(有很多,属于...等),瞧 - “查找”工作完美。
我还发现一些地方说我可以使用 $uses 变量,但我发现其他一些地方立即驳回了这一点,并说只使用正确的关联。
So - the issue was, my associations were off. I missunderstood the associations, but after reading this incredibly helpful article, I rewrote my associations (hasMany, belongsTo...etc), and voila - the "find" worked perfectly.
I also found a few places that said I could use a $uses variable, but I found a few other places that dismissed that immediately, and said to just use correct associations.