Harry Mustoe-Playfair
Harry Mustoe-Playfair

Reputation: 1419

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?

Upvotes: 2

Views: 742

Answers (4)

Rvanlaak
Rvanlaak

Reputation: 3085

This is related to the Doctrine ORM cache drivers:

doctrine:
    orm:
        entity_managers:
            default:
                metadata_cache_driver: apcu
                result_cache_driver: apcu
                query_cache_driver: apcu

We used APCu to even on DEV do caching, clearing APCu (by restarting Apache) did the trick.

Upvotes: 0

Harry Mustoe-Playfair
Harry Mustoe-Playfair

Reputation: 1419

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

Upvotes: 1

Guido
Guido

Reputation: 103

I think you need load the view in the location. So you must create a method in your Location entity like this:

public function getViews() {
    return $this->views;
}

and then to persist into database, do this:

$location = new Entity\Location();
$view = new Entity\View();
$location->getViews()->add($view);
$this->em->persist($location)
$view->setLocation($location);
$this->em->persist($view);
$this->em->flush();

Upvotes: 0

George Mickleburgh
George Mickleburgh

Reputation: 1217

You could try explicitly referencing the correct columns that Doctrine needs to do a join on.

/**
 * @ManyToOne(targetEntity="Location")
 * @JoinColumn(name="location_id", referencedColumnName="id")
 */
private $location;

Also, in this example:

$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();

You do not need to persist the entity if you are just updating the existing data.

Upvotes: 0

Related Questions