Skip to content
This repository was archived by the owner on Dec 29, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions src/Model/Behavior/SearchableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Search\Model\Behavior;

use Cake\Core\Configure;
use Cake\I18n\I18n;
use Cake\ORM\Behavior;
use Cake\ORM\Query;
use Cake\ORM\Table;
Expand Down Expand Up @@ -240,9 +241,8 @@ protected function _addCondLike($data, $field) {

$cond = [];
foreach ($fieldNames as $fieldName) {
if (strpos($fieldName, '.') === false) {
$fieldName = $this->_table->alias() . '.' . $fieldName;
}

$fieldName = $this->_getFullFieldName($fieldName);

if ($field['before'] === true) {
$field['before'] = '%';
Expand Down Expand Up @@ -325,9 +325,9 @@ protected function _addCondValue($data, $field) {

$cond = [];
foreach ($fieldNames as $fieldName) {
if (strpos($fieldName, '.') === false) {
$fieldName = $this->_table->alias() . '.' . $fieldName;
}

$fieldName = $this->_getFullFieldName($fieldName);

if (is_array($fieldValue) && empty($fieldValue)) {
continue;
}
Expand Down Expand Up @@ -375,4 +375,32 @@ protected function _addCondFinder(Query $query, $data, $field) {
}
return $query;
}


/**
* Returns the full field name including the table alias.
* If the table has the translate behavior and the field is configured to be
* translated, returns the corresponding field to filter by the translated
* content.
*
* @param $fieldName Field name
* @return string
*/
protected function _getFullFieldName($fieldName)
{
// If the fieldName already contains a table alias, do nothing.
if (strpos($fieldName, '.') === false) {
// If this is a translated field, search in the translated table instead of the original one
if ($this->_table->hasBehavior('Translate') &&
$this->_table->locale() != I18n::defaultLocale() &&
in_array($fieldName, $this->_table->behaviors()->get('Translate')->config('fields'))) {
$fieldName = $this->_table->alias() . '_' . $fieldName . '_translation.content';
} else {
$fieldName = $this->_table->alias() . '.' . $fieldName;
}
}

return $fieldName;
}

}
31 changes: 31 additions & 0 deletions tests/Fixture/ColorsFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Copyright 2009 - 2014, Cake Development Corporation (http://cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2009 - 2014, Cake Development Corporation (http://cakedc.com)
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Search\Test\Fixture;

use Cake\TestSuite\Fixture\TestFixture;

/**
* Tag Fixture
*/
class ColorsFixture extends TestFixture
{

/**
* Fields
*
* @var array $fields
*/
public $fields = [
'id' => ['type' => 'string', 'null' => false, 'default' => null, 'length' => 36],
'name' => ['type' => 'string', 'null' => false, 'default' => null, 'length' => 30],
];

}
Loading