php 找不到目标实体“某个实体”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14707541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:50:12  来源:igfitidea点击:

The target-entity "some entity" cannot be found

phpdoctrinezend-framework2

提问by noobie-php

i am using ZF2 with doctrine i am getting this error.

我正在使用带有学说的 ZF2 我收到此错误。

The target-entity Entity\User cannot be found in 'Subject\Entity\Subject#user'.

在“Subject\Entity\Subject#user”中找不到目标实体 Entity\User。

Here is the snippet to my code.

这是我的代码片段。

<?php

namespace Subject\Entity;

use Doctrine\ORM\Mapping as ORM;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
/** 

* @ORM\Entity

* @ORM\Table(name="subject")

* @property string $subjectname

* @property int $user_id

* @property int $id

*/
 class Subject implements InputFilterAwareInterface {

  protected $inputFilter;
 /**

 * @ORM\Id

 * @ORM\Column(type="integer");

 * @ORM\GeneratedValue(strategy="AUTO")

 */
protected $id;
/**

 * @ORM\Column(type="string")

 */
protected $subjectname;

/**
 * @ORM\ManyToOne(targetEntity="Entity\User", inversedBy="subjects")
 * @var User|null
 */
private $user;

/** @return User|null */
public function getUser() {
    return $this->user;
}

/** @param User $user */
public function setUser(User $user) {
    if($user === null || $user instanceof User) {
        $this->user = $user;
    } else {
        throw new InvalidArgumentException('$user must be instance of Entity\User or null!');
    }
}}

and then my "User" entity

然后是我的“用户”实体

namespace Subject\Entity;

use Doctrine\ORM\Mapping as ORM;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;

/*
* @ORM\Entity

* @ORM\Table(name="users")

* @property string $username

* @property string $password

* @property int $id

*/
class User implements InputFilterAwareInterface {

 protected $_username;
 protected $_password;

 /**
 * @ORM\OneToMany(targetEntity="Entity\Subject", mappedBy="user")
 * @var Collection
 */
private $subjects;

/** @ORM\Id() @ORM\Column(type="integer") @ORM\GeneratedValue(strategy="AUTO") @var   int */
protected $_id;

public function __get($property) {

    return $this->$property;
}

public function __set($property, $value) {

    $this->$property = $value;
}

//Getters and setters

/** @return Collection */
public function getSubjects() {
    return $this->comments;
}

/** @param Comment $comment */
public function addSubject(Subject $comment) {
    $this->comments->add($comment);
    $comment->setUser($this);
}

}

}

回答by leftclickben

Your entity declaration is incorrect:

您的实体声明不正确:

 * @ORM\ManyToOne(targetEntity="Entity\User", inversedBy="subjects")

This should be either this:

这应该是这样的:

 * @ORM\ManyToOne(targetEntity="Subject\Entity\User", inversedBy="subjects")

Or, since the two classes share the same namespace, you can also use this:

或者,由于两个类共享相同的命名空间,您也可以使用:

 * @ORM\ManyToOne(targetEntity="User", inversedBy="subjects")

The targetEntityhas to be the fully qualified class name (FQCN), except if referring to a class in the same namespace, in which case the short name may be used (as per the last example above).

targetEntity必须是完全合格的类名(FQCN),除非指的是在相同的命名空间的一类,在这种情况下,可以使用短名称(按照上面的最后一个例子)。