在 ZF2 中向 mysql table 添加数据
Adding data to mysql table in ZF2
This is the code for add action, This action adds subjectName(from subjects Table), totalMarks and Description (from SubjectMarks Table)
public function addAction() {
$postArray = $this->params()->fromPost();
$entityManager = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
This if condition is set true if data is entered and addbutton is clicked
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');
}
First else is executed that runs the query and sends data to 'add' view in form of 'data'
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,
)
);
}
}
This is the code for add.phtml 'FORM' which is called when 'else' part executes and query selects the data required, and send the written data back, where it is received by if() condition.
<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>
问题是totalMarks
和subjectMarkDesc
添加正确,而subjectName
取了主题[的id[0] =29=],对于输入的任何名称。
答案很简单,我们可以看到subjectName
作为输入被插入而subjectId
被隐藏,所以没有直接关系即, 阐明哪个 subjectName
分配给 subjectId
.
所以放一个 select
HTML 标签就可以解决问题,例如
<select name="addSubjectMarks">
<?php foreach ($this->data as $item):?>
Here we can see subjectId and subjectName are related, unlike the question
<option value="<?php echo $item['subjectId'];?> "> <?php echo $item['subjectName'];?> </option>
<?php endforeach; ?>
</select>
Similarly there in the controller, inside the if (isset($postArray['submit']) && $postArray['subjectName'])
condition, we will only replace this code
$subjectMarks->setSubject(
$entityManager->find('Application\Entity\Subjects', $postArray['addSubjectMarks'])
);
with this code
$subjectMarks->setSubject(
$entityManager->find('Application\Entity\Subjects', $postArray['subjectName'])
);
$subjectMarks->setSubject(
$entityManager->find('Application\Entity\Subjects', $postArray['subjectId'])
);
and it will perfectly. The select tag will output only those subjectNames
which are already present in the subject Table.
This is the code for add action, This action adds subjectName(from subjects Table), totalMarks and Description (from SubjectMarks Table)
public function addAction() {
$postArray = $this->params()->fromPost();
$entityManager = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
This if condition is set true if data is entered and addbutton is clicked
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');
}
First else is executed that runs the query and sends data to 'add' view in form of 'data'
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,
)
);
}
}
This is the code for add.phtml 'FORM' which is called when 'else' part executes and query selects the data required, and send the written data back, where it is received by if() condition.
<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>
问题是totalMarks
和subjectMarkDesc
添加正确,而subjectName
取了主题[的id[0] =29=],对于输入的任何名称。
答案很简单,我们可以看到subjectName
作为输入被插入而subjectId
被隐藏,所以没有直接关系即, 阐明哪个 subjectName
分配给 subjectId
.
所以放一个 select
HTML 标签就可以解决问题,例如
<select name="addSubjectMarks">
<?php foreach ($this->data as $item):?>
Here we can see subjectId and subjectName are related, unlike the question
<option value="<?php echo $item['subjectId'];?> "> <?php echo $item['subjectName'];?> </option>
<?php endforeach; ?>
</select>
Similarly there in the controller, inside the
if (isset($postArray['submit']) && $postArray['subjectName'])
condition, we will only replace this code
$subjectMarks->setSubject(
$entityManager->find('Application\Entity\Subjects', $postArray['addSubjectMarks'])
);
with this code
$subjectMarks->setSubject(
$entityManager->find('Application\Entity\Subjects', $postArray['subjectName'])
);
$subjectMarks->setSubject(
$entityManager->find('Application\Entity\Subjects', $postArray['subjectId'])
);
and it will perfectly. The select tag will output only those
subjectNames
which are already present in the subject Table.