/**
* Generates a SQL sub-query for replacing in ORDER BY clause.
*
* @param string $column Name of the column being replaced by this sub-query
* @param string|null $bundle Consider attributes only for a specific bundle
* @return string SQL sub-query statement
*/
protected function _subQuery($column, $bundle = null)
{
$alias = $this->_table->alias();
$pk = $this->_table->primaryKey();
$type = $this->_toolbox->getType($column);
$subConditions = ['EavAttribute.table_alias' => $this->_table->table(), 'EavValues.entity_id' => "{$alias}.{$pk}", 'EavAttribute.name' => $column];
if (!empty($bundle)) {
$subConditions['EavAttribute.bundle'] = $bundle;
}
$subQuery = TableRegistry::get('Eav.EavValues')->find()->contain(['EavAttribute'])->select(["EavValues.value_{$type}"])->where($subConditions)->sql();
return str_replace([':c0', ':c1', ':c2', ':c3'], ['"' . $this->_table->table() . '"', "{$alias}.{$pk}", '"' . $column . '"', '"' . $bundle . '"'], $subQuery);
}