Symfony 2阵列成形

时间:2016-06-30 14:57:55

标签: php forms symfony

所以我解释了我的问题 我们可以看到here,我有两个实体(用户和模块)通过ManyToMany关联链接两个额外的字段(注意和观察)。似乎与交响乐这种关系似乎是不可能的。为了解决这个问题,我创建了一个第三个实体(Result),它通过ManyToOne关系链接到其他实体。我将2个额外字段定义为第三个实体的属性。

现在我必须制作一个表格,将学生的结果保存到数据库中。我不想逐个保存每个学生的结果,但我希望(如the picture所示)一个包含所有用户的表单,并且只有一个提交,我保存所有结果表单中的用户到数据库。

以下是我的实体,控制器和表单的代码:

实体User.php

class User extends BaseUser {
protected $id;
/**@ORM\OneToMany(targetEntity="App\ScholarityBundle\Entity\Result", mappedBy="markUserIds")     */
protected $markUserIds;
public function __construct() {
    parent::__construct();
    $this->markUserIds = new ArrayCollection();
}
public function getMarkUserIds()    {
    return $this->markUserIds;
}

Entity Module.php

class Module{
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;
/**
 * @ORM\OneToMany(targetEntity="App\ScholarityBundle\Entity\Result", mappedBy="markClassCoursesIds")
 */
protected $markClassCoursesIds;
public function __construct()    {
    $this->markClassCoursesIds = new ArrayCollection();
}
public function getId()    {
    return $this->id;
}    
public function addMarkClassCoursesId(\App\ScholarityBundle\Entity\Result $markClassCoursesIds)    {
    $this->markClassCoursesIds[] = $markClassCoursesIds;
    return $this;
}
public function getMarkClassCoursesIds()    {
    return $this->markClassCoursesIds;
}

}

实体Result.php

class Result{
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;
/**
 * @ORM\Column(type="integer", length=2)
 */
private $mark;
/**
 * @ORM\Column(type="string", length=100)
 */
private $observation;
/**
 * @ORM\ManyToOne(targetEntity="App\SettingBundle\Entity\Module", inversedBy="markClassCoursesIds")
 * @ORM\JoinColumn(name="class_courses_id", referencedColumnName="id")
 */
private $markClassCoursesId;
/**
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="markUserIds")
 * @ORM\JoinColumn(name="mark_user_Id", referencedColumnName="id")
 */
private $markUserId;

public function getId()    {
    return $this->id;
}
public function setMark($mark)    {
    $this->mark = $mark;
    return $this;
}
public function getMark()    {
    return $this->mark;
}
public function setObservation($observation)    {
    $this->observation = $observation;
    return $this;
}
public function getObservation()    {
    return $this->observation;
}
public function setMarkClassCoursesId(\App\SettingBundle\Entity\Module $markClassCoursesId = null)    {
    $this->markClassCoursesId = $markClassCoursesId;
    return $this;
}
public function getMarkClassCoursesId()    {
    return $this->markClassCoursesId;
}
public function setMarkUserId(\AppBundle\Entity\User $markUserId = null)    {
    $this->markUserId = $markUserId;
    return $this;
}
public function getMarkUserId()    {
    return $this->markUserId;
}

}

Controller ResultController.php

class ResultController extends Controller{
public function newAction(Request $request)
{
    $result = new Result();
    $em = $this->getDoctrine()->getEntityManager();
    $repository = $em->getRepository('AppBundle:User');
    $users= $repository->findAllStudent();
    $form = $this->createForm(new ResultType(), $result, array('users' => $users));
    if ($request->isMethod('POST')) {
        $form->handleRequest($request);
        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($result);
            $em->flush();
            return $this->redirect($this->generateUrl('scholarity_coursesmark')); 
        }
    }
    return $this->render('AppScholarityBundle:result:new.html.twig', array(
        'form' => $form->createView(),
    )); 
}

}

表单CustomResultType.php

class CustomResultType extends AbstractType{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('mark');
    $builder->add('observation');
    $builder->add('markUserId');
}

}

表格ResultType.php

class ResultType extends AbstractType{
public function buildForm(FormBuilderInterface $builder, array $options)    {
    foreach ($options['users'] as $user) {
        $builder->add('markUserId' . $user->getId(), CustomResultType::class, [
            'data' => $user,
        ]);
    }
}
public function configureOptions(OptionsResolver $resolver)    {
    $resolver->setDefaults(array(
        'data_class' => 'App\ScholarityBundle\Entity\Result',
        'users' => null,
    ));
}
public function getName()    {
    return 'result';
}

}

1 个答案:

答案 0 :(得分:1)

使用Symfony的Form组件实际上非常容易。首先,您需要创建一个自定义类型,例如UserModuleType代表一个用户(或在您的上一张图片中:一行)。

现在你可以创建另一种类型,我们称之为MultipleUserModuleType(可能更好地命名)。在里面你可以做像

这样的事情
foreach ($options['users'] as $user) {
    $builder->add('usermodule' . $user->getId(), UserModuleType::class, [
        'data' => $user,
    ]);
}

或直接使用CollectionType之类的(未经测试)

$builder->add('usermodules', CollectionType::class, [
    'entry_type' => UserModuleType::class,
]);