PersistenceObjects for php

Manual

Connection types

Many to one

But what if we do want a car to have many owners? For example joint ownership between a husband and wife. Then we would need to create many to one connections between owners and a car.

We can create this with a column in the owner table that references to the car id like in the first example with one to one connections. But the reference column is not unique.

This is not possible at version 0.8 because the development focus is put on connection tables, which is the prefered way (see below this).

owner
id varchar(32), primary key
car_id varchar(32), references subject.id
car
id varchar(32), primary key

And in code:

class OwnerPersistence extends PersistenceObject {
	
	public function OwnerPersistence() {
		
		$this->tableName = "owner";
		$this->className = "Owner";
		
		$this->addManyToOnePropertyDescription("car", "id", "owner", "car_id", "Car");
		
		parent::PersistenceObject(func_get_args());

	}
}
class CarPersistence extends PersistenceObject {
	
	public function CarPersistence() {
		
		$this->tableName = "car";
		$this->className = "Car";
		
		$this->addOneToOnePropertyDescription("owner", "car_id", "owner", "id", "Owner");
		
		parent::PersistenceObject(func_get_args());

	}
}

Again, this is easy but not very versatile if we have to deal with changes. Another way to do this is to create a connection table and set the combination of owner id and car id as unique.

owner
id varchar(32), primary key
owner_car
owner_id varchar(32)
car_id varchar(32)
unique: owner_id, car_id
car
id varchar(32), primary key

And in code:

class OwnerPersistence extends PersistenceObject {
	
	public function OwnerPersistence() {
		
		$this->tableName = "owner";
		$this->className = "Owner";
		
		$this->addManyToOnePropertyDescription("car", "owner_id", "owner_car", "car_id", "Car");
		
		parent::PersistenceObject(func_get_args());

	}
}
class CarPersistence extends PersistenceObject {
	
	public function CarPersistence() {
		
		$this->tableName = "car";
		$this->className = "Car";
		
		$this->addOneToOnePropertyDescription("owner", "car_id", "owner_car", "owner_id", "Owner");
		
		parent::PersistenceObject(func_get_args());

	}
}