在ZF2中将数据添加到mysql表

时间:2017-01-26 08:01:51

标签: php mysql zend-framework2

  

这是添加操作的代码,此操作添加subjectName(来自主题表),totalMarks和Description(来自SubjectMarks表)

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

如果输入数据并单击添加按钮,则此if条件设置为true

    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');
    } 
  

执行第一个运行查询的else并将数据发送到'添加'以'数据'

的形式查看
    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>

问题totalMarkssubjectMarkDesc被正确添加,而subjectName被取为主题表的id [0],因为任何名称都是进入了。

1 个答案:

答案 0 :(得分:0)

答案很简单,因为我们可以看到subjectName作为输入插入而subjectId被隐藏,因此没有直接关系即澄清哪个{ {1}}已分配给subjectName

因此,添加subjectId HTML标记可以解决问题,例如

select
  

在这里,我们可以看到subjectId和subjectName是相关的,与问题

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

同样在控制器中,在 <option value="<?php echo $item['subjectId'];?> "> <?php echo $item['subjectName'];?> </option> <?php endforeach; ?> </select> 条件内,我们只会替换此代码

if (isset($postArray['submit']) && $postArray['subjectName'])
  

使用此代码

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

它会很完美。 select标记将仅输出已存在于主题表中的$subjectMarks->setSubject( $entityManager->find('Application\Entity\Subjects', $postArray['subjectName']) ); $subjectMarks->setSubject( $entityManager->find('Application\Entity\Subjects', $postArray['subjectId']) );