<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] = $this->defaultSettings + $settings;
	}

	public function beforeFind(Model $Model, $query) {
		if ($this->settings[$Model->alias]['automaticJoins']) {
			foreach ((array)$query['conditions'] as $key => $val) {
				list($modelName, $field) = $this->_split($key);
				if (isset($Model->hasAndBelongsToMany[$modelName])) {
					$query['filter'][$modelName][$modelName . '.' . $field] = $val;
					unset($query['conditions'][$key]);
				}
			}
		}

		if (empty($query['filter'])) {
			return true;
		}
		foreach ($query['filter'] as $modelName => $conditions) {
			if (!isset($Model->hasAndBelongsToMany[$modelName])) {
				throw new Exception(sprintf('Model "%s" does not have an HABTM relation to "%s"', $Model->alias, $modelName));
			}

			$association = $Model->hasAndBelongsToMany[$modelName];
			list(, $with) = $this->_split($association['with']);
			$query['joins'][] = array(
				'table' => $association['joinTable'],
				'alias' => $with,
				'type' => 'INNER',
				'foreignKey' => false,
				'conditions' => array(
					$Model->alias . '.' . $Model->primaryKey . ' = ' . $with . '.' . $association['foreignKey']
				)
			);

			$query['joins'][] = array(
				'table' => $Model->{$modelName}->table,
				'alias' => $Model->{$modelName}->alias,
				'type' => 'INNER',
				'foreignKey' => false,
				'conditions' => array(
					$Model->{$modelName}->alias . '.' . $Model->{$modelName}->primaryKey . ' = ' . $with . '.' . $association['associationForeignKey']
				)
			);

			if (!empty($conditions)) {
				$query['conditions'] = Hash::merge($query['conditions'], $conditions);
			}
		}<commit_msg>Fixed error in FilterHabtmBehavior when using custom with models<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] = $this->defaultSettings + $settings;
	}

	public function beforeFind(Model $Model, $query) {
		if ($this->settings[$Model->alias]['automaticJoins']) {
			foreach ((array)$query['conditions'] as $key => $val) {
				list($modelName, $field) = $this->_split($key);
				if (isset($Model->hasAndBelongsToMany[$modelName])) {
					$query['filter'][$modelName][$modelName . '.' . $field] = $val;
					unset($query['conditions'][$key]);
				}
			}
		}
		if (empty($query['filter'])) {
			return true;
		}
		foreach ($query['filter'] as $modelName => $conditions) {
			if (!isset($Model->hasAndBelongsToMany[$modelName])) {
				throw new Exception(sprintf('Model "%s" does not have an HABTM relation to "%s"', $Model->alias, $modelName));
			}

			$association = $Model->hasAndBelongsToMany[$modelName];
			list(, $with) = $this->_split($association['with']);

			$query['joins'][] = array(
				'table' => $Model->{$with}->useTable,
				'alias' => $Model->{$with}->alias,
				'type' => 'INNER',
				'foreignKey' => false,
				'conditions' => array(
					$Model->alias . '.' . $Model->primaryKey . ' = ' . $with . '.' . $association['foreignKey']
				)
			);
			$query['joins'][] = array(
				'table' => $Model->{$modelName}->table,
				'alias' => $Model->{$modelName}->alias,
				'type' => 'INNER',
				'foreignKey' => false,
				'conditions' => array(
					$Model->{$modelName}->alias . '.' . $Model->{$modelName}->primaryKey . ' = ' . $with . '.' . $association['associationForeignKey']
				)
			);

			if (!empty($conditions)) {
				$query['conditions'] = Hash::merge($query['conditions'], $conditions);
			}
		}
{'fraction_non_alphanumeric': 0.2191921790583998, 'fraction_numerical': 0.0, 'mean_word_length': 4.010309278350515, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 75, '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': '3625575', 'n_tokens_mistral': 1491, 'n_tokens_neox': 1266, 'n_words': 232}