user3396420
user3396420

Reputation: 840

Doctrine: the target-entity cannot be found in (two bundles)

I'm trying to do a ManyToOne relation in Symfony2 with Doctrine. My entities are:

namespace My\ApiBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
 * @ORM\Entity
 * @ORM\Table(name="company")
 */
class Company
{
    /**
     * @var integer
     *
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

public function __construct() {
    $this->users = new ArrayCollection();
}

/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

And the other one:

namespace My\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Entity\User as BaseUser;

/**
 * User
 *
 * @ORM\Table(name="user")
 * @ORM\Entity
 */
class User extends BaseUser
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @var
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

...

/**
     * @ORM\ManyToOne(targetEntity="My\ApiBundle\Entity\Company")
     * @ORM\JoinColumn(name="idLastCompany", referencedColumnName="id")
     */
    protected $idLastCompany;

Obviusly with other attributes and their sets and gets methods, but my only relation is between Company with idLastCompany. When I clear cache for example, I get this error:

MappingException: The target-entity My\ApiBundle\Entity\Copmany cannot be found in 'My\UserBundle\Entity\User#idLastCompany'.

Any Idea? Thanks

Upvotes: 3

Views: 1074

Answers (1)

pcm
pcm

Reputation: 856

The error message tells you everything you need :)

MappingException: The target-entity My\ApiBundle\Entity\ Copmany cannot be found in 'My\UserBundle\Entity\User#idLastCompany'.
You spelled CoPMany instead of Company either in the entity file name, in the entity class name or in the relation field $idLastCompany docblock.
Even though the code you posted here is correct, your actual code contains a typo.

I would search the entire project for "Copmany" and fix the typo. Then it will work.

Upvotes: 1

Related Questions