我想在我的联合表RoleUser idUser和idRole中插入,但是在函数中我应该添加一个对象user和role
我该怎么做?
联合表 RoleUser :
/**
* RoleUser
*
* @ORM\Table(name="role_user", indexes={@ORM\Index(name="fk_role_user_id", columns={"ref_user_id"}), @ORM\Index(name="fk_role_id", columns={"ref_role_id"})})
* @ORM\Entity(repositoryClass="AppBundle\Repository\RoleUserRepository")
*/
class RoleUser
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var \AppBundle\Entity\Role
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Role")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="ref_role_id", referencedColumnName="id")
* })
*/
private $refRole;
/**
* @var \AppBundle\Entity\User
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\User")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="ref_user_id", referencedColumnName="id")
* })
*/
private $refUser;
/**
* @param Role $refRole
*/
public function setRefRole(\AppBundle\Entity\Role $refRole)
{
$this->refRole = $refRole;
}
/**
* @param User $refUser
*/
public function setRefUser(\AppBundle\Entity\User $refUser)
{
$this->refUser= $refUser;
}
}
在我的控制器中,我想插入以下内容(对于特殊情况,我应该在后台插入,用户无法选择其角色):
$user = new User();
$role= new Role();
$roleUser =new RoleUser();
$roleUser->setRefUser($user->getId());
$roleUser->setRefRole(1);
但是我知道我应该通过一个用户和一个角色:
$roleUser->setRefUser($user);
$roleUser->setRefRole($role);
答案 0 :(得分:0)
您需要使用一个ManyToMany关系而不是OneToMany并删除您的Entity RoleUser。您可以按照文档官员中的下一个example来映射这种关系。
保存:
在这种情况下,您需要在两个实体中添加此关系:
<?php
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Location
*
* @ORM\Table(name="location")
* @ORM\Entity
*/
class Location
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* <p>Represent the </p>
* @ORM\ManyToMany(targetEntity="LogoLocation", mappedBy="locations")
*/
private $logoLocationCurse;
/**
* Location constructor.
*/
public function __construct()
{
$this->logoLocationCurse = new ArrayCollection();
}
public function addlogoLocationCurse(LogoLocation $location)
{
$this->logoLocationCurse->add($location);
$location->addLocations($this);
}
public function removeLogo(LogoLocation $location)
{
$this->logoLocationCurse->removeElement($location);
$location->removeLocation($this);
}
}
<?php
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use \DateTime;
/**
* Class LogoLocation
* @ORM\Table(name="logo_location_curso")
* @ORM\Entity
*/
class LogoLocation
{
/**
* @var integer
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* Many Users have Many Groups.
* @ORM\ManyToMany(targetEntity="Location", inversedBy="logocurso")
* @ORM\JoinTable(name="logo_locations")
*/
private $locations;
/**
* LogoLocation constructor.
*/
public function __construct()
{
$this->locations = new ArrayCollection();
}
/**
* @param Location $location
*/
public function addLocations(Location $location)
{
$this->locations->add($location);
}
/**
* @param Location $location
*/
public function removeLocation(Location $location)
{
$this->locations->removeElement($location);
}
/**
*
*/
public function removeLocations()
{
/** @var Location $location */
foreach ($this->locations as $location) {
$location->removeLogo($this);
}
}
}
然后冲洗