<commit_before><?php

class MvcModel {

	public $table = null;
	public $primary_key = 'id';
	public $belongs_to = null;
	public $has_many = null;
	public $has_and_belongs_to_many = null;
	public $associations = null;
	public $admin_pages = null;
	public $validation_error = null;
	public $validation_error_html = null;
	public $hide_menu = false;
	private $data_validator = null;
	private $db_adapter = null;
	
	function __construct() {
		
		global $wpdb;
		
		$this->name = preg_replace('/Model$/', '', get_class($this));
		
		$table = empty($this->table) ? $wpdb->prefix.MvcInflector::tableize($this->name) : $this->process_table_name($this->table);
		
		$defaults = array(
			'model_name' => $this->name,
			'table' => $table,
			'table_reference' => empty($this->database) ? '`'.$table.'`' : '`'.$this->database.'`.`'.$table.'`',
			'selects' => empty($this->selects) ? array('`'.$this->name.'`.*') : $this->default_selects,
			'order' => empty($this->order) ? null : $this->order,
			'joins' => empty($this->joins) ? null : $this->joins,
			'conditions' => empty($this->conditions) ? null : $this->conditions,
			'limit' => empty($this->limit) ? null : $this->limit,
			'includes' => empty($this->includes) ? null : $this->includes,
			'per_page' => empty($this->per_page) ? 10 : $this->per_page,
			'validate' => empty($this->validate) ? null : $this->validate
		);
		
		foreach ($defaults as $key => $value) {
			$this->{$key} = $value;
		}
		
		$this->db_adapter = new MvcDatabaseAdapter();
		$this->db_adapter->set_defaults($defaults);
		
		$this->data_validator = new MvcDataValidator();
		
		$this->init_admin_pages();
		$this->init_admin_columns();
		$this->init_associations();
		$this->init_schema();
	
	}
	
	public function new_object($data) {
		$object = false;
		foreach ($data as $field => $value) {
			$object->{$field} = $value;
		}
		$object = $this->process_objects($object);
		return $object;
	}
	
	public function create($data) {
		if (empty($data[$this->name])) {
			return false;
		}
		$model_data = $data[$this->name];
		if (method_exists($this, 'before_save')) {
			if (!$this->before_save($model_data)) {
				return false;
			}
		}
		$id = $this->insert($model_data);
		$this->update_associations($id, $model_data);
		if (method_exists($this, 'after_save')) {
			$object = $this->find_by_id($id);
			$this->after_save($object);
		}
		return $id;
	}
	
	public function save($data) {
		if (empty($data[$this->name])) {
			return false;
		}
		if (!empty($data[$this->name]['id'])) {
			$model_data = $data[$this->name];
			$id = $model_data['id'];
			unset($model_data['id']);
			$valid = $this->validate_data($model_data);
			if ($valid !== true) {
				$this->validation_error = $valid;
				$this->validation_error_html = $this->validation_error->get_html();
				$this->invalid_data = $model_data;
				return false;
			}
			if (method_exists($this, 'before_save')) {
				if (!$this->before_save($model_data)) {
					return false;
				}
			}
			$this->update($id, $model_data);
			$this->update_associations($id, $model_data);
		} else {
			$id = $this->create($data);
		}
		if (method_exists($this, 'after_save')) {
			$object = $this->find_by_id($id);
			$this->after_save($object);
		}
		return true;
	}
	
	public function insert($data) {
		$insert_id = $this->db_adapter->insert($data);
		$this->insert_id = $insert_id;
		return $insert_id;
	}
	
	public function update($id, $data) {
		$options = array(
			'conditions' => array($this->name.'.'.$this->primary_key => $id)
		);
		$this->db_adapter->update_all($data, $options);
	}
	
	public function update_all($data, $options=array()) {
		$this->db_adapter->update_all($data, $options);
	}
	
	public function delete($id) {
		$options = array(
			'conditions' => array($this->primary_key => $id)
		);
		$this->db_adapter->delete_all($options);
	}
	
	public function delete_all($options=array()) {
		$this->db_adapter->delete_all($options);
	}
	
	public function find($options=array()) {
		$options = $this->process_find_options($options);
		$objects = $this->db_adapter->get_results($options);
		$objects = $this->process_objects($objects, $options);
		return $objects;
	}
	
