Php Can';t获取用户';在Symfony2中删除它的化身实体

Php Can';t获取用户';在Symfony2中删除它的化身实体,php,symfony,doctrine,entity,one-to-one,Php,Symfony,Doctrine,Entity,One To One,我有一个用户实体和一个具有一对一关系的化身实体: //MyBundle/Entity/User.php /** * @ORM\OneToOne(targetEntity="Participso\UserBundle\Entity\Avatar", cascade={"remove", "persist"}) */ protected $avatar; 如果用户已经有了头像,我想使用以下方法删除前一个头像: //MyBundle/Controller/UserController.php

我有一个用户实体和一个具有一对一关系的化身实体:

//MyBundle/Entity/User.php

/**
 * @ORM\OneToOne(targetEntity="Participso\UserBundle\Entity\Avatar", cascade={"remove", "persist"})
 */
protected $avatar;
如果用户已经有了头像,我想使用以下方法删除前一个头像:

//MyBundle/Controller/UserController.php

if ($avatarForm->isValid()) {
    $em = $this->getDoctrine()->getManager();

    if($currentUser->getAvatar()){
        $em->remove($currentUser->getAvatar());
    }

    $currentUser->setAvatar($avatar);
    $em->persist($currentUser); //"cascade=persist" from User
    $em->flush();
}
这是:

//MyBundle/Entity/Avatar.php

/**
 * @ORM\PostRemove()
 */
public function removeUpload()
{
    $file = __DIR__.'/../../../../web/uploads/avatar/' . $this->fileName;
    unlink($file);
}
但是当我做
var\u dump($currentUser->getAvatar())
即使数据库中存在文件名,我也会得到一个空文件名:

object(Proxies\__CG__\Participso\UserBundle\Entity\Avatar)[355]
    public '__initializer__' => 
object(Closure)[348]
    public '__cloner__' => 
object(Closure)[349]
    public '__isInitialized__' => boolean false
    private 'id' (Participso\UserBundle\Entity\Avatar) => int 20
    public 'file' => null
    private 'fileName' (Participso\UserBundle\Entity\Avatar) => null
当我执行
var\u dump($currentUser->getAvatar()->>getFileName)
时,我得到了文件名!!(如前所述,不为空)

有人有主意吗?

试试这个:

if($currentUser->getAvatar()){
       $currentUser->removeUpload();
}

谢谢你的回复

事实上,这是因为“懒散加载”原则:

如果我这样做:

var_dump($currentUser->getAvatar());exit;
输出:

object(Proxies\__CG__\Participso\UserBundle\Entity\Avatar)[355]
    private 'id' (Participso\UserBundle\Entity\Avatar) => int 20
    private 'fileName' (Participso\UserBundle\Entity\Avatar) => null
object(Proxies\__CG__\Participso\UserBundle\Entity\Avatar)[355]
    private 'id' (Participso\UserBundle\Entity\Avatar) => int 20
    private 'fileName' (Participso\UserBundle\Entity\Avatar) => string 'd4e5eadd3757498a22b14ad1f81869c2baf459d3.png'
但如果我这样做了

$whatever = $currentUser->getAvatar()->getFileName();
var_dump($currentUser->getAvatar());exit;
输出:

object(Proxies\__CG__\Participso\UserBundle\Entity\Avatar)[355]
    private 'id' (Participso\UserBundle\Entity\Avatar) => int 20
    private 'fileName' (Participso\UserBundle\Entity\Avatar) => null
object(Proxies\__CG__\Participso\UserBundle\Entity\Avatar)[355]
    private 'id' (Participso\UserBundle\Entity\Avatar) => int 20
    private 'fileName' (Participso\UserBundle\Entity\Avatar) => string 'd4e5eadd3757498a22b14ad1f81869c2baf459d3.png'
这很烦人。。。我将创建一个新帖子来解决这个问题

编辑:新帖子已发布,并已解决:

为了避免从条令中延迟加载,只需在父实体中添加
fetch=“EAGER”

/**
 * @OneToOne(targetEntity="User")
 * @JoinColumn(name="user_id", referencedColumnName="id", fetch="EAGER")
 */

我想,这是
$em->remove($currentUser->getAvatar())仅为实体工作。谢谢,事实上getAvatar应该返回虚拟实体,但我不明白为什么它不'…在
$em->remove()
之后的
if
块中尝试刷新。我不能,这是用户的foreing键:“错误:无法删除或更新父行:外键约束失败”感谢您的回复,但是即使文件被删除,数据库行仍然存在。。。是$currentUser->getAvatar()->removeUpload();:)