Reputation: 2062
I have two different kinds of objects: Ride and Location.
A Ride has an origin and a destination which are Location objects.
Location does not point back to the Ride.
This means I have a many-to-one uni-directional relationship in doctrine.
How can I make doctrine ensure that I don't have any duplicate Location objects in my database?
For Example: If I create a Ride from Minneapolis, MN to Mankato, MN and persist them I now have stored one Ride and two Location objects in my database.
Now that those are persisted, I go to create another Ride from Mankato, MN to New Ulm, MN and persist them.
Doctrine has duplicated the Location Mankato, MN.
Does doctrine have this functionality, or is it my responsibility to check if I am creating duplicate objects?
Upvotes: 1
Views: 404
Reputation: 1740
Can you put a unique constraint on Location Name (Maybe state name + city name)?
It's your responsibility to see if the location already exists. And if so, fetch it and supply it to your Ride object.
class Ride {
private $from;
private $to;
public function setFrom(Location $from) {$this->from = $from;}
public function setTo(Location $to) {$this->to = $to;}
}
Upvotes: 2