Symfony2子表单集合,带有已过滤的实体擦除当前实体

时间:2015-09-11 16:26:54

标签: php forms symfony entity subform

抱歉我的英语我是法国人!我会试着解释一下我的问题......

上下文 我想为足球比赛创建比赛报告,每个队长可以添加或删除他自己球队的球员的数据(进球,助攻等)。

问题 一切都在为一个队长工作,我可以添加/删除成功记录在数据库中的玩家,只有当前队长的队员。但是,当另一个想要这样做时,子表单显示我之前记录的统计数据,但是带有团队名称......如果我验证,数据库中的行将被新玩家的名称覆盖。

示例:我输入的结果是我的球员“Player1”得分两次!当我保存时,一切都完全保存在数据库中。 当我想和第二队长做同样的事情时,我的球员“Player2”得分为1球。之前保存的行被player2 ...

的数据覆盖

代码 这是我的代码

简化控制器

    $em = $this->getDoctrine()->getManager();
    $match = $em->getRepository('OCPlatformBundle:Matchs')->findOneById($id);
    $form = $this->get('form.factory')->create(new MatchType(), $match);

    $form->handleRequest($request);

    //VERIF FEUILLE DE MATCH
    foreach($match->getStatsplayer() as $player){
            $player->setDefcleansheet(false);
            $player->setGkcleansheet(false);
            $player->setGame($match);
            $player->setClub($user->getClub());
    }

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($match);

        $em->flush();

MatchType.php

public function buildForm(FormBuilderInterface $builder, array $options)
{

    $builder
      ->add('homescore', 'choice', array('choices'   => array(0,1,2,3,4,5,6,7,8,9,10,11,12)))
      ->add('awayscore', 'choice', array('choices'   => array(0,1,2,3,4,5,6,7,8,9,10,11,12)))
      ->add('statsplayer', 'collection', array(
            'type'         => new StatsType(),
            'allow_add'    => true,
            'allow_delete' => true))
      ->add('save',      'submit')
    ;
}

StatsType.php

public function buildForm(FormBuilderInterface $builder, array $options)
{
    //Players to show is filtered with the querybuilder
    $builder
      ->add('player', 'entity', array(
        'class' => 'OCUserBundle:User',
        'property' => 'username',
        'query_builder' => function(\Doctrine\ORM\EntityRepository $er) {
                global $kernel;
                $user = $kernel->getContainer()->get('security.context')->getToken()->getUser();
                return $er->createQueryBuilder('q')
                ->where('q.club = :pf')
                ->orderBy('q.club', 'ASC')
                ->setParameter('pf', $user->getClub());
            },
        'required' => true, 
        'label' => false,
        ))
      ->add('goals', 'choice', array(
                    'choices'   => array(0,1,2,3,4,5,6,7),
                    'label' => 'goals', 'translation_domain' => 'FOSUserBundle', 'attr' => array(
                'placeholder' => 'goals'
            )))
      ->add('assists', 'choice', array(
                    'choices'   => array(0,1,2,3,4,5,6,7),
                    'label' => 'assists', 'translation_domain' => 'FOSUserBundle', 'attr' => array(
                'placeholder' => 'assists'
            )))

}

感谢您的帮助!

修改

我的实体:

Matchs.php

/**
* Matchs
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="OC\PlatformBundle\Entity\MatchsRepository")
*/
class Matchs
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

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

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

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

/**
 * @ORM\ManyToOne(targetEntity="OC\PlatformBundle\Entity\Club")
 * @ORM\Joincolumn(nullable=false)
 */
private $home;

/**
 * @ORM\ManyToOne(targetEntity="OC\PlatformBundle\Entity\Club")
 * @ORM\Joincolumn(nullable=false)
 */
private $away;

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

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

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

/**
 * @ORM\OneToMany(targetEntity="OC\PlatformBundle\Entity\Stats", mappedBy="game", cascade={"persist"})
 * @Assert\Valid
 */
private $statsplayer;

Stats.php

/**
* Stats
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="OC\PlatformBundle\Entity\StatsRepository")
* @UniqueEntity({"player","game"})
*/
class Stats
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
private $id;

/**
 * @ORM\ManyToOne(targetEntity="OC\UserBundle\Entity\User", cascade={"persist"})
 * @ORM\JoinColumn(nullable=false)
 */
private $player;

/**
 * @ORM\ManyToOne(targetEntity="OC\PlatformBundle\Entity\Club")
 * @ORM\JoinColumn(nullable=false)
 */
private $club;

/**
 * @ORM\ManyToOne(targetEntity="OC\PlatformBundle\Entity\Matchs", inversedBy="statsplayer", cascade={"persist"})
 * @ORM\JoinColumns({
 *  @ORM\JoinColumn(name="game_id", referencedColumnName="id")
 * })
 */
private $game;

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

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

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

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

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

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

0 个答案:

没有答案