PersistenceObjects for php

Manual

Connection types

One to one

One to one connection is the simplest one. Basically it describes a cohesion where One object contains one single instance of another object. Like for example a car has one owner. The simplest way to implement this in the database would be to have a table for the Owner which would have one column that references to the car-table's id-column. We make the reference unique to have each car have only one owner.

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), unique, 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->addOneToOnePropertyDescription("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());

	}
}

Another way to implement this is to create a connection table for the property and set the owner id as unique and car id as unique.

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

And in code:

class OwnerPersistence extends PersistenceObject {
	
	public function OwnerPersistence() {
		
		$this->tableName = "owner";
		$this->className = "Owner";
		
		$this->addOneToOnePropertyDescription("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());

	}
}

Codewise the difference between the two methods is not much. But in databaselevel while the first method is easier, the latter is more versatile when there is need to make changes.