我有3个类,有一个关系。这些类是:用户,组和用户组(此一个具有额外的字段)。
问题(2,但相似)
appbundle entity user
协会AppBundle Entity User#UserGroups是指未定义为关联而是字段的拥有侧面字段AppBundle Entity userGroup#用户。协会AppBundle Entity User#UserGroups指的是拥有的侧面字段AppBundle Entity Entity UserGroup#不存在的用户。
appbundle entity group
协会AppBundle Entity group#UserGroups是指未定义为关联而是字段的side side appbundle entity entity userGroup#组。协会AppBundle Entity group#UserGroups是指不存在的side side appbundle entity userGroup#组。
appbundle/entity/user.php
<?php
// src/AppBundle/Entity/User.php
namespace AppBundleEntity;
use FOSUserBundleModelUser as BaseUser;
use DoctrineORMMapping as ORM;
use AppBundleEntityUserGroup;
/**
* @ORMEntity(repositoryClass="AppBundleRepositoryUserRepository")
* @ORMTable(name="fos_user")
*/
class User extends BaseUser
{
/**Id
* @ORMId
* @ORMColumn(type="integer")
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORMColumn(type="string", length=15)
*/
protected $phone;
/**
* @ORMOneToMany(targetEntity="UserGroup", mappedBy="user")
*/
private $usergroups;
public function __construct()
{
parent::__construct();
$this->usergroups = new DoctrineCommonCollectionsArrayCollection();
}
/**
* Set phone
*
* @param string $phone
* @return User
*/
public function setPhone($phone)
{
$this->phone = $phone;
return $this;
}
/**
* Get phone
*
* @return string
*/
public function getPhone()
{
return $this->phone;
}
/**
* Add usergroups
*
* @param UserGroup $usergroup
* @return User
*/
public function addUsergroup(UserGroup $usergroup)
{
$this->usergroups[] = $usergroup;
return $this;
}
/**
* Remove usergroups
*
* @param UserGroup $usergroup
*/
public function removeUsergroup(UserGroup $usergroup)
{
$this->usergroups->removeElement($usergroup);
}
/**
* Get usergroups
*
* @return DoctrineCommonCollectionsCollection
*/
public function getUsergroups()
{
return $this->usergroups;
}
}
appbundle/entity/group.php
<?php
// src/AppBundle/Entity/Group.php
namespace AppBundleEntity;
use FOSUserBundleModelGroup as BaseGroup;
use DoctrineORMMapping as ORM;
use AppBundleEntityUserGroup;
/**
* @ORMEntity(repositoryClass="AppBundleRepositoryGroupRepository")
* @ORMTable(name="fos_group")
*
*/
class Group extends BaseGroup
{
/**
* @ORMId
* @ORMColumn(type="integer")
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORMColumn(type="string", columnDefinition="ENUM('department', 'workgroup')")
*/
protected $grouptype;
/**
* @ORMOneToMany(targetEntity="UserGroup", mappedBy="group")
*/
private $usergroups;
public function __construct()
{
parent::__construct('',array());
$this->usergroups = new DoctrineCommonCollectionsArrayCollection();
}
/**
* Is the given User the author of this Post?
*
* @param User $user
*
* @return bool
*/
public function isAdmin(User $user)
{
//$users_tmp = $this->getUsergroups();
//exit(DoctrineCommonUtilDebug::dump($users_tmp));
/*
foreach ($this->usergroups as $user) {
if ($user->)
$valor = $valor * 2;
}
return $user->id() === $this->getId();
*/
return true;
}
/**
* Set grouptype
*
* @param string $grouptype
* @return Group
*/
public function setGrouptype($grouptype)
{
$this->grouptype = $grouptype;
return $this;
}
/**
* Get grouptype
*
* @return string
*/
public function getGrouptype()
{
return $this->grouptype;
}
/**
* Add usergroups
*
* @param AppBundleEntityUserGroup $usergroups
* @return Group
*/
public function addUsergroup(AppBundleEntityUserGroup $usergroups)
{
$this->usergroups[] = $usergroups;
return $this;
}
/**
* Remove usergroups
*
* @param AppBundleEntityUserGroup $usergroups
*/
public function removeUsergroup(AppBundleEntityUserGroup $usergroups)
{
$this->usergroups->removeElement($usergroups);
}
/**
* Get usergroups
*
* @return DoctrineCommonCollectionsCollection
*/
public function getUsergroups()
{
return $this->usergroups;
}
}
AppBundle/Entity/userGroup.php
<?php
namespace AppBundleEntity;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
use SymfonyComponentValidatorConstraints as Assert;
/**
* @ORMEntity(repositoryClass="AppBundleRepositoryUserGroupRepository")
* @ORMTable(name="fos_user_group")
*/
class UserGroup
{
/**
* @ORMId
* @ORMGeneratedValue
* @ORMColumn(type="integer")
*/
private $id;
/**
* @ORMColumn(type="integer")
* @ORMManyToOne(targetEntity="AppBundleEntityUser", inversedBy="usergroups", cascade={"persist"})
* @ORMJoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;
/**
* @ORMColumn(type="integer")
* @ORMManyToOne(targetEntity="AppBundleEntityGroup", inversedBy="usergroups", cascade={"persist"})
* @ORMJoinColumn(name="group_id", referencedColumnName="id")
*/
private $group;
/**
* @ORMColumn(type="string")
*/
private $role;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set user
*
* @param integer $user
* @return UserGroup
*/
public function setUser($user)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return integer
*/
public function getUser()
{
return $this->user;
}
/**
* Set group
*
* @param integer $group
* @return UserGroup
*/
public function setGroup($group)
{
$this->group = $group;
return $this;
}
/**
* Get group
*
* @return integer
*/
public function getGroup()
{
return $this->group;
}
/**
* Set role
*
* @param string $role
* @return UserGroup
*/
public function setRole($role)
{
$this->role = $role;
return $this;
}
/**
* Get role
*
* @return string
*/
public function getRole()
{
return $this->role;
}
}
请任何帮助。谢谢...
第一个错误:
AppBundleEntityUserGroup#user
您添加了映射@ORMColumn(type="integer")
-删除它,映射器知道如何定义关系。
/**
* @ORMColumn(type="integer")
* @ORMManyToOne(targetEntity="AppBundleEntityUser", inversedBy="usergroups", cascade={"persist"})
* @ORMJoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;
to
/**
* @ORMManyToOne(targetEntity="AppBundleEntityUser", inversedBy="usergroups", cascade={"persist"})
* @ORMJoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;
第二个错误 - 相同的情况:
/**
* @ORMColumn(type="integer")
* @ORMManyToOne(targetEntity="AppBundleEntityGroup", inversedBy="usergroups", cascade={"persist"})
* @ORMJoinColumn(name="group_id", referencedColumnName="id")
*/
private $group;
列与关联不同。我认为,这就是全部:)
您将其注释错误。以下是注释多个关系的示例
/**
* @ORMManyToOne(targetEntity="Group")
* @ORMJoinColumn(name="group_id", referencedColumnName="id", onDelete="persist")
*/
以下链接可以帮助您更多。映射信息谢谢