<commit_before><?php

class MvcModel {

	public $name = null;
	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 $properties = null;
	public $validation_error = null;
	public $validation_error_html = null;
	public $schema = null;
	public $wp_post = null;
	private $data_validator = null;
	private $db_adapter = null;
	private $wp_post_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,
			'group' => empty($this->group) ? null : $this->group,
			'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_schema();
		
		if ($this->wp_post) {
			$this->wp_post_adapter = new MvcPostAdapter();
			$this->wp_post_adapter->verify_settings($this);
			if (empty($this->belongs_to)) {
				$this->belongs_to = array();
			}
			$association = array(
				'Post' => array(
					'class' => 'MvcPost'
				)
			);
			$this->belongs_to = array_merge($association, $this->belongs_to);
		}
		
		$this->init_associations();
		$this->init_properties();
	
	}
	
	public function new_object($data) {
		$object = new MvcModelObject($this);
		foreach ($data as $field => $value) {
			$object->$field = $value;
		}
		$object = $this->process_objects($object);
		return $object;
	}
	
	public function create($data) {
		$data = $this->object_to_array($data);
		if (empty($data[$this->name])) {
			$data = array($this->name => $data);
		}
		$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) {
		$data = $this->object_to_array($data);
		if (empty($data[$this->name])) {
			$data = array($this->name => $data);
		}
		if (!empty($data[$this->name]['id'])) {
			$model_data = $data[$this->name];
			$id = $model_data['id'];
			unset($model_data['id']);<commit_msg>Replacing an old reference to 'id' with $this->primary_key<commit_after><?php

class MvcModel {

	public $name = null;
	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 $properties = null;
	public $validation_error = null;
	public $validation_error_html = null;
	public $schema = null;
	public $wp_post = null;
	private $data_validator = null;
	private $db_adapter = null;
	private $wp_post_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,
			'group' => empty($this->group) ? null : $this->group,
			'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_schema();
		
		if ($this->wp_post) {
			$this->wp_post_adapter = new MvcPostAdapter();
			$this->wp_post_adapter->verify_settings($this);
			if (empty($this->belongs_to)) {
				$this->belongs_to = array();
			}
			$association = array(
				'Post' => array(
					'class' => 'MvcPost'
				)
			);
			$this->belongs_to = array_merge($association, $this->belongs_to);
		}
		
		$this->init_associations();
		$this->init_properties();
	
	}
	
	public function new_object($data) {
		$object = new MvcModelObject($this);
		foreach ($data as $field => $value) {
			$object->$field = $value;
		}
		$object = $this->process_objects($object);
		return $object;
	}
	
	public function create($data) {
		$data = $this->object_to_array($data);
		if (empty($data[$this->name])) {
			$data = array($this->name => $data);
		}
		$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) {
		$data = $this->object_to_array($data);
		if (empty($data[$this->name])) {
			$data = array($this->name => $data);
		}
		if (!empty($data[$this->name][$this->primary_key])) {
			$model_data = $data[$this->name];
			$id = $model_data[$this->primary_key];
			unset($model_data[$this->primary_key]);
{'fraction_non_alphanumeric': 0.20670647680293983, 'fraction_numerical': 0.0006124636349716736, 'mean_word_length': 4.127158555729984, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 163, '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': '6057173', 'n_tokens_mistral': 2612, 'n_tokens_neox': 2322, 'n_words': 418}