<commit_before><?php
App::uses('ModelBehavior', 'Model/Behavior');
class FilterHabtmBehavior extends ModelBehavior {

	public $defaultSettings = array(
		'automaticJoins' => true
	);

	public $settings = array();

	public function setup(Model $Model, $settings = array()) {
		$this->settings[$Model->alias] = $settings + $this->defaultSettings;
	}

	public function beforeFind(Model $Model, $query) {
		if ($this->settings[$Model->alias]['automaticJoins']) {
			$joins = $this->extractJoins($Model, $query['conditions']);
			foreach ($joins as $join) {
				$query['joins'][] = $join;
			}
		}
		return $query;
	}

	public function extractJoins(Model $Model, $conditions) {
		if (!is_array($conditions)) {
			return array();
		}

		$joins = array();
		foreach ($conditions as $key => $val) {
			if (is_numeric($key)) {
				$joins = array_merge($joins, $this->extractJoins($Model, $val));
				continue;
			}

			list($habtmModel) = pluginSplit($key);
			if (!isset($Model->hasAndBelongsToMany[$habtmModel])) {
				continue;
			}

			$association = $Model->hasAndBelongsToMany[$habtmModel];
			list($plugin, $withModel) = pluginSplit($association['with']);

			if (!isset($joins[$withModel])) {
				$joins[$withModel] = array(
					'table' => $Model->{$withModel}->useTable,
					'alias' => $Model->{$withModel}->alias,
					'type' => 'INNER',
					'foreignKey' => false,
					'conditions' => array(
						$Model->alias . '.' . $Model->primaryKey . ' = ' . $withModel . '.' . $association['foreignKey']
					)
				);
			}
			if (!isset($joins[$habtmModel])) {
				$joins[$habtmModel] = array(
					'table' => $Model->{$habtmModel}->table,
					'alias' => $Model->{$habtmModel}->alias,
					'type' => 'INNER',
					'foreignKey' => false,
					'conditions' => array(
						$Model->{$habtmModel}->alias . '.' . $Model->{$habtmModel}->primaryKey . ' = ' . $withModel . '.' . $association['associationForeignKey']
					)
				);
			}
		}
		return $joins;
	}

}<commit_msg>Use with model's alias<commit_after><?php
App::uses('ModelBehavior', 'Model/Behavior');
class FilterHabtmBehavior extends ModelBehavior {

	public $defaultSettings = array(
		'automaticJoins' => true
	);

	public $settings = array();

	public function setup(Model $Model, $settings = array()) {
		$this->settings[$Model->alias] = $settings + $this->defaultSettings;
	}

	public function beforeFind(Model $Model, $query) {
		if ($this->settings[$Model->alias]['automaticJoins']) {
			$joins = $this->extractJoins($Model, $query['conditions']);
			foreach ($joins as $join) {
				$query['joins'][] = $join;
			}
		}
		return $query;
	}

	public function extractJoins(Model $Model, $conditions) {
		if (!is_array($conditions)) {
			return array();
		}

		$joins = array();
		foreach ($conditions as $key => $val) {
			if (is_numeric($key)) {
				$joins = array_merge($joins, $this->extractJoins($Model, $val));
				continue;
			}

			list($habtmModel) = pluginSplit($key);
			if (!isset($Model->hasAndBelongsToMany[$habtmModel])) {
				continue;
			}

			$association = $Model->hasAndBelongsToMany[$habtmModel];
			list($plugin, $withModel) = pluginSplit($association['with']);

			if (!isset($joins[$withModel])) {
				$joins[$withModel] = array(
					'table' => $Model->{$withModel}->useTable,
					'alias' => $Model->{$withModel}->alias,
					'type' => 'INNER',
					'foreignKey' => false,
					'conditions' => array(
						$Model->alias . '.' . $Model->primaryKey . ' = ' . $Model->{$withModel}->alias . '.' . $association['foreignKey']
					)
				);
			}
			if (!isset($joins[$habtmModel])) {
				$joins[$habtmModel] = array(
					'table' => $Model->{$habtmModel}->table,
					'alias' => $Model->{$habtmModel}->alias,
					'type' => 'INNER',
					'foreignKey' => false,
					'conditions' => array(
						$Model->{$habtmModel}->alias . '.' . $Model->{$habtmModel}->primaryKey . ' = ' . $Model->{$withModel}->alias . '.' . $association['associationForeignKey']
					)
				);
			}
		}
		return $joins;
	}

}
{'fraction_non_alphanumeric': 0.20786092214663643, 'fraction_numerical': 0.0, 'mean_word_length': 3.692671394799054, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 77, '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': '2256245', 'n_tokens_mistral': 1614, 'n_tokens_neox': 1339, 'n_words': 229}