<commit_before><?php 

/****************************************************************************************************************

	CHILD CLASS EXAMPLE:
		
		class User extends DinklyDataModel
		{
			public $registry = array(
				'id'              => 'ID',
				'created'         => 'Created',
				'modified'        => 'Modified',
				'is_deleted'      => 'Deleted',
				'first_name'      => 'FirstName',
				'last_name'       => 'LastName',
			);
			
			public $dbTable = 'users';
		}
		
	CHILD CLASS USAGE EXAMPLE:
		
		$user = new User();
		$user->init(1);
		echo $user->getCreated() 

***************************************************************************************************************/

abstract class DinklyDataModel extends DinklyDataConnector
{
	protected $db;
	protected $dbTable;
	protected $isNew;
	protected $regDirty = array();
	protected $registry = array();
	
	public function __construct($db = null)
	{
		if(!$db) $this->db = self::fetchDB();
		else $this->db = $db;

		$this->isNew = true;
		
		foreach($this->getRegistry() as $element) { $this->$element = NULL; }
	}

	public function init($id)
	{
		$Select = $this->getSelectQuery() . " where id=" . $this->db->quote($id);
		$result = $this->db->query($Select)->fetchAll();
				
		if($result != array())
		{
			$this->hydrate($result, true);
		}
	}

	/* Init object with properties other than id. Example: $user->initWith(array('Username' => $username)); */
	public function initWith($properties = array())
	{
		if($properties != array())
		{
			$cols = array();
			foreach($properties as $property => $value)
			{
				$col_name = Dinkly::convertFromCamelCase($property);
				if(array_key_exists($col_name, $this->registry)) $cols[$col_name] = $value;
			}

			$where = '';
			foreach($cols as $col => $value)
			{
				$where .= ' AND ' . $col . ' = ' . $this->db->quote($value); 
			}
			$where = ' where ' . trim($where, ' AND');

			$query = $this->getSelectQuery() . $where;
			$result = $this->db->query($query)->fetchAll();
					
			if($result != array())
			{
				$this->hydrate($result, true);
			}
			else return false;
		}
		else return false;
	}

	/* Return object properties as array */
	public function toArray($scrub_values = array())
 	{
 		foreach($this->getRegistry() as $key => $element)
 		{
 			if(!in_array($key, $scrub_values))
 			{
 				$array[$key] = $this->$element;
 			}
 		}
 		return $array;
 	}
	
	/* Only manipulates fields that have been modified in some way */
	public function save($force_insert = false)
	{
		if(!$this->isNew && !$force_insert) { return $this->update(); }
		else { return $this->insert(); }
	}
	
	public function getSelectQuery()
	{ 
		return "select " . implode(", ", $this->getColumns()) . " from " . $this->getDBTable();
	}
	<commit_msg>removed reference to scrub values<commit_after><?php 

/****************************************************************************************************************

	CHILD CLASS EXAMPLE:
		
		class User extends DinklyDataModel
		{
			public $registry = array(
				'id'              => 'ID',
				'created'         => 'Created',
				'modified'        => 'Modified',
				'is_deleted'      => 'Deleted',
				'first_name'      => 'FirstName',
				'last_name'       => 'LastName',
			);
			
			public $dbTable = 'users';
		}
		
	CHILD CLASS USAGE EXAMPLE:
		
		$user = new User();
		$user->init(1);
		echo $user->getCreated() 

***************************************************************************************************************/

abstract class DinklyDataModel extends DinklyDataConnector
{
	protected $db;
	protected $dbTable;
	protected $isNew;
	protected $regDirty = array();
	protected $registry = array();
	
	public function __construct($db = null)
	{
		if(!$db) $this->db = self::fetchDB();
		else $this->db = $db;

		$this->isNew = true;
		
		foreach($this->getRegistry() as $element) { $this->$element = NULL; }
	}

	public function init($id)
	{
		$Select = $this->getSelectQuery() . " where id=" . $this->db->quote($id);
		$result = $this->db->query($Select)->fetchAll();
				
		if($result != array())
		{
			$this->hydrate($result, true);
		}
	}

	/* Init object with properties other than id. Example: $user->initWith(array('Username' => $username)); */
	public function initWith($properties = array())
	{
		if($properties != array())
		{
			$cols = array();
			foreach($properties as $property => $value)
			{
				$col_name = Dinkly::convertFromCamelCase($property);
				if(array_key_exists($col_name, $this->registry)) $cols[$col_name] = $value;
			}

			$where = '';
			foreach($cols as $col => $value)
			{
				$where .= ' AND ' . $col . ' = ' . $this->db->quote($value); 
			}
			$where = ' where ' . trim($where, ' AND');

			$query = $this->getSelectQuery() . $where;
			$result = $this->db->query($query)->fetchAll();
					
			if($result != array())
			{
				$this->hydrate($result, true);
			}
			else return false;
		}
		else return false;
	}

	/* Return object properties as array */
	public function toArray()
 	{
 		foreach($this->getRegistry() as $key => $element)
 		{
 			if(!in_array($key, $scrub_values))
 			{
 				$array[$key] = $this->$element;
 			}
 		}
 		return $array;
 	}
	
	/* Only manipulates fields that have been modified in some way */
	public function save($force_insert = false)
	{
		if(!$this->isNew && !$force_insert) { return $this->update(); }
		else { return $this->insert(); }
	}
	
	public function getSelectQuery()
	{ 
		return "select " . implode(", ", $this->getColumns()) . " from " . $this->getDBTable();
	}
	
{'fraction_non_alphanumeric': 0.25094509450945096, 'fraction_numerical': 0.00036003600360036, 'mean_word_length': 3.350822239624119, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 83, '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': '1056968', 'n_tokens_mistral': 2064, 'n_tokens_neox': 1759, 'n_words': 381}