dongshao1021 2014-12-15 15:49
浏览 69

在数据库Symfony中添加多对多的关系

I'm trying to add new user with ManyToMany relationship with group, but only user is saving in database

this is my User.php

 /**
 * Acme\UserBundle\Entity\User
 *
 * @ORM\Table(name="User")
 * @ORM\Entity(repositoryClass="Acme\UserBundle\Entity\UserRepository")
 */

class User implements AdvancedUserInterface, \Serializable
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id()
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

/**
 * @ORM\Column(name="username", type="string", length=25, unique=true)
 */
private $username;

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

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

/**
 * @ORM\Column(name="email", type="string", length=60, unique=true)
 */
private $email;

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

/**
 * @ORM\ManyToMany(targetEntity="Group", inversedBy="users")
 *
  * @ORM\JoinTable(name="user_group",
   * joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
 * inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
 * )
 *
 */
private $groups;

public function __construct()
{
    $this->isActive = true;
    $this->salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
    $this->groups = new ArrayCollection();
}








   /**
 * @inheritDoc
 */
    public function getRoles()
    {
      $roles = array();
    foreach ($this->groups as $role) {
        $roles[] = $role->getRole();
    }
        return $roles;

}

 /**
 * @see \Serializable::serialize()
 */
public function serialize()
{
    /*
     * ! Don't serialize $roles field !
     */
    return \json_encode(array(
        $this->id,
        $this->username,
        $this->email,
        $this->salt,
        $this->password,
        $this->isActive
    ));
}

/**
 * @see \Serializable::unserialize()
 */
public function unserialize($serialized)
{
    list (
        $this->id,
        $this->username,
        $this->email,
        $this->salt,
        $this->password,
        $this->isActive
    ) = \json_decode($serialized);
}




public function eraseCredentials()
{
}

public function getUsername()
{
    return $this->username;
}

public function getSalt()
{
    return $this->salt;
}

public function getPassword()
{
    return $this->password;
}
  public function isAccountNonExpired()
{
    return true;
}

public function isAccountNonLocked()
{
    return true;
}

public function isCredentialsNonExpired()
{
    return true;
}

public function isEnabled()
{
    return $this->isActive;
}


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

/**
 * Set username
 *
 * @param string $username
 * @return User
 */
public function setUsername($username)
{
    $this->username = $username;

    return $this;
}

/**
 * Set salt
 *
 * @param string $salt
 * @return User
 */
public function setSalt($salt)
{
    $this->salt = $salt;

    return $this;
}

/**
 * Set password
 *
 * @param string $password
 * @return User
 */
public function setPassword($password)
{
    $this->password = $password;

    return $this;
}

/**
 * Set email
 *
 * @param string $email
 * @return User
 */
public function setEmail($email)
{
    $this->email = $email;

    return $this;
}

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

/**
 * Set isActive
 *
 * @param boolean $isActive
 * @return User
 */
public function setIsActive($isActive)
{
    $this->isActive = $isActive;

    return $this;
}

/**
 * Get isActive
 *
 * @return boolean 
 */
public function getIsActive()
{
    return $this->isActive;
}

/**
 * Add groups
 *
 * @param \Acme\UserBundle\Entity\Group $groups
 * @return User
 */
public function addGroup(\Acme\UserBundle\Entity\Group $groups)
{
    $groups->addUser($this);
    $this->groups -> add($groups);

    return $this->groups;
}

/**
 * Remove groups
 *
 * @param \Acme\UserBundle\Entity\Group $groups
 */
public function removeGroup(\Acme\UserBundle\Entity\Group $groups)
{
    $this->groups->removeElement($groups);
}

/**
 * Get groups
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getGroups()
{
    return $this->groups;
}
}

my Group.php

<?php
namespace Acme\UserBundle\Entity;

use Symfony\Component\Security\Core\Role\RoleInterface;
use Doctrine\Common\Collections\ArrayCollection;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Table(name="groups")
 * @ORM\Entity
 */
class Group implements RoleInterface, \Serializable
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id()
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

/** @ORM\Column(name="role", type="string", length=20, unique=true) */
private $role;

/** @ORM\ManyToMany(targetEntity="User", mappedBy="groups",cascade={"persist"}) */
private $users;

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

// ... getters and setters for each property

/** @see RoleInterface */
public function getRole()
{
    return $this->role;
}

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


/**
 * @see \Serializable::serialize()
 */
public function serialize()
{
    /*
     * ! Don't serialize $users field !
     */
    return \json_encode(array(
        $this->id,
        $this->role
    ));
}

/**
 * @see \Serializable::unserialize()
 */
public function unserialize($serialized)
{
    list(
        $this->id,
        $this->role
    ) = \json_decode($serialized);
}

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

    return $this;
}

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

/**
 * Set role
 *
 * @param string $role
 * @return Group
 */
public function setRole($role)
{
    $this->role = $role;

    return $this;
}

/**
 * Add users
 *
 * @param \Acme\UserBundle\Entity\User $users
 * @return Group
 */
public function addUser(\Acme\UserBundle\Entity\User $users)
{
    $this->users[] = $users;

    return $this;
}

/**
 * Remove users
 *
 * @param \Acme\UserBundle\Entity\User $users
 */
public function removeUser(\Acme\UserBundle\Entity\User $users)
{
    $this->users->removeElement($users);
}

/**
 * Get users
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getUsers()
{
    return $this->users;
}
}

and fragment from my controller

 public function newAction(Request $request)
{

    $user = new User();


    $form = $this->createFormBuilder($user)
        ->add('username', 'text')
        ->add('password', 'text')
        ->add('email', 'email')
        ->add('submit','submit')
        ->getForm();

         $form->handleRequest($request);

         if ($form->isValid()) {

   $user=$form->getData();
   $group=new Group();

   $factory = $this->get('security.encoder_factory');
   $encoder = $factory->getEncoder($user);
   $user->setSalt(md5(time()));
   $pass = $encoder->encodePassword($form->getData()->getPassword(), $user->getSalt());
   $user->setPassword($pass);

  $group->setRole('ROLE_USER');
  $user->addGroup($group);
   $em = $this->getDoctrine()->getManager();



   $em->merge($user);
   $em->flush();



    return $this->redirect($this->generateUrl('login'));
}

    return $this->render('AcmeUserBundle:User:new.html.twig', array(
        'form' => $form->createView(),
    ));

in database i have 3 table : User, Group and user_group (user_id, group_id) everything generated from entities. I can register new user but it isnt saving in user_group.

Thanks for any help.

Ps.Sorry for my poor english

  • 写回答

2条回答 默认 最新

  • dpdp42233 2014-12-15 16:00
    关注

    Update your controller to contain:

    $group = $em->getRepository('AcmeUserBundle:Group')->findOne(array('role' => 'ROLE_USER'));
    $user->addGroup($group);    
    $em->persist($user);
    $em->persist($group);
    $em->flush();
    

    You forgot to persist the Group entity in your code, so it didn't save any data.

    By the way, I suggest using a UserService that handles all that logic instead of putting it all in your controllers.

    Also, in your User.php, you can just do:

    /**
     * @ORM\ManyToMany(targetEntity="Group", inversedBy="user")
     */
    protected $groups;
    

    The @JoinColumn annotation is not required. If it is not specified the attributes name and referencedColumnName are inferred from the table and primary key names.

    评论

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了