cakephp多行中的外键不起作用

时间:2014-05-09 11:14:39

标签: cakephp

我有2张桌子:

门票

`CREATE TABLE IF NOT EXISTS `tickets` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `nom_model` varchar(32) NOT NULL,
  `is_active` tinyint(1) NOT NULL,
  `date_modif` date NOT NULL,
  PRIMARY KEY (`id`));`

详情

  `CREATE TABLE IF NOT EXISTS `details` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `ticket_id` int(11) NOT NULL,
  `nom` varchar(32) NOT NULL,
  `text` varchar(32) NOT NULL,
  `taille` enum('8','9','10','11','12','13','14','15','16','17','18','19','20') NOT NULL,
  `police` enum('Impact','courier new','times new roman','comic sans ms') NOT NULL,
  `bold` int(2) NOT NULL,
  `italic` int(2) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `ticket_id` (`ticket_id`));`

门票有11个区域(msg_acc,nom_buro,heure,date ....) 这是表格详细信息中字段nom的属性 并且在一个区域中有(文本,警察,粗体,斜体)

因此必须在表中保存11行(详情)

我想创建表单(包含表格票证中的nom模型并在表格详细信息中保存11行,表格详细信息中的外键ticket_id是新票证创建(nom_model)):

我创建表格保存11行市场很好,但是关键不行 nom_model不保存在表(票证)中,11行保存,ticket_id等于0

模型Ticket.php中的代码

 <?php
class Ticket extends AppModel
{
   var $name = 'Ticket';
   public $displayField='id';
   public $hasMany = array(
   'Detail'
   );
}
?>

模型Detail.php中的代码

    <?php
class Detail extends AppModel{   
   var $name = 'Detail';
   public $belongsTo = array(
   'Ticket'
   );
}
?>

控制器detailsController.php中的代码

    $this->set('title_for_layout', __('Add Details'));
        $tickets= $this->Detail->Ticket->find('list');
        if (!empty($this->request->data)) {
        if($this->Detail->saveAll($this->request->data['Detail'])){
          $this->Session->setFlash(__('The Model has been saved'));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The Model could not be saved. Please, try again.'));
              }
          $this->redirect(array('action' => 'index'));

          }   
                $this->set('tickets', $tickets);

视图中的代码add.ctp

<?php echo $this->form->create('Detail');?>
   <fieldset>
      <legend>Add New Detail</legend>
   <?php
   echo $this->form->input('Ticket.$i.nom_model');//not working
   for($i=0;$i<$count;$i++){
      echo $this->form->input("Detail.$i.name");
      echo $this->form->input("Detail.$i.text");
      echo $this->form->input("Detail.$i.taille");
      echo $this->form->input("Detail.$i.police");
      echo $this->form->input("Detail.$i.bold");
      echo $this->form->input("Detail.$i.italic");
       $this->form->hidden('ticket_id');} ?> </fieldset>
      <?php echo $this->form->end('Submit');?> 

1 个答案:

答案 0 :(得分:0)

您要按saveAll 保存请求数据,而不使用关联的故障单

Ticked的关联数据位于$this->request->data['Ticket'],将被忽略:

//(Line 4 of your posted controller)
$this->Detail->saveAll($this->request->data['Detail'])

<强>更新

您的$this->request->data应具有以下结构:

Array
(
   [Ticket] => Array
                 (
                       [id] => 1
                       ...
                 )
   [Detail] => Array
                      (
                       [0] => Array
                             (                                 
                               [ticket_id] => 1
                               ...
                             )

                       [1] => Array
                             (                                  
                               [ticket_id] => 1
                               ...
                             )
                       ..........
                     )
)

调整您的视图,生成该数据字段,您可以尝试通过$this->Detail->saveAll($this->request->data)

保存发布的数据

<强>更新

您的基本视图应如下所示:

   <fieldset>
      <legend>Add New Detail</legend>
   <?php

   echo $this->form->input("Ticket.nom_model");
   echo $this->form->hidden("Ticket.id", array('value' => '1'));
   for($i=0;$i < $count;$i++){
      echo $this->form->input("Detail.$i.name");
      echo $this->form->input("Detail.$i.text");
      echo $this->form->input("Detail.$i.taille");
      echo $this->form->input("Detail.$i.police");
      echo $this->form->input("Detail.$i.bold");
      echo $this->form->input("Detail.$i.italic");
      echo $this->form->hidden("Detail.$i.ticket_id", array('value' => '1'));} ?> 
   <?php echo $this->form->end('Submit');?> 

   </fieldset>

这是一个示例视图,通过Detail

Ticket引用到Ticked.id = 1

<强>更新

您应该在Detail中定义从TicketModels/Detail.php的外键:

public $belongsTo = array(
    'Ticket' => array(
        'className' => 'Ticket',
        'foreignKey' => 'ticket_id'
        )
   );