gpt4 book ai didi

php - 找不到目标实体 "some entity"

转载 作者:IT王子 更新时间:2023-10-29 00:11:52 26 4
gpt4 key购买 nike

我在使用 ZF2 时遇到了这个错误。

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

或者,因为这两个类共享同一个命名空间,你也可以使用这个:

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

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

关于php - 找不到目标实体 "some entity",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14707541/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com