Search code examples
phpdoctrine-ormdoctrineassociationsmodel-associations

Doctrine won't add persist entity relationship


I have two entities, View and Location

Each View can have a Location.

In my view I thus have:

class View
{
    //..... Other Stuff.....

    /**
     * @ManyToOne(targetEntity="Location", inversedBy="views")
     **/
    private $location;

    //...setters and getters....

    public function setLocation($location){
        $this->location = $location;
    }

}

and then for my Location

class Location
{
    //.....other stuff.....

    /**
     * @OneToMany(targetEntity="View", mappedBy="location")
     **/
    private $views;

    public function __construct() {
        $this->created = $this->updated = new \DateTime("now");
        $this->views = new \Doctrine\Common\Collections\ArrayCollection();
    }

    // .... Getters and Setters ....
}

But when I try and do this:

<?php
    $this->pageview = $this->em->getRepository('Entities\View')->find(1);
    $this->location = $this->em->getRepository('Entities\Location')->find(1);
    $this->pageview->setLocation($this->location);
    $this->em->persist($this->pageview);
    $this->em->flush();
?>

Or even when I create new entities:

<?php
    $pv = new Entities\Pageview;
    $lc = new Entities\Location;
    $this->em->persist($lc);
    $this->em->flush();
    $pv->setLocation($lc);
    $this->em->persist($pv);
    $this->em->flush();
?>

Doctrine never sets the location_id in the database (it is always NULL).

I've checked the SQL queries and they're not even being attempted at being set, all I'm getting is:

INSERT INTO View (field1, field2, created, updated) VALUES ('val1', 'val2', '2013-07-17T12:10:56+01:00', '2013-07-17T12:10:56+01:00')

No reference to locations whatsoever...The weird thing is I can update field1 and field2 fine...and all other relations are working throughout my application...I just can't get views and locations to work...

EDIT

I have the exact some code working now on another computer. I don't know why it wasn't working, but I just moved the files back and restarted my computer and now it is...cacheing problem I guess?


Solution

  • Restarted my computer and the problem got solved...I don't know why it was going wrong!

    Maybe something to do with caches or proxies...I dunno...