<?php

class ModelFind
{
	public static function get($modelName, $condition = null, $order = null)
	{
		$model = new $modelName;
		if(empty($order)) {
			$order = $model->representingColumn() . " ASC";
		}
		$return = $model->find(array('condition' => $condition, 'order' => $order));
		if(!$return) {
			throw new Exception("Busca de {$modelName} não retornou nenhum registro. SQL: $condition", 1);
		}
		return $return;	
	}

	public static function getFirstInserted($modelName, $condition = null)
	{
		$model = new $modelName;
		return self::get($modelName, $condition, "{$model->tableSchema->primaryKey} ASC");
	}

	public static function getLastInserted($modelName, $condition = null)
	{
		$model = new $modelName;
		return self::get($modelName, $condition, "{$model->tableSchema->primaryKey} DESC");
	}

	public static function getRand($modelName, $condition = null)
	{
		return self::get($modelName, $condition, 'RAND()');
	}
}