	public function find_by_id($id, $options=array()) {
		$options = $this->process_find_options($options);
		$options['conditions'] = array($this->name.'.'.$this->primary_key => $id);
		$object = $this->db_adapter->get_results($options);<commit_msg>Adding a find_one() method to MvcModel<commit_after><?php

class MvcModel {

	public $table = null;
	public $primary_key = 'id';
	public $belongs_to = null;
	public $has_many = null;
	public $has_and_belongs_to_many = null;
	public $associations = null;
	public $admin_pages = null;
	public $validation_error = null;
	public $validation_error_html = null;
	public $hide_menu = false;
	private $data_validator = null;
	private $db_adapter = null;
	
	function __construct() {
		
		global $wpdb;
		
		$this->name = preg_replace('/Model$/', '', get_class($this));
		
		$table = empty($this->table) ? $wpdb->prefix.MvcInflector::tableize($this->name) : $this->process_table_name($this->table);
		
		$defaults = array(
			'model_name' => $this->name,
			'table' => $table,
			'table_reference' => empty($this->database) ? '`'.$table.'`' : '`'.$this->database.'`.`'.$table.'`',
			'selects' => empty($this->selects) ? array('`'.$this->name.'`.*') : $this->default_selects,
			'order' => empty($this->order) ? null : $this->order,
			'joins' => empty($this->joins) ? null : $this->joins,
			'conditions' => empty($this->conditions) ? null : $this->conditions,
			'limit' => empty($this->limit) ? null : $this->limit,
			'includes' => empty($this->includes) ? null : $this->includes,
			'per_page' => empty($this->per_page) ? 10 : $this->per_page,
			'validate' => empty($this->validate) ? null : $this->validate
		);
		
		foreach ($defaults as $key => $value) {
			$this->{$key} = $value;
		}
		
		$this->db_adapter = new MvcDatabaseAdapter();
		$this->db_adapter->set_defaults($defaults);
		
		$this->data_validator = new MvcDataValidator();
		
		$this->init_admin_pages();
		$this->init_admin_columns();
		$this->init_associations();
		$this->init_schema();
	
	}
	
	public function new_object($data) {
		$object = false;
		foreach ($data as $field => $value) {
			$object->{$field} = $value;
		}
		$object = $this->process_objects($object);
		return $object;
	}
	
	public function create($data) {
		if (empty($data[$this->name])) {
			return false;
		}
		$model_data = $data[$this->name];
		if (method_exists($this, 'before_save')) {
			if (!$this->before_save($model_data)) {
				return false;
			}
		}
		$id = $this->insert($model_data);
		$this->update_associations($id, $model_data);
		if (method_exists($this, 'after_save')) {
			$object = $this->find_by_id($id);
			$this->after_save($object);
		}
		return $id;
	}
	
	public function save($data) {
		if (empty($data[$this->name])) {
			return false;
		}
		if (!empty($data[$this->name]['id'])) {
			$model_data = $data[$this->name];
			$id = $model_data['id'];
			unset($model_data['id']);
			$valid = $this->validate_data($model_data);
			if ($valid !== true) {
				$this->validation_error = $valid;
				$this->validation_error_html = $this->validation_error->get_html();
				$this->invalid_data = $model_data;
				return false;
			}
			if (method_exists($this, 'before_save')) {
				if (!$this->before_save($model_data)) {
					return false;
				}
			}
			$this->update($id, $model_data);
			$this->update_associations($id, $model_data);
		} else {
			$id = $this->create($data);
		}
		if (method_exists($this, 'after_save')) {
			$object = $this->find_by_id($id);
			$this->after_save($object);
		}
		return true;
	}
	
	public function insert($data) {
		$insert_id = $this->db_adapter->insert($data);
		$this->insert_id = $insert_id;
		return $insert_id;
	}
	
	public function update($id, $data) {
		$options = array(
			'conditions' => array($this->name.'.'.$this->primary_key => $id)
		);
		$this->db_adapter->update_all($data, $options);
	}
	
	public function update_all($data, $options=array()) {
		$this->db_adapter->update_all($data, $options);
	}
	
	public function delete($id) {
		$options = array(
			'conditions' => array($this->primary_key => $id)
		);
		$this->db_adapter->delete_all($options);
	}
	
	public function delete_all($options=array()) {
		$this->db_adapter->delete_all($options);
	}
	
	public function find($options=array()) {
		$options = $this->process_find_options($options);
		$objects = $this->db_adapter->get_results($options);
		$objects = $this->process_objects($objects, $options);
		return $objects;
	}
	
	public function find_one($options=array()) {
		$options['limit'] = 1;
		$options = $this->process_find_options($options);
		$object = $this->db_adapter->get_results($options);
		$object = isset($object[0]) ? $object[0] : null;
		$object = $this->process_objects($object, $options);
		return $object;
	}
	
	public function find_by_id($id, $options=array()) {
		$options = $this->process_find_options($options);
		$options['conditions'] = array($this->name.'.'.$this->primary_key => $id);
		$object = $this->db_adapter->get_results($options);
{'fraction_non_alphanumeric': 0.2033285886346217, 'fraction_numerical': 0.0007664513303405234, 'mean_word_length': 4.357184750733138, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 207, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3187454', 'n_tokens_mistral': 3584, 'n_tokens_neox': 3196, 'n_words': 552}