Location: PHPKode > projects > Porte > porte-0.2.2/src/plugins/associations/OneToOneBelongsTo.php
<?php

/**
 * Copyright (c) 2008, SARL Adaltas. All rights reserved.
 * Code licensed under the BSD License:
 * http://porte.adaltas.com/en/developer/license.html
 */

/**
 * PorteOneToOneHasOne
 * 
 * @package    Porte
 * @subpackage plugin
 * @author     David Worms info(at)adaltas.com
 * @copyright  2008 Adaltas
 */
class PorteOneToOneBelongsTo{
	
	/**
	 * Return the associated record or null if not defined.
	 * 
	 * @return mixed PorteRecord if association is defined or null
	 * @param $property string Record property
	 * @param $record PorteRecord Record to be associated
	 */
	public static function getRecord($property,PorteRecord $record){
		return PorteOneToManyBelongsTo::getRecord($property,$record);
	}
	
	/**
	 * Associated record may be an object (instance of PorteRecord), an int (or a 
	 * string casted to an int), an array (representing the record, like the return value
	 * of the "record->toArray" method) or null (if property not tagged as not_null).
	 * 
	 * @return PorteRecord Current record
	 * @param $property string Record property
	 * @param $record PorteRecord Record recieving the association
	 * @param $assocRecord mixed[optional] Associated record as a record, primary key, array or null; default to null of not provided
	 */
	public static function setRecord($property,PorteRecord $record,$assocRecord=null){
		$config = $record->porte->models->{$record->type}['properties'][$property];
		if(!is_null($assocRecord))
			$assocRecord = PorteAssociations::makeRecord($record,$config,'belongs_to',$assocRecord);
		// Free the old association if one existed
		if(array_key_exists($property,$record->associations)&&!is_null($record->associations[$property])){
			$oldAssocRecord = $record->associations[$property];
			$oldAssocRecord->associations[$config['belongs_to']['property']] = null;
		}
		$record->associations[$property] = $assocRecord;
		if(!is_null($assocRecord))
			$assocRecord->associations[$config['belongs_to']['property']] = $record;
		return $record;
	}
	
	/**
	 * Break the association if one existed.
	 * 
	 * @return PorteRecord Current record
	 * @param $property string Record property
	 * @param $record PorteRecord Record recieving the association
	 */
	public static function deleteRecord($property,PorteRecord $record){
		return self::setRecord($property,$record);
	}
	
}

?>
Return current item: Porte