我有2个实体 - 用户和标签。
这是我的用户:
<?php
namespace Project\Model;
/**
* @Entity
* @Table(name="users")
* @InheritanceType("JOINED")
* @DiscriminatorColumn(name="discr", type="string")
* @DiscriminatorMap({"user" = "User", "client" = "Client", "staff" = "Staff"})
**/
class User implements \JsonSerializable {
/** @Id @Column(type="integer") @GeneratedValue **/
protected $id;
/** @Column(type="string", name="first_name") **/
protected $firstName;
/**
* @ManyToMany(targetEntity="Project\Model\Tag", inversedBy="users")
* @JoinTable(name="user_tags")
**/
protected $tags;
/**
* Construct a new user.
*/
public function __construct() {
$this->tags = new \Doctrine\Common\Collections\ArrayCollection();
}
// Getters
public function getId() {
return $this->id;
}
public function getFirstName() {
return $this->firstName;
}
public function getTags() {
return $this->tags;
}
// Setters
public function setFirstName($firstName) {
$this->firstName = $firstName;
}
/**
* Add a tag to a user.
* @param Tag
*/
public function addTag(Tag $tag) {
$tag->addUser($this);
$this->tags[] = $tag;
}
}
这是我的标签:
<?php
namespace Project\Model;
/**
* @Entity
* @Table(name="tags")
**/
class Tag implements \JsonSerializable {
/** @Id @Column(type="integer") @GeneratedValue **/
protected $id;
/** @Column(type="string") **/
protected $tag;
/**
* @ManyToMany(targetEntity="Project\Model\User", mappedBy="tags")
*/
protected $users;
public function __construct() {
$this->users = new \Doctrine\Common\Collections\ArrayCollection();
}
// Getters
public function getId() {
return $this->id;
}
public function getTag() {
return $this->tag;
}
// Setters
public function setTag($tag) {
$this->tag = $tag;
}
public function addUser(User $user) {
$this->users[] = $user;
}
}
如果我创建一个新标签,一个新用户,将标签添加到用户,然后调用getTag()方法,它什么也不返回 - 任何人都可以帮我解决我出错的地方吗?
$tag = new Tag();
$tag->setTag('Foo');
$entityManager->persist($tag);
$user = new User();
$user->addTag($tag);
$entityManager->persist($user);
$entityManger->flush();
var_dump($user->getTags());
答案 0 :(得分:0)
我认为问题可能来自你的ManyToMany关系。你用
for name, group in data.groupby('hour'):
d = {'group_' + str(name) : group}
然而你应该有类似的东西(假设你当然使用Symfony 2):
@ManyToMany(targetEntity="Tag", inversedBy="users")
此外,您使用@ManyToMany(targetEntity="AppBundle\Entity\Tag")
但不使用inversedBy
,因此您的映射无效。
最后一个更详细,但在Tag类中命名属性“tag”并不是最干净的。也许把它改成“名字”。
答案 1 :(得分:0)
在Tag类中,使用以下方法引用User类:
/**
* @ManyToMany(targetEntity="Bix\Model\User", mappedBy="tags")
*/
“Bix”应该是“Project”吗?除非这是你问题中的拼写错误,否则会导致问题。
一方应“拥有”该关联,并负责在添加关联时设置反关联。
<?php
// Assuming that the User is the "owning side".
class User {
// Mappings as you have them, minus the "Bix" namespace thing.
public function getTags()
{
return $this->tags;
}
public function addTag(Tag $tag)
{
$tag->addUser($this);
$this->tags->add($tag);
}
public function removeTag(Tag $tag)
{
$tag->removeUser($this);
$this->tags->removeElement($tag);
}
}
class Tag {
// Mappings as you have them, minus the "Bix" namespace thing.
public function getUsers()
{
return $this->users;
}
public function addUser(User $user)
{
$this->users->add($user);
}
public function removeUser(User $user)
{
$this->users->removeElement($user);
}
}