问题描述
我有一段时间让我的协会在这里工作。每次我在事件控制器上运行视图操作,并使用var_dump,我只是回到事件信息,没有公司信息,它就像它忽略我的协会,一起,更令人烦恼的是没有错误消息。
I am having a hell of a time getting my association to work here. every time I run the view action on the Events controller, and use var_dump, I just get back the event info, and no company info, it's like its ignoring my association all together, and whats even more annoying is there is no error messages.
事件表有一个名为Company_ID的字段,Companies表有一个ID字段。 (是大写的公司ID和ID)。
the Events table has a field called Company_ID, and the Companies table has an ID field. (yes upper case Company_ID, and ID).
class Event extends AppModel{
public $name = "Events";
// public $belongsTo = 'Company';
public $belongsTo = array('Company'=>array(
'className' => 'Company',
'foreignKey'=>'Company_ID'
));
//public $hasOne = 'Company';
/*public $hasOne = array('Company'=>array(
'className' => 'Company',
'foreignKey'=>'Company_ID'
));*/
}
App::uses("AppModel", "Model");
class Company extends AppModel{
public $name = "Company";
}
呼叫控制器。
public function view() {
$id = $this->request->params['id'];
$this->set(
'event',
$this->Event->find(
'first',
array('conditions' => array('Event.id' => $id))
)
);
}
推荐答案
CakePHP的可能重复保存相关模型。请参阅这是迄今为止为什么是我的模型逻辑未执行问题的最常见原因。
possible duplicate of CakePHP does not save associated Models. see This is by far the most common cause of "why is my model logic not being executed" questions.
这是文件名,其中一些是复数形式对单数。我将Events.php更改为Event.php,将Companies.php更改为Company.php。
It was the file names, some of them where the plural form vs. singular. I changed Events.php to Event.php and Companies.php to Company.php that seemed to do the trick.
这篇关于CakePHP $ belongsTo不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!