2017-01-26 2 views
-1

にMySQLのテーブルにデータを追加し、このアクションは、(対象表からの)サブジェクト名を追加し、(SubjectMarks表から)totalMarksと説明これは追加アクションのコードであるZF2

public function addAction() { 
    $postArray = $this->params()->fromPost(); 
    $entityManager = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default'); 

このデータが入力されるとaddbuttonが

if (isset($postArray['submit']) && $postArray['subjectName']) { 
     $subjectMarks = new SubjectMarks(); 


     $subjectMarks->setSubject(
      $entityManager->find('Application\Entity\Subjects', $postArray['subjectName']) 
     ); 

     $subjectMarks->setClasses(
      $entityManager->find('Application\Entity\Classes', $postArray['classId']) 
     ); 
     $subjectMarks->setSubject(
      $entityManager->find('Application\Entity\Subjects', $postArray['subjectId']) 
     ); 

     $subjectMarks->setTotalMarks($postArray['totalMarks']); 
     $subjectMarks->setDescription($postArray['subjectMarksDesc']); 
     $entityManager->persist($subjectMarks); 
     $entityManager->flush(); 

     return $this->redirect()->toUrl($this->getRequest()->getBaseUrl().'/subjectmarks/totalmarks/index'); 
    } 
をクリックすると条件がtrueに設定されている場合

が他の最初のクエリを実行し、「データ」の形式で表示を「追加」

else 
    { 
     $entityManager = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default'); 
     $qb = $entityManager->createQueryBuilder(); 
     $qb->select(array(
      'subjectMarks.subjectMarkId as subjectMarkId', 
      'subjectMarks.totalMarks as totalMarks', 
      'subjectMarks.description as subjectMarksDesc', 
      'subject.subjectId as subjectId', 
      'subject.name as subjectName', 
      'class.classId as classId', 
     )) 
      ->from('Application\Entity\SubjectMarks', 'subjectMarks') 
      ->leftJoin('subjectMarks.subject', 'subject') 
      ->leftJoin('subjectMarks.classes', 'class'); 
     $data = $qb->getQuery()->getScalarResult(); 

     return new ViewModel(array(
       'data' => $data, 
      ) 
     ); 
    } 
} 

にデータを送るように実行されるこれが呼ばれadd.phtml「FORM」のコードです'else'部分が実行され、問い合わせが必要なデータを選択し、書き込まれたデータを返信します。ここでif()条件で受信されます。

<form name="myForm" action="<?php echo $this->url('subjectmarks/totalmarks', array('action'=>'add'));?>" method="POST"> 
<input type="hidden" name="classId" value="<?php echo $this->data[0]['classId'];?>"> 
<input type="hidden" name="subjectId" value="<?php echo $this->data[0]['subjectId'];?>"> 
<input type="text" name="subjectName" > 
<input type="text" name="totalMarks" > 
<input required="required" type="text" name="subjectMarksDesc" > 

<button type="reset" class="btn btn-primary" > <a style="color: white" href="<?php echo $this->basePath('subjectmarks/totalmarks/index');?>">Cancel</a></button> 
<button type="submit" name="submit" class="btn btn-success">Submit</button> 

通報subjectNameが入力された任意の名前のために、[0]対象テーブルのIDを取っている間totalMarkssubjectMarkDescが正しく追加されることです。それはsubjectIdに割り当てられているsubjectName明確に、私たちは入力とsubjectIdが隠されているようsubjectNameが挿入されていることを見ることができるように

答えて

0

答えは、簡単ですので、は直接関係すなわちはありません。ここでは、質問同様

<option value="<?php echo $item['subjectId'];?> "> <?php echo $item['subjectName'];?> </option> 
<?php endforeach; ?> 
</select> 

とは異なり、subjectIdとサブジェクト名が関連している見ることができるようにselect HTMLタグが問題を解決します入れて

、例えば

<select name="addSubjectMarks"> 
<?php foreach ($this->data as $item):?> 

コントローラーの中にif (isset($postArray['submit']) && $postArray['subjectName'])の状態の中で、私たちはこのコードを置き換えます

$subjectMarks->setSubject(
      $entityManager->find('Application\Entity\Subjects', $postArray['addSubjectMarks']) 
     ); 
このコード

$subjectMarks->setSubject(
     $entityManager->find('Application\Entity\Subjects', $postArray['subjectName']) 
    ); 
$subjectMarks->setSubject(
     $entityManager->find('Application\Entity\Subjects', $postArray['subjectId']) 
    ); 

、それは完璧になります。 selectタグは、対象の表にすでにあるsubjectNamesのみを出力します。

関連する問題