<?php

/*
 * This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/.
 */

final class ObjectNameActions extends Actions{
	
	public static function insertObjectName(ObjectName $ObjectName){
		return parent::MySQLCreateAction(/* insert */);
	}
	
	public static function selectByObjectNameId($id){
		// Return one object by primary key selection
		return new ObjectName(parent::MySQLReadReturnSingleResultAsArrayAction(/* select */));
	}
	
	public static function selectList(PagingConfig $PagingConfig=null){
		// Return an object collection
		$ObjectNameCollection = new ObjectNameCollection(parent::MySQLReadReturnArrayOfObjectsAction(/* list */,
			$PagingConfig
		));
		
		if($PagingConfig instanceof PagingConfig){
			$ObjectNameCollection->setPaginationData($PagingConfig->getStart(), $PagingConfig->getLimit(), self::MySQLTotalRows());
		}
		
		return $ObjectNameCollection;
	}
	
	// Placeholder function for creating lists and linking objects.
	// This method makes object chaining and fast querying possible by using "IN" in mysql to pull down relative data
	// and bind objects to collections
	public static function selectListForDataCollection(DataCollection $DataCollection){
		$primary_keys = array();
		foreach($DataCollection->toArray() as $DataObject){
			$DataObject->set('ObjectName', new ObjectName());
			if($DataObject->getInteger('primary_key') > 0){
				$primary_keys[] = $DataObject->getInteger('primary_key');
			}
		}
		
		$primary_keys = array_unique($primary_keys);
		
		$ObjectNameCollection = new ObjectNameCollection(parent::MySQLReadReturnArrayOfObjectsAction(/* list */));
		
		foreach($ObjectNameCollection->toArray() as $ObjectName){
			$array = $DataCollection->getObjectByFieldValue('primary_key',$ObjectName->getInteger('primary_key'));
			foreach($array as $DataObject){
				$DataObject->set('ObjectName',$ObjectName);
			}
		}
		
		return $DataCollection;
	}
	
	public static function updateObjectName(ObjectName $ObjectName){
		return parent::MySQLUpdateAction(/* update */);
	}
	
	public static function deleteObjectNameById($id){
		return parent::MySQLUpdateAction(/* delete */);
	}

}