Symfony3尝试在连接表中添加记录

时间:2017-06-15 11:36:43

标签: php symfony doctrine symfony-forms

用户有很多游戏。 游戏可以属于许多用户。

我有一个包含游戏列表的表单,我希望将游戏列表添加到当前登录的用户中。

当我提交表单时没有任何反应,我希望至少有1条记录添加到users_games:

enter image description here

更新

  • 添加了用户实体

FormType addGameToUserType:

namespace AppBundle\Form;

use AppBundle\Entity\Game;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;

class addGameToUserType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('game', EntityType::class, [
            'class' => 'AppBundle:Game',
            'choice_label' => function ($game) {
                return $game->getName();
            },
            'multiple' => true,
            'expanded' => false,
        ]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {

    }

    public function getBlockPrefix()
    {
        return 'app_bundleadd_game_to_user';
    }
}

UserController addGameAction:

    /**
     * Adds game(s) to current user.
     *
     * @Route("user/game/add", name="game_add")
     * @Method({"GET", "POST"})
     */
    public function addGameAction(Request $request)
    {
        /** @var  $form */
        $form = $this->createForm('AppBundle\Form\addGameToUserType');
        $form->handleRequest($request);


        if ($form->isSubmitted() && $form->isValid()) {
            echo $form->get('game')->getData();

            $em = $this->getDoctrine()->getManager();


            /** @var  $game */
            $game = new Game();

            $game->getId();

            /** @var User $userObject */
            $userObject = $this->getUser();

            $user = $em->getRepository('AppBundle:User')
                ->find(['id' => $userObject->getId()]);


            $game->addGameUser($user);
            $em->persist($user);
            $em->flush();
        }


        return $this->render('user/addGame.html.twig', array(
            'form' => $form->createView()
        ));
    }

游戏实体:

    <?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Game
 *
 * @ORM\Table(name="game")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\GameRepository")
 */
class Game
{
    /**
     * @ORM\OneToMany(targetEntity="PlayLog", mappedBy="game")
     * @ORM\OrderBy({"date" = "DESC"})
     *
     */
    private $playlogs;


    private $users;




    /**
     * @return ArrayCollection
     */
    public function getUsers()
    {
        return $this->users;
    }

    /**
     * @param ArrayCollection $users
     */
    public function setUsers($users)
    {
        $this->users = $users;
    }

//    private $categories;

    public function __construct()
    {
        $this->playlogs = new ArrayCollection();
        $this->users = new ArrayCollection();
    }
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;


    /**
     * @var string
     * @Assert\NotBlank()
     * @Assert\Length(
     *     min = "3",
     *  max = "100"
     * )
     * @ORM\Column(name="name", type="string", length=255, unique=true)
     */
    private $name;


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



    /**
     * Set name
     *
     * @param string $name
     *
     * @return Game
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

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



    /**
     * @return mixed
     */
    public function getPlaylogs()
    {
        return $this->playlogs;
    }

    /**
     * @param mixed $playlogs
     */
    public function setPlaylogs($playlogs)
    {
        $this->playlogs = $playlogs;
    }

    public function addPlayLog(PlayLog $playlog)
    {
        $this->playlog->add($playlog);
        $playlog->setPlayLogs($this);
    }


    public function addGameUser(User $user){
        $this->users[] = $user;
    }
}

用户实体:     

namespace AppBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser
{
    /**
     * @ORM\ManyToMany(targetEntity="Game")
     *
     * @ORM\JoinTable(name="users_games",
     *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="game_id", referencedColumnName="id")}
     *      )
     */
    private $games;


    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\PlayLog", mappedBy="user")
     *
     */
    private $playlogs;

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

    /**
     * @param mixed $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }


    public function __construct()
    {
        $this->games = new ArrayCollection();
    }

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;


    /**
     * @return mixed
     */
    public function getGames()
    {
        return $this->games;
    }

    /**
     * @param mixed $games
     */
    public function setGames($games)
    {
        $this->games = $games;
    }

    public function addGame(Game $game)
    {
//        $this->games->add($game);
        $this->games[] = $game;
        return $this;
    }
    public function removeGame(Game $game)
    {
        $this->games->removeElement($game);
    }


    /**
     * @return mixed
     */
    public function getPlaylogs()
    {
        return $this->playlogs;
    }

    /**
     * @param mixed $playlogs
     */
    public function setPlaylogs($playlogs)
    {
        $this->playlogs = $playlogs;
    }

    public function addPlayLog(PlayLog $playlog)
    {
        $this->playlog->add($playlog);
        $playlog->setPlayLogs($this);
    }


}

0 个答案:

没有答案