Symfony2服务原则manytone返回null
我知道有很多像这样的问题,但我向你们保证,我已经花了6个小时寻找互联网,我如何才能解决这个问题,没有成功 我使用Symfony2控制器作为服务,因此我必须将所需的一切发送到控制器的Symfony2服务原则manytone返回null,symfony,doctrine-orm,Symfony,Doctrine Orm,我知道有很多像这样的问题,但我向你们保证,我已经花了6个小时寻找互联网,我如何才能解决这个问题,没有成功 我使用Symfony2控制器作为服务,因此我必须将所需的一切发送到控制器的\u construct方法 我正在尝试使用两个实体,其中一个是“用户”,另一个是“部门”(我附上下面的文件内容),每个实体都有存储库 在用户实体内部,我使用的原则是@ORM\manytone和@ORM\JoinColumn,在部门实体内部,我使用的原则是@ORM\OneToMany 所以,当我尝试这样做的时候 $de
\u construct
方法
我正在尝试使用两个实体,其中一个是“用户”,另一个是“部门”(我附上下面的文件内容),每个实体都有存储库
在用户实体内部,我使用的原则是@ORM\manytone
和@ORM\JoinColumn
,在部门实体内部,我使用的原则是@ORM\OneToMany
所以,当我尝试这样做的时候
$department = $this->departmentRepository->find(1);
$users = $department->getUsers();
…我得到了一个ArrayCollection()
,但当我这样做时
$user = $this->userRepository->find(1);
$department = $user->getDepartment();
。。。此方法返回“null”而不是Department()
我已经检查了MySQL表,我向您保证,“users”表中存储的记录具有部门ID。所以,我不明白为什么会这样
$user = $this->userRepository->find(1);
$department = $user->getDepartment();
这是关于@ORM\ManyToOne
和@ORM\join列的用户实体配置:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table(name="`users`", options={"collate"="utf8_unicode_ci"})
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
*/
class User
{
// Here there are other properties
/**
* @ORM\ManyToOne(targetEntity="Department", inversedBy="users")
* @ORM\JoinColumn(name="`department_id`", referencedColumnName="`id`", nullable=true)
*/
private $department;
// Here there are Getters and Setters
}
可能会清除缓存。验证getDepartment()没有拼写错误。我想背勾可能有什么作用,尽管它们应该没问题。昨天我清理了Symfony2缓存好几次,对翻译做了一些测试。这个问题仍然不起作用(很抱歉,在这里。当我遇到这些谜团时,我通常会关闭一个新项目,然后尝试用最少的额外代码重现问题。我所能想到的是,您有某种条令过滤器(可能被另一个包使用)这在某种程度上造成了干扰。或者你正在对一个文件进行更改,但实际使用的是另一个文件。谢谢你的建议。事实上,我不明白为什么会发生这种情况,因为在我以前的项目中,所有工作都很好,而我正在应用“相同的”配置OneToMany和ManyToOne条令注释。如果我找不到原因,也许我会尝试创建一个新项目,我会复制源代码,逐段复制,以找到错误。如果有人知道什么会导致此错误,请,我可以在“重新启动”之前节省一些时间正如Cerad所说,这个项目。谢谢你,伙计。有人发现了什么吗?我也面临着同样的问题。
<?php
namespace AppBundle\Controller;
use AppBundle\Entity\User;
use AppBundle\Repository\UserRepository;
// There are more "use", I have cleaned this source code
/**
* @Route(service="app.user.controller")
*/
class UserController
{
// There are more properties
/**
* @var UserRepository
*/
private $userRepository;
// There are a constructor with this:
public function __constructor(UserRepository $userRepository) { $this->userRepository = $userRepository; }
/**
* There is a method to list all users (it works) but does not load the departments
*/
public function listAction()
{
$users = $this->userRepository->findAll();
exit(var_dump($users[0]->getDepartment()));
return ['users' => $users];
}
}