目标实体“一些实体”找不到 [英] The target-entity "some entity" cannot be found

查看:87
本文介绍了目标实体“一些实体”找不到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ZF2与原则我收到这个错误。

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


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

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

这是代码片段。

<?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!');
    }
}}

然后我的用户实体



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);
}

}

推荐答案

您的实体声明不正确:

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

这应该是这样的:

 * @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")

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

The targetEntity has 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).

这篇关于目标实体“一些实体”找不到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