symfony - 将字段插入联合表原则
问题描述
我想插入我的联合表 RoleUser idUser 和 idRole ,但在函数中我应该添加一个对象用户和角色
我怎样才能做到这一点?
联合表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);
解决方案
您需要使用关系 ManyToMany 而不是 OneToMany 并删除您的 Entity RoleUser。您可以按照官方文档中的下一个示例来映射这种关系。
为了保存:
在这种情况下,您需要在两个实体中添加此关系:
<?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);
}
}
}
并进行冲洗
推荐阅读
- web-applications - 网站如何确定信用卡号是否有效?
- ios - 忽略 R.Swift 中的 segue 资源
- redis - 为什么我在redis中设置了requirepass密码后它不起作用?
- c# - 是否可以使用控制台应用程序作为 selenium webscraper 的传递来获取值并将其传递回主程序?
- python - 在python中有效地使用条件语句而不是字典
- python - 如何在 Python 中转换大写名称?
- c# - Azure 事件网格订阅 VM 上的控制台应用程序
- javascript - JS while循环不会停止在true
- android - 使用 URL 方案的 Google Pay UPI 集成 - iOS
- arrays - Azure 逻辑应用程序,从提取存档到文件夹 FTP 连接器的数组中设置对象变量