<commit_before><?php

App::import('Model', 'Entity.Entity');

class EntityModel extends EntityAppModel {
	protected $entity;
	
	public function toEntity($data) {
		if (empty($this->entity)) return null;
		
		if (empty($data[$this->name]['id'])) return null;
		
		$class = $this->entity;
		if (!class_exists($class)) {
			if (!App::import('Model', $class)) {
				return null;
			}
		}
		
		$entity = new $class();
		$entity->init($this, $data);
		return $entity;
	}
	
	public function toEntities($list_of_data) {
		$result = array();
		foreach ($list_of_data as $data) {
			$result[] = $this->toEntity($data);
		}
		return $result;
	}
	
	public function afterFind($result, $primary) {
		$result = parent::afterFind($result, $primary);
		
		if ($primary and !empty($this->entity)) {
			$result = $this->toEntities($result);
		}
		return $result;
	}
}

<commit_msg>entityByXXX() allEntitiesBy() で検索できるように実装。 findでの検索時には、entity=>true を渡さなければ自動で変換しなく変更。<commit_after><?php

App::import('Model', 'Entity.Entity');

class EntityModel extends EntityAppModel {
	protected $entity;
	protected $convertToEntity;
	
	public function toEntity($data) {
		if (empty($this->entity)) return null;
		
		if (empty($data[$this->name]['id'])) return null;
		
		$class = $this->entity;
		if (!class_exists($class)) {
			if (!App::import('Model', $class)) {
				return null;
			}
		}
		
		$entity = new $class();
		$entity->init($this, $data);
		return $entity;
	}
	
	public function toEntities($list_of_data) {
		$result = array();
		foreach ($list_of_data as $data) {
			$result[] = $this->toEntity($data);
		}
		return $result;
	}
	
	public function beforeFind($queryData) {
		$this->convertToEntity = !empty($queryData['entity']);
		
		return parent::beforeFind($queryData);
	}
	
	public function afterFind($result, $primary) {
		$result = parent::afterFind($result, $primary);
		
		if ($this->convertToEntity and $primary and !empty($this->entity)) {
			$result = $this->toEntities($result);
		}
		
		return $result;
	}
	
	public function call__($method, $params) {
		$to_entity = false;
		$all = false;
		
		if (preg_match('/^(entity|allentities)by(.+)$/i', $method, $matches)) {
			$to_entity = true;
			$all = (strtolower($matches[1]) == 'allentities');
			$method = ($all ? 'findAllBy' : 'findBy'). $matches[2];
		}
		
		$return = parent::call__($method, $params);
		
		if ($to_entity) {
			$return = ($all ? $this->toEntities($return) : $this->toEntity($return));
		}
		
		return $return;
	}
}


{'fraction_non_alphanumeric': 0.18782748891575976, 'fraction_numerical': 0.0008061265618702137, 'mean_word_length': 4.20335429769392, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 22, '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': '886164', 'n_tokens_mistral': 987, 'n_tokens_neox': 867, 'n_words': 176}