【问题标题】:symfony2 entity oneToMany and methodssymfony2 实体 oneToMany 和方法
【发布时间】:2013-03-17 10:40:32
【问题描述】:

您好,我已经完全成功地设置了我的实体 onetoMany 和 ManyToOne,我生成了 setter 和 getter,并在用户实体中创建了这个方法:

用户实体:

    /**
 * @ORM\OneToMany(targetEntity="TB\RequestsBundle\Entity\Requests", mappedBy="followeeuser")
 */
protected $followees;   

请求实体:

/**
 * @ORM\ManyToOne(targetEntity="TB\UserBundle\Entity\User", inversedBy="followees")
 * @ORM\JoinColumn(name="followee_id", referencedColumnName="id", nullable=false)
 */ 
protected $followeeuser;

当我使用我自己的自定义查询时,它工作得很好......但我无法弄清楚如何使用 symfony 生成的这个函数:

    public function addFollowee(\TB\UserBundle\Entity\User $followee)
{
    $this->followees[] = $followee;
}  

我不知道该传递什么...我尝试首先根据用户的 id 从树枝获取用户对象...效果很好,但发生错误:

$user->addFollowee($userRepository->find($target_user_id));

Found entity of type TB\UserBundle\Entity\User on association TB\UserBundle\Entity\User#followees, but expecting TB\RequestsBundle\Entity\Requests

【问题讨论】:

  • 这个函数是由 Symfony2 创建的?它应该被称为:public function addFollowee(TB\RequestsBundle\Entity\Requests $followee)
  • 是的,即使我会传递给它什么?
  • 我不知道你在那里做什么。但如果您尝试为用户设置“关注”功能,请看这里:docs.doctrine-project.org/projects/doctrine-orm/en/latest/…
  • 我看到了这十亿次 :) 我无法使用它,因为我的实体中有额外的字段......我正在尝试通过像 addFollowee 这样的用户设置追随者......并且不知道我需要放什么那里...现在我这样做了 => 请求存储库,然后是 new Requests() 并填写所有字段...但我正在寻找最好的方法

标签: symfony entity many-to-one relationships


【解决方案1】:

也许你应该在编码之前考虑一下你想要做什么。拿起一支笔和一张纸。 :)

如果我错了,请告诉我,但这是我认为你正在尝试做的事情:

一个用户可以有多个“关注者”。 一个“关注者”可以有一个用户。

所以,OneToMany 关系是可以的。

以下是如何编写它,来自文档:

Requests.php(顺便说一句,你应该使用 Request.php)

/**
 * @ORM\ManyToOne(targetEntity="User", inversedBy="requests")
 **/
private $user;

用户.php

/**
 * @ORM\OneToMany(targetEntity="Requests", mappedBy="user", cascade={"all"})
 **/
private $requests;

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

现在您可以检查您的关系是否正常,并更新您的架构:

php app/console doctrine:schema:validate
php app/console doctrine:schema:update --force

关于 getter/setter :

Requests.php

public function getUser()
{
    return $this->user;
}

public function setUser(User $user) // Please add a Use statement on top of your document
{
    $this->user = $user;
    return $this;
}

用户.php

public function addRequest(Requests $request)
{
    $this->requests->add($request);
    return $this;
}

public function removeRequest(Requests $request)
{
    $this->requests->removeElement($request);
    return $this;
}

// Get requests and set requests (you know how to write those ones)

现在,要将用户设置为请求,请使用

$request->setUser($user);

要向用户添加请求,请使用

$user->addRequest($request);

【讨论】:

  • hm...您的代码几乎相同...我想...这是什么请求? $request->setUser($user);它的 $request=new Request(); ?因为那么它会抛出除 setUser one ofc 之外的所有空字段的错误。
  • 你在开玩笑吗?从你的代码:'TB\RequestsBundle\Entity\Requests' 我不知道它来自哪里,这取决于你的应用程序!这是您要添加到用户的实体!
  • ...你没有明白我的意思...我的意思是一切都很清楚...就在最后一个代码...你写了 $request->setUser($user);是的,肯定 $request 代表一个请求实体......但我将如何“创建,调用”它?我必须像我所说的那样在我的控制器中写 $request=new Request() 吗?或者以某种方式不同。你得到了我的应用程序。在控制器中添加关注者...基于我们的实体。
  • 我不知道您的应用程序是什么,也不知道您希望如何创建请求。如果要通过表单创建请求,请查看表单文档:symfony.com/doc/master/book/forms.html 如果要创建新请求,只需使用 new Requests() 并填写所需数据
猜你喜欢
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-03
  • 1970-01-01
相关资源
最近更新 更多