FOSUserBundle and persisting in database

I have a User entity class that inherits from FOSUserBundle BaseUser class. On the other side an Avis entity class.

/**
 * Avis
 *
 * @ORMTable()
 * @ORMEntity
 */
 class Avis
 {
   /**
     * @var integer
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
     private $id;

     /**
      * @var integer
      *
      * @AssertNotBlank(message = "Merci d'attribuer une note", groups = {"Default"})
      *
      * @ORMColumn(name="note", type="smallint")
      */
      private $note;

    /**
      * @var string
      *
      * @AssertNotBlank(message = "Merci d'entrer un message", groups = {"Default"})
      * @AssertLength(max = 500, maxMessage = "Votre message doit contenir au maximum {{ limit }} caractères de longueur", groups = {"Default"})
      *
      * @ORMColumn(name="message", type="string", length=500)
      */
      private $message;

    /**
      * @AssertType(type="LeJardinEbeneBundleEntityUser")
      *
      * @ORMOneToOne(targetEntity="LeJardinEbeneBundleEntityUser", cascade={"persist"}, inversedBy="avis")
      * @ORMJoinColumn(nullable=true)
      */
      private $user;



/**
  * User
  *
  * @ORMTable(name="fos_user")
  * @ORMEntity
  */
  class User extends BaseUser
  {
   /**
     * @var integer
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
     protected $id;

   /**
     * @AssertType(type="LeJardinEbeneBundleEntityAvis")
     *
     * @ORMOneToOne(targetEntity="LeJardinEbeneBundleEntityAvis", mappedBy="user")
     * @ORMJoinColumn(nullable=true)
     */
     protected $avis;

So Avis is the owner of this bidirectional relation. In my form I display Avis object fields and when validating the form I would like :

  • Avis to be persisted in the database (that already works)
  • Relation to be persisted also => user_id in Avis table and avis_id in fos_user table
  • Here is what I tried without success :

        $user = $this->getUser();
        $form = $this->createForm('avis', $avis);
        $form->handleRequest($request);
        $avis = new Avis();if ($form->isValid()) {
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($avis);
            $entityManager->flush();
            if($user) {
                $userManager = $this->get('fos_user.user_manager');
                $userManager->updateUser($avis->getUser(), true);
            }
    

    Thanks. Regards,


    With a one-to-one and a many-to-many only one table gets a foreign-index column. In your case the "avis" table will get a column user_id. So you will only have to set the $user property before you persist. Doctrine will set the $avis property in the User class automatically after (re)loading.

     $user = $this->getUser();
     $avis = new Avis();
     $form = $this->createForm('avis', $avis);
    
     $form->handleRequest($request);
     if ($form->isValid()) {
            $entityManager = $this->getDoctrine()->getManager();
    
            $avis->setUser($user);
    
            $entityManager->persist($avis);
            $entityManager->flush();
    
     }
    
    链接地址: http://www.djcxy.com/p/91690.html

    上一篇: 如何更改FosUserBundle表单中的数据接收格式?

    下一篇: FOSUserBundle并坚持数据库