Doctrine:MappingException:找不到目标实体CLASS

时间:2017-07-06 22:56:09

标签: php doctrine-orm doctrine

我不明白......

  

致命错误:未捕获的Doctrine \ ORM \ Mapping \ MappingException:The   目标实体商店无法在'用户#shop'中找到。在

我只有一个User课程。

/**
 * @Entity
 **/
class User
{

    /** @Id
     * @Column(type="integer")
     * @GeneratedValue
     **/
    protected $id;

    /**
     * @OneToMany(targetEntity="Shop", mappedBy="user")
     */
    protected $shops;

    public function __construct()
    {
        $this->shops = new \Doctrine\Common\Collections\ArrayCollection();
    }
}

谁拥有多个Shop s。

/**
 * @Entity
 */
class Shop
{

    /** @Id
     * @Column(type="integer")
     * @GeneratedValue
     **/
    protected $id;

    /**
     * @ManyToOne(targetEntity="User", inversedBy="shops")
     */
    protected $user;
}

它们位于同一目录中,因此不是namespace问题吗?

Getters&已经生成的Setters很好,但仍然没有改变。

TIPS :如果将我的所有课程都放在同一个php文件中,那就可以了!

1 个答案:

答案 0 :(得分:1)

Provided these are the full files, you are not writing any namespace declarations on the classes. Therefore the Entity classes are put in the \ root namespace.

So to resolve this you will either need to use the FQN (Full Qualified Name) or import the classes.

Using FQN (my recommendation)

/**
 * @OneToMany(targetEntity="\Shop", mappedBy="user")
 */

(add the same in the other class as well)

Using import

Add this on the top of the User class

use \Shop;

(the inverse in the other class of course)

Note: The PHP Interpreter doesn't check for usages in Annotations, so if you don't use the class elsewhere in the file, PHP might show a warning about an unused import if that is enabled.