Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
symfony2实体一域和方法_Symfony_Entity_Many To One_Relationships - Fatal编程技术网

symfony2实体一域和方法

symfony2实体一域和方法,symfony,entity,many-to-one,relationships,Symfony,Entity,Many To One,Relationships,您好,我已经完全成功地设置了我的实体onetoMany和我生成的许多Setter和Getter,并在用户实体中创建了此方法: 用户实体: /** * @ORM\OneToMany(targetEntity="TB\RequestsBundle\Entity\Requests", mappedBy="followeeuser") */ protected $followees; 请求实体: /** * @ORM\ManyToOne(targetEntity="TB\UserB

您好,我已经完全成功地设置了我的实体onetoMany和我生成的许多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从twig获取用户对象。。。工作正常,但出现错误:

$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

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

如果我错了,请告诉我,但以下是我认为您正试图做的:

一个用户可以有多个“followee”。 一个“followee”可以有一个用户

所以,一对一的关系是可以的

下面是如何从文档中编写它:

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

User.php

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

public function __construct()
{
    $this->requests = new \ArrayCollection();
}
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)
现在,您可以检查您的关系是否正常,并更新您的模式:

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;
}
User.php

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

public function __construct()
{
    $this->requests = new \ArrayCollection();
}
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);

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

如果我错了,请告诉我,但以下是我认为您正试图做的:

一个用户可以有多个“followee”。 一个“followee”可以有一个用户

所以,一对一的关系是可以的

下面是如何从文档中编写它:

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

User.php

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

public function __construct()
{
    $this->requests = new \ArrayCollection();
}
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)
现在,您可以检查您的关系是否正常,并更新您的模式:

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;
}
User.php

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

public function __construct()
{
    $this->requests = new \ArrayCollection();
}
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);

此函数是由Symfony2创建的?应该调用它:
public function addFollowee(TB\RequestsBundle\Entity\Requests$followee)
yes,即使我传递给它什么?我不知道你在那里做什么。但是,如果你试图为用户设置一个“以下”功能,请看这里:我多次看到这个功能:)我无法使用它,因为我的实体中有额外的字段。。。我正在尝试通过像addFollowee这样的用户设置跟随者。。不知道我需要在那里放什么。。。现在我像=>RequestRepository那样做,然后是new Requests(),并填充所有字段。。。但是我正在寻找这个函数由Symfony2创建的最佳方式?应该调用它:
public function addFollowee(TB\RequestsBundle\Entity\Requests$followee)
yes,即使我传递给它什么?我不知道你在那里做什么。但是,如果你试图为用户设置一个“以下”功能,请看这里:我多次看到这个功能:)我无法使用它,因为我的实体中有额外的字段。。。我正在尝试通过像addFollowee这样的用户设置跟随者。。不知道我需要在那里放什么。。。现在我像=>RequestRepository那样做,然后是new Requests(),并填充所有字段。。。但是我在寻找最好的方法。。。你的代码几乎是一样的。。。我想。。。这是什么要求$请求->设置用户($user);它的$request=newrequest()?因为它会对所有空字段抛出错误,除了setUser one ofc。你在开玩笑吗?从您的代码:“TB\RequestsBundle\Entity\Requests”我不知道它来自哪里,这取决于您的应用程序!它是您要添加到用户中的实体!。。。你没有得到我。。。我是说一切都很清楚。。。直到最后一段代码。。。您编写了$request->setUser($user);是,确定$request代表一个请求实体。。。但我将如何“创造,调用”它?我是否必须在控制器中按我所说的$request=newrequest()进行写入?或者有点不同。你完全可以得到我的应用程序。在控制器中添加followee。。。基于我们的实体。我不知道你的应用程序是什么,也不知道你希望如何创建请求。如果您想通过表单创建请求,请参阅表单文档:如果您想创建新请求,只需使用new Requests()并将所需数据填入表单即可。。。你的代码几乎是一样的。。。我想。。。这是什么要求$请求->设置用户($user);它的$request=newrequest()?因为它会对所有空字段抛出错误,除了setUser one ofc。你在开玩笑吗?从您的代码:“TB\RequestsBundle\Entity\Requests”我不知道它来自哪里,这取决于您的应用程序!它是您要添加到用户中的实体!。。。你没有得到我。。。我是说一切都很清楚。。。直到最后一段代码。。。您编写了$request->setUser($user);是,确定$request代表一个请求实体。。。但我将如何“创造,调用”它?我是否必须在控制器中按我所说的$request=newrequest()进行写入?或者有点不同。你完全可以得到我的应用程序。在控制器中添加followee。。。基于我们的实体。我不知道你的应用程序是什么,也不知道你希望如何创建请求。如果要通过表单创建请求,请参阅表单文档:如果要创建新请求,只需使用new Requests()并填写所需数据即可