2012-08-31 62 views
1

我介绍CakePHP(V2)工作,我想创建关系的形式与3个表:CakePHP的形式与3款

任务 - 1,N - 1,1 - quiz_questions - 1, N - 1,1 - quiz_answers

型号任务:

class Task extends AppModel { 
    public $name = 'Task'; 
    public $hasMany = 'QuizQuestion'; 

    public $_schema = array(
     'type' => array(
      'type' => 'tinyint' 
     ), 
     'level' => array(
      'type' => 'tinyint' 
     ), 
     'date_availability' => array(
      'type' => 'timestamp' 
     ), 
     'date_end_availability' => array(
      'type' => 'timestamp' 
     ), 
     'date_accomplishment' => array(
      'type' => 'timestamp' 
     ), 
     'title' => array(
      'type' => 'string' 
     ), 
     'description' => array(
      'type' => 'string' 
     ) 
    ); 

    public $validate = array(
     'type' => array(
      'required' => array(
       'rule' => array('notEmpty'), 
       'required' => true, 
       'allowEmpty' => false, 
      ) 
     ), ..............etc 

    ); 

模型QuizQuestion:

<?php 
App::uses('AppModel', 'Model'); 

class QuizQuestion extends AppModel { 
    public $name = 'QuizQuestion'; 
    public $hasMany = 'QuizAnswer'; 
    public $belongsTo = 'Task'; 

    public $_schema = array(
     'title' => array(
      'type' => 'string' 
     ) 
    ); 

     etc.. 
} 
?> 

模型QuizAnswer

<?php 
App::uses('AppModel', 'Model'); 

    class QuizAnswer extends AppModel { 
     public $name = 'QuizAnswer'; 
     public $belongsTo = 'QuizQuestion' ; 

     public $_schema = array(
      'title' => array(
       'type' => 'string' 
      ) 
     ); 
      ...etc.... 
    } 
    ?> 

TasksController家()

public function home() { 
      if ($this->request->is('post')) { 
       $this->Task->create(false);    

       $this->Task->set($this->request->data); 

       $valid = $this->Task->validates(); 

       /*debug($this->Task); 
       exit();*/ 

       if ($valid) { 

         $r = $this->Task->saveAll($this->request->data['Task']); 
         debug($this->request->data['Task']); 
         if (!$r) { 
          $this->Session->setFlash(
            __('INTERNAL_ERROR (%s)', __LINE__), 'flash_error'); 
         } else { 
          $this->Session->setFlash(
            __('Mission enregistré.'), 'flash_success'); 

          $this->redirect(array(
           'controller' => 'tasks', 
           'action' => 'home' 
          )); 
         } 
       } 
else { 
          $this->Session->setFlash(__('pas bon'), 'flash_error');  


} 
      } 
    } 

和形式:

echo $this->Form->create('Task', $formOptions); 
    ?> 

    <fieldset> 
     <?php 

     /*** ETC ***/ 


     $formLabel['text'] = __('Titre du Quizz'); 
     echo $this->Form->input('title', array(
      'label' => $formLabel, 
      'class' => 'input-xxlarge' 
     )); 





     $formLabel['text'] = __('Libelle question %u'); 
      echo $this->Form->input('Task.QuizQuestion.0.title', array(
       'label' => $formLabel, 
       'class' => 'input-xxlarge ' 
      )); 
      $formLabel['text'] = __(('0') . ' - '); 
      echo $this->Form->input('Task.QuizQuestion.0.QuizAnswer.0.title', array(
       'label' => $formLabel, 
       'class' => 'span8' 
      )); 
     ?> 
    </fieldset> 





    <div class="form-actions"> 
<?php 
echo $this->Form->button(
     '<i class="icon-white icon-ok"></i> ' . 
     __('Publier'), array(
    'escape' => false, 
    'class' => 'btn btn-success', 
    'div' => null 
     ) 
); 
?> 
    </div> 
     <?php echo $this->Form->end(); ?> 

'标题' 和 'Task.QuizQuestion.0.title' 被插入到数据库,但“任务.QuizQuestion.0.QuizAnswer.0.title'没有。

debug($ this-> request-> data ['Task']);

array(
    'type' => '1', 
    'level' => '1', 
    'date_availability' => array(
     'day' => '17', 
     'month' => '08', 
     'year' => '2012', 
     'hour' => '00', 
     'min' => '00' 
    ), 
    'date_end_availability' => array(
     'day' => '18', 
     'month' => '08', 
     'year' => '2012', 
     'hour' => '00', 
     'min' => '00' 
    ), 
    'date_accomplishment' => array(
     'day' => '18', 
     'month' => '08', 
     'year' => '2012', 
     'hour' => '00', 
     'min' => '00' 
    ), 
    'title' => 'ju', 
    'description' => 'ji', 
    'QuizQuestion' => array(
     (int) 0 => array(
      'title' => 'jo', 
      'QuizAnswer' => array(
       (int) 0 => array(
        'title' => 'je' 
       ) 
      ) 
     ) 
    ) 
) 

QuizAnswer在这里,但我不知道如何检索值,任何人都可以帮忙吗?

回答

1

,你可以像这样访问

*<?php 
$data['Task'] = array(
    'type' => '1', 
    'level' => '1', 
    'date_availability' => array(
     'day' => '17', 
     'month' => '08', 
     'year' => '2012', 
     'hour' => '00', 
     'min' => '00' 
    ), 
    'date_end_availability' => array(
     'day' => '18', 
     'month' => '08', 
     'year' => '2012', 
     'hour' => '00', 
     'min' => '00' 
    ), 
    'date_accomplishment' => array(
     'day' => '18', 
     'month' => '08', 
     'year' => '2012', 
     'hour' => '00', 
     'min' => '00' 
    ), 
    'title' => 'ju', 
    'description' => 'ji', 
    'QuizQuestion' => array(
     (int) 0 => array(
      'title' => 'jo', 
      'QuizAnswer' => array(
       (int) 0 => array(
        'title' => 'je' 
       ) 
      ) 
     ) 
    ) 
) 
; 

echo $data['Task']['QuizQuestion'][0]['QuizAnswer'][0]['title']; 


?>* 

你的答案,你可以把它像这样在你的代码来访问它

$这个 - >请求 - >数据[“任务”] ['QuizQuestion '] [0] [' QuizAnswer '] [0] [' 标题'];

+0

让我知道是否有困难! –

+0

谢谢你的回答! 但有没有其他方式来检索数据并将其在数据库中自动插入?因为Quizanswer有一个QuizQuestion的外键。 – Juck

+0

如果您觉得有用,请投票支持这个答案 –