繁体   English   中英

CakePHP-保存关联的模型数据

[英]CakePHP- saving associated model data

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

Schedule属于ScheduleContact联系人

从属于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')); ?>

等等..

请求数据以这种形式出现(来自var_dump($this->request->data) ):

array (size=3)
  'Schedule' => 
    array (size=3)
      'start_at' => string '2014-3-25 11:15' (length=15)
      'finish_at' => string '2014-03-25 11:30:00' (length=19)
      'donation_method_id' => string '1' (length=1)
  'ScheduleContact' => 
    array (size=1)
      0 => 
        array (size=6)
          'name' => string 'Jane' (length=4)
          'surname' => string 'Powell' (length=6)
          'address' => string 'Hamilton Hodell, 5th Floor 66-68 Margaret Street' length=39)
          'city' => string 'London' (length=6)
          'tel_no' => string '+44(0)20-7636 1221' (length=18)
          'email' => string 'powell_jane@gmail.com' (length=22)
  'log' => 
    array (size=3)
      'ds' => string 'default' (length=7)
      'sql' => string 'SELECT `Schedule`.`id`, `Schedule`.`donation_method_id`, `Schedule`.`schedule_contact_id`, `Schedule`.`start_at`, `Schedule`.`finish_at`, `DonationMethod`.`id`, `DonationMethod`.`donation_method`, `DonationMethod`.`recovery_time`, `DonationMethod`.`duration`, `ScheduleContact`.`id`, `ScheduleContact`.`name`, `ScheduleContact`.`surname`, `ScheduleContact`.`address`, `ScheduleContact`.`city`, `ScheduleContact`.`tel_no`, `ScheduleContact`.`email`, `ScheduleContact`.`donor_id` FROM `blood_services_db`.`schedule'... (length=852)
      'hash' => string '5f2b2b3a462f6555cb5290fb49c42df04a7948e0' (length=40)

最后,我试图像这样保存数据:

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

有什么问题吗? 谢谢

基本上,问题在于saveAssociated需要在主模型中应用。 考虑到这一点,如上所述,主模型具有bolongsTo关系,这意味着the current model contains the foreign key. ,这的确是正确的。 那么您可能会问哪里问题呢?

问题是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中的数据后,外键将保存在Schedule模型内。

您去了...一旦超过该学习曲线,CakePHP就很棒。

您的模型名称违反了Cake的命名约定。 它应该是:

ScheduleContact

一些问题可以给您更好的答案:

  • 您可以添加模型关联变量($ belongsTo等)吗?

  • 您是否使用可遏制的行为?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM