<commit_before><?php
/**
 * Fuel
 *
 * Fuel is a fast, lightweight, community driven PHP5 framework.
 *
 * @package		Fuel
 * @version		1.0
 * @author		Fuel Development Team
 * @license		MIT License
 * @copyright	2010 - 2011 Fuel Development Team
 * @link		http://fuelphp.com
 */

namespace ActiveRecord;


use \DB;
use \Database;
use \Inflector;

class Model {

	/**
	 * Queries the table for the given primary key value ($id).  $id can also
	 * contain 2 special values:
	 *
	 * <ul>
	 * <li>'all' - Will return all of the records in the table</li>
	 * <li>'first' - Will return the first record.  This will set automatically
	 * set the 'limit' option to 1.</li>
	 * </ul>
	 *
	 * The following options are available to use in the $options parameter:
	 *
	 * <ul>
	 * <li>include - an array of associations to include in the query.  Example:
	 * <code>array('group', 'posts')</code></li>
	 * <li>select - an array of columns to select (defaults to '*').  Example:
	 * <code>array('first_name', 'last_name')</code></li>
	 * <li>limit - the maximum number of records to fetch</li>
	 * <li>offset - the offset to start from</li>
	 * <li>order - an array containing the ORDER BY clause.  Example:
	 * <code>array('last_name', 'ASC')</code></li>
	 * <li>where - an array of arrays containing the where clauses.  Example:
	 * <code>array(array('enabled', '=', 1), array('blacklisted', '=', 0))</code>
	 * <li>or_where - identical to 'where' except these are OR WHERE statements.</li>
	 *
	 * Usage:
	 *
	 * <code>$user = User::find(2, array('include' => array('group')));</code>
	 *
	 * @param	int|string	$id			the primary key value
	 * @param	srray		$options	the find options
	 * @return	object		the result
	 */
	public static function find($id, $options = array())
	{
		$instance = new static;
		$results = $instance->run_find($id, $options);
		unset($instance);

		return $results;
	}

	/**
	 * This allows for queries called by a static method on the model class.  It
	 * supports both 'and' and 'or' queries, or a mixture of both.  You can also
	 * set the last parameter to an array of option. see {@link find} for
	 * available options.
	 *
	 * Usage:
	 *
	 * <code>
	 * Model_User::find_by_group_id(2);
	 * Model_User::find_by_username_and_password('<PASSWORD>', 'password');
	 * Model_User::find_by_username_or_group_id('demo', 2);
	 * Model_User::find_by_email_and_password_or_group_id('<EMAIL>', 'password', 2);
	 * </code>
	 *
	 * @param	string	$name		the method name called
	 * @param	array	$arguments	the method arguments
	 * @return	object|array	an instance or array of instances found
	 */
	public static function __callStatic($name, $arguments)
	{
		if ($name == '_init')
		{
			return;
		}
		if (strncmp($name, 'find_by_', 8) !== 0 && $name != '_init')
		{
			throw new \Exception('Invalid method call.  Method '.$name.' does not exist.', 0);
		}

		$name = substr($name, 8);

		$and_parts = explode('_and_', $name);

		$temp_model = new static;
		$table_name = $temp_model->table_name;
		unset($temp_model);

		$where = array();
		$or_where = array();

		foreach ($and_parts as $and_part)
		{
			$or_parts = explode('_or_', $and_part);
			if (count($or_parts) == 1)
			{
				$where[] = array(
					$table_name.'.'.$or_parts[0], '=', array_shift($arguments)
				);
			}
			else
			{
				foreach($or_parts as $or_part)
				{
					$or_where[] = array(
						$table_name.'.'.$or_part, '=', array_shift($arguments)
					);
				}
			}
		}

		$options = count($arguments) > 0 ? array_pop($arguments) : array();

		if ( ! array_key_exists('where', $options))
		{
			$options['where'] = $where;
		}
		else
		{
			$options['where'] = $options['where'] + $where;
		}

		if ( ! array_key_exists('or_where', $options))
		{
			$options['or_where'] = $or_where;
		}
		else
		{
			$options['or_where'] = $options['or_where'] + $or_where;
		}

		return static::find('all', $options);
	}

	/**
	 * This function is used to re-format the given $row into the $col_lookup
	 * format.
	 *
	 * @param	array	$row		the record
	 * @param	array	$col_lookup	the lookup schema
	 * @return	array	the new $row<commit_msg>Fixes #94: When only 1 result is returned from the static find_by_ it will now just return that single result instead of an error.<commit_after><?php
/**
 * Fuel
 *
 * Fuel is a fast, lightweight, community driven PHP5 framework.
 *
 * @package		Fuel
 * @version		1.0
 * @author		Fuel Development Team
 * @license		MIT License
 * @copyright	2010 - 2011 Fuel Development Team
 * @link		http://fuelphp.com
 */

namespace ActiveRecord;


use \DB;
use \Database;
use \Inflector;

class Model {

	/**
	 * Queries the table for the given primary key value ($id).  $id can also
	 * contain 2 special values:
	 *
	 * <ul>
	 * <li>'all' - Will return all of the records in the table</li>
	 * <li>'first' - Will return the first record.  This will set automatically
	 * set the 'limit' option to 1.</li>
	 * </ul>
	 *
	 * The following options are available to use in the $options parameter:
	 *
	 * <ul>
	 * <li>include - an array of associations to include in the query.  Example:
	 * <code>array('group', 'posts')</code></li>
	 * <li>select - an array of columns to select (defaults to '*').  Example:
	 * <code>array('first_name', 'last_name')</code></li>
	 * <li>limit - the maximum number of records to fetch</li>
	 * <li>offset - the offset to start from</li>
	 * <li>order - an array containing the ORDER BY clause.  Example:
	 * <code>array('last_name', 'ASC')</code></li>
	 * <li>where - an array of arrays containing the where clauses.  Example:
	 * <code>array(array('enabled', '=', 1), array('blacklisted', '=', 0))</code>
	 * <li>or_where - identical to 'where' except these are OR WHERE statements.</li>
	 *
	 * Usage:
	 *
	 * <code>$user = User::find(2, array('include' => array('group')));</code>
	 *
	 * @param	int|string	$id			the primary key value
	 * @param	srray		$options	the find options
	 * @return	object		the result
	 */
	public static function find($id, $options = array())
	{
		$instance = new static;
		$results = $instance->run_find($id, $options);
		unset($instance);

		return $results;
	}

	/**
	 * This allows for queries called by a static method on the model class.  It
	 * supports both 'and' and 'or' queries, or a mixture of both.  You can also
	 * set the last parameter to an array of option. see {@link find} for
	 * available options.
	 *
	 * Usage:
	 *
	 * <code>
	 * Model_User::find_by_group_id(2);
	 * Model_User::find_by_username_and_password('demo', 'password');
	 * Model_User::find_by_username_or_group_id('demo', 2);
	 * Model_User::find_by_email_and_password_or_group_id('<EMAIL>', 'password', 2);
	 * </code>
	 *
	 * @param	string	$name		the method name called
	 * @param	array	$arguments	the method arguments
	 * @return	object|array	an instance or array of instances found
	 */
	public static function __callStatic($name, $arguments)
	{
		if ($name == '_init')
		{
			return;
		}
		if (strncmp($name, 'find_by_', 8) !== 0 && $name != '_init')
		{
			throw new \Exception('Invalid method call.  Method '.$name.' does not exist.', 0);
		}

		$name = substr($name, 8);

		$and_parts = explode('_and_', $name);

		$temp_model = new static;
		$table_name = $temp_model->table_name;
		unset($temp_model);

		$where = array();
		$or_where = array();

		foreach ($and_parts as $and_part)
		{
			$or_parts = explode('_or_', $and_part);
			if (count($or_parts) == 1)
			{
				$where[] = array(
					$table_name.'.'.$or_parts[0], '=', array_shift($arguments)
				);
			}
			else
			{
				foreach($or_parts as $or_part)
				{
					$or_where[] = array(
						$table_name.'.'.$or_part, '=', array_shift($arguments)
					);
				}
			}
		}

		$options = count($arguments) > 0 ? array_pop($arguments) : array();

		if ( ! array_key_exists('where', $options))
		{
			$options['where'] = $where;
		}
		else
		{
			$options['where'] = $options['where'] + $where;
		}

		if ( ! array_key_exists('or_where', $options))
		{
			$options['or_where'] = $or_where;
		}
		else
		{
			$options['or_where'] = $options['or_where'] + $or_where;
		}

		$results = static::find('all', $options);
		
		return count($results) === 1 ? $results[0] : (count($results) === 0 ? null : $results);
	}

	/**
	 * This function is used to re-format the given $row into the $col_lookup
	 * format.
	 *
	 * @param	array	$row		the record
	 * @param	array	$col_lookup	the lookup schema
	 * @return	array	the new $row
{'fraction_non_alphanumeric': 0.15894511760513186, 'fraction_numerical': 0.00688999762413875, 'mean_word_length': 3.4055468341182626, 'pattern_counts': {'":': 0, '<': 72, '<?xml version=': 0, '>': 78, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5991217', 'n_tokens_mistral': 3208, 'n_tokens_neox': 2820, 'n_words': 838}