我的项目中有两个实体:用户和头像。
用户拥有具有一对一关系的 Avatar。
Avatar 是一个具有文件对象和文件名的实体。它使用 @ORM\HasLifecycleCallbacks 来保存文件或将其删除,如 Symfony2 documentation 中所述。 .
在我的 Controller 中,我想从当前用户中删除 Avatar 实体(我使用 $user = $this->get('security.context')->getToken()->getUser()
),但我无法使用 $user->getAvatar()
获取头像:
var_dump($user->getAvatar());
object(AppBundle\Entity\Avatar)
private 'id' => int 20
public 'file' => null
private 'fileName' => null
但是如果我尝试访问头像的文件名,它会被返回:
$filename = $user->getAvatar()->getFileName();
var_dump($user->getAvatar());
object(AppBundle\Entity\Avatar)
private 'id' => int 20
public 'file' => null
private 'fileName' => string 'myfile.png'
如何获取与我的用户关联的头像?
最佳答案
如 Doctrine docs 中所述,您只需指定要执行的获取行为即可。
/**
* @OneToOne(targetEntity="User", fetch="EAGER")
* @JoinColumn(name="user_id", referencedColumnName="id")
*/
请参阅 YAML 或其他配置示例的文档。
关于php - 避免延迟加载 Doctrine Symfony2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31763867/