Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The target-entity "some entity" cannot be found

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

The target-entity Entity\User cannot be found in 'Subject\Entity\Subject#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);
}

}

like image 582
noobie-php Avatar asked Feb 05 '13 12:02

noobie-php


2 Answers

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

like image 178
leftclickben Avatar answered Nov 19 '22 05:11

leftclickben


In our case, the file name was not the same as the class name: it was just a typo.

like image 21
mariusa Avatar answered Nov 19 '22 05:11

mariusa