Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CakePHP-保存关联的模型数据_Php_Cakephp - Fatal编程技术网

CakePHP-保存关联的模型数据

CakePHP-保存关联的模型数据,php,cakephp,Php,Cakephp,我有两个模型。。一个叫做Schedule,另一个叫做ScheduleContact SchedulebelongsToSchedule联系人 从属于scheduleController的一个操作中,我希望为主模型以及关联模型保存来自序列化表单的值 在我看来,我有以下几点 <?php echo $this->Form->input('Schedule.start_at', array('id' => 'start_at', 'type' => 'hidden'));

我有两个模型。。一个叫做
Schedule
,另一个叫做
ScheduleContact

Schedule
belongsTo
Schedule联系人

从属于scheduleController的一个操作中,我希望为主模型以及关联模型保存来自序列化表单的值

在我看来,我有以下几点

<?php echo $this->Form->input('Schedule.start_at', array('id' => 'start_at', 'type' => 'hidden')); ?>
<?php echo $this->Form->input('Schedule.finish_at', array('id' => 'finish_at', 'type' => 'hidden')); ?>
<?php echo $this->Form->input('ScheduleContact.0.name', array('id' => 'name','div' => 'inline-input')); ?>
<?php echo $this->Form->input('ScheduleContact.0.surname', array('id' => 'surname','div' => 'inline-input compact')); ?>
最后,我尝试这样保存数据:

if($this->Schedule->saveAssociated($this->request->data)){
它不会将任何内容保存到数据库中,因此从未满足此if条件


有什么问题吗?感谢

基本上,问题是saveAssociated需要在主模型中应用。考虑到这一点,如上所述,主模型有一个bolongsTo关系,这意味着当前模型包含外键。,这也是正确的。那么问题出在哪里呢不问

问题是saveAsocciated相当于

 $user = $this->User->save($this->request->data);

        // If the user was saved, Now we add this information to the data
        // and save the Profile.

        if (!empty($user)) {
            // The ID of the newly created user has been set
            // as $this->User->id.
            $this->request->data['Profile']['user_id'] = $this->User->id;
         }
这意味着它首先尝试保存主模型。因此,我的设置不正确,因为主模型需要是
ScheduleContact
,因为在
ScheduleContact
id中成功插入数据后,外键将保存在
ScheduleContact
模型中


好了……一旦你通过了学习曲线,CakePHP就很棒了。

你的模型名打破了Cake的命名惯例。它应该是:

预定联系人

以下几个问题可以为您提供更好的答案:

  • 能否添加模型关联变量($belongsTo等)

  • 您是否使用可控制行为


您是否正确定义了Schedule.php和ScheduleContacts.php中的关系?如果您坚持CakePHP的标准命名约定()ie:Schedule.php中的Schedule和ContactsSchedule.php中的ContactsSchedule分别指的是表Schedule和contacts_Schedule。嗨,是的,我在开始使用cake时在约定方面遇到了问题,但现在我已经克服了这一点。不过我已经解决了。检查答案你是如何得到$this->request->data以那种格式返回的。在其他方面r字,其中每个数组的第一级是不同的模型。我在两个表之间有一对一的关联,但我的数据不是以那种格式返回的。很抱歉,在我的代码中,我遵循了所有的约定。至于你的答案,我可以看到这将使我更接近于“解决”问题,因为如果我没有这样做的话按照惯例,这个协会将不存在。看看答案看看问题是什么!谢谢
 $user = $this->User->save($this->request->data);

        // If the user was saved, Now we add this information to the data
        // and save the Profile.

        if (!empty($user)) {
            // The ID of the newly created user has been set
            // as $this->User->id.
            $this->request->data['Profile']['user_id'] = $this->User->id;
         }