に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を取っている間totalMarks
とsubjectMarkDesc
が正しく追加されることです。それはsubjectId
に割り当てられているsubjectName
明確に、私たちは入力とsubjectId
が隠されているようsubjectName
が挿入されていることを見ることができるように