我正在使用 ZF2 与学说我收到此错误。
在“Subject\Entity\Subject#user”中找不到目标实体 Entity\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);
}
}