Nikitas
Nikitas

Reputation: 1003

Doctrine 2 "Found entity of type on association but expected"

Working with ZF2 and Doctrine 2. Trying to insert data using entity manager.

I have this Entity:

class Link
{
    /**
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")
    * @ORM\Column(type="integer")
    */
    protected $link_id;

    /** @ORM\Column(type="string", length=255, nullable=false) */
    protected $title;

    /** @ORM\Column(type="string", length=255, nullable=false) */
    protected $short_description;

    /** @ORM\Column(columnDefinition="LONGTEXT NOT NULL") */
    protected $description;

    /** @ORM\Column(type="string", length=255, nullable=false) */
    protected $webpage_url;

    /** @ORM\Column(type="string", length=255, nullable=false) */
    protected $email;

    /** @ORM\Column(type="string", length=255, nullable=false) */
    protected $meta_keys;

    /** @ORM\Column(type="datetime", columnDefinition="DATETIME NOT NULL") */
    protected $date_created;

    /**
     * @ORM\ManyToOne(targetEntity="Schema\Entity\Category")
     **/
    private $category_id;

    public function __get($property) {
        if (property_exists($this, $property)) {
              return $this->$property;
        }
    }

    public function __set($property, $value) {
        if (property_exists($this, $property)) {
            $this->$property = $value;
        }
        return $this;
    }
 }

And this

class LinkType
{
    /**
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")
    * @ORM\Column(type="integer")
    */
    protected $link_type_id;

    /** @ORM\Column(type="string", length=255, nullable=false) */
    protected $name;

    public function __get($property) {
        if (property_exists($this, $property)) {
              return $this->$property;
        }
    }

    public function __set($property, $value) {
        if (property_exists($this, $property)) {
            $this->$property = $value;
        }
        return $this;
    }
}

When i try this:

$link = new Link();
$link->title = 'aa';
$link->category_id = array('1');
$link->link_type_id = array('1');
$link->description = 'adsfa';
$link->webpage_url = 'asdfad';
$link->short_description = 'aa';
$link->email = 'asdf';
$link->meta_keys = 'asdf';
$link->date_created ='2014-01-14 13:26:54';


$this->getObjectManager()->persist($link); // ?????
$this->getObjectManager()->flush();

Gives me Error: Found entity of type on association Schema\Entity\Link#category_id, but expecting Schema\Entity\Category

I tried also putting cascade={"persist"} in annontations but gives me error: Class '' does not exist

Why?

Upvotes: 2

Views: 5354

Answers (1)

Sam
Sam

Reputation: 16455

You have to set the category_id to a value of Schema\Entity\Category[] and not array()

Upvotes: 3

Related Questions