2016-09-29 51 views
-2

我正在乌干达Kyambogo大学开展symfony最后一年的项目,以清理不同部门内的学生。 我想单击按钮并清除学生。 我想要这个按钮来提交从学生实体表中呈现的学生的详细信息是 submited到另一个实体ClearedData 即使你不完全使用我的例子,你只是告诉我你可以如何使用它你自己的例子我会很高兴 因为我需要找到一种方式来开始。 这是我的表和清晰的按钮提交的代码是一个我想被点击和这个数据发送到数据库如何发送点击按钮,并在symfony中提交数据库中的学生详细信息

{% extends 'base.html.twig' %} 
{% block body %} 
<div class="box table-responsive"> 
     <div class="box-header"> 
     <h1 class="box-title">Student List</h1> 

     </div> 
     <!-- box-header --> 
     <div class="box-body"> 
      <table id="example1" class="table table-striped"> 
      <thead> 
      <tr> 
      <th>ID</th> 
      <th>First Name</th> 
      <th>Last Name</th> 
      <th>Registration No.</th> 
      <th>Gender</th> 
      <th>Year Of Study</th> 
      <th>Status</th> 
      <th>Actions</th> 
      </tr> 
     </thead> 
     {% for student in students %} 
     <tr> 
      <td>{{ student.id }}</td> 
      <td>{{ student.firstName }}</td> 
      <td>{{ student.lastName }}</td> 
      <td>{{ student.regNo }}</td> 
      <td>{{ student.gender }}</td> 
      <td>{{ student.yearOfStudy }}</td> 
      <td>Cleared/Not Cleared</td> 
      <td><input type="submit" value="Clear" class="btn btn-xs btn-facebook"></td> 
     </tr> 
    {% endfor %} 
<tfoot> 
    </tfoot> 

      </table> 
     </div> 
     <!-- /.box-body --> 
     </div>   
{% endblock %} 

这是我想被提交表单

实体类
<?php 

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* ClearedData 
* 
* @ORM\Table(name="cleared_data") 
* @ORM\Entity(repositoryClass="AppBundle\Repository \ClearedDataRepository") 
* @ORM\HasLifecycleCallbacks() 
*/ 
class ClearedData 
{ 
/** 
* @var int 
* 
* @ORM\Column(name="id", type="integer") 
* @ORM\Id 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
private $id; 

/** 
* @var string 
* 
* @ORM\Column(name="regNo", type="string", length=255) 
*/ 
private $regNo; 

/** 
* @var string 
* 
* @ORM\Column(name="department", type="string", length=255) 
*/ 
private $department; 

/** 
* @var string 
* 
* @ORM\Column(name="status", type="string", length=40) 
*/ 
private $status; 

/** 
* @var \DateTime 
* 
* @ORM\Column(name="date", type="date") 
*/ 
private $date; 


/** 
* Get id 
* 
* @return integer 
*/ 
public function getId() 
{ 
    return $this->id; 
} 

/** 
* Set regNo 
* 
* @param string $regNo 
* @return ClearedData 
*/ 
public function setRegNo($regNo) 
{ 
    $this->regNo = $regNo; 

    return $this; 
} 

/** 
* Get regNo 
* 
* @return string 
*/ 
public function getRegNo() 
{ 
    return $this->regNo; 
} 

/** 
* Set department 
* 
* @param string $department 
* @return ClearedData 
*/ 
public function setDepartment($department) 
{ 
    $this->department = $department; 

    return $this; 
} 

/** 
* Get department 
* 
* @return string 
*/ 
public function getDepartment() 
{ 
    return $this->department; 
} 

/** 
* Set status 
* 
* @param string $status 
* @return ClearedData 
*/ 
public function setStatus($status) 
{ 
    $this->status = $status; 

    return $this; 
} 

/** 
* Get status 
* 
* @return string 
*/ 
public function getStatus() 
{ 
    return $this->status; 
} 

/** 
* Set date 
*@ORM\PrePersist 
* @param \DateTime $date 
* @return ClearedData 
*/ 
public function setDate($date) 
{ 
    $this->date = new \DateTime(); 

    return $this; 
} 

/** 
* Get date 
* 
* @return \DateTime 
*/ 
public function getDate() 
{ 
    return $this->date; 
} 
} 

This is the table with the clear button i want to click and clear a student

回答

0

更改输入提交,使这个:

<a href="{{ path('clear_student', { 'idstudent': student.id }) }}">Clear</a> 

创建路径路由“clear_student:

clear_student: 
    path: /student/clear/{idstudent} 
    defaults: {_controller: YourBundle:TheName:clearStudent} 

,做你的控制器:

<?php 

namespace YourBundle\Controller; 

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 
use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\HttpFoundation\Request; 
use YourBundle\Entity\ClearedData; 

class TheNameController extends Controller 
{ 
    public function clearStudentAction(Request $request, $idstudent) 
    { 
     $em = $this->getDoctrine()->getManager(); 

     $student = $em->getRepository('YourBundle:Student')->findOneById($idstudent); 

(适用于这里像你想你的clearedStudent的必要值)

 $clearedStudent = new ClearedData(); 
     $clearedStudent->setRegNo($student->getRegNo()); 
     $clearedStudent->setDepartment(); 
     $clearedStudent->setStatus(); 
     $clearedStudent->setDate(); 

     $em->persist(clearedStudent); 
     $em->flush(); 

如果需要删除学生:

 $em->remove($student); 
     $em->flush(); 

,并返回到你想要像目标的网址:

 return $this->render('default/targetPage.html.twig', array(
     'example' => $example, //If you have some data to send to the targetPage 
     )); 

    } 
} 

我希望这会帮助你,但我不明白你还没有形成 - >所以donc需要提交按钮。而且你在你的ClearedStudent中有一些不需要的价值,这个价值不会出现在学生身上。所以我希望它会帮助你:)祝你好运

警告:我建议你更新你的两个实体,让它们匹配 - >如果它的目标是将另一个替换为另一个,那么你需要有相同的所需字段。

+0

非常感谢您帮助我解决问题 –

+0

您好,我想知道如何在fosuser包中使用字段,然后我可以给每个学生提供不同的视图以便能够看到他已经清除的部门自己的表格,我还没有粘贴任何代码,因为我只是想知道如何根据用户名作为fosuser包中的字段生成视图以生成视图对于不同的用户,因为我真的不知道从哪里开始,我什么都没有解决 –

相关问题