fix: [componenent:CRUD] Fixed oneToMany contextual filters
parent
41948044cd
commit
d61c6c930a
|
@ -512,13 +512,24 @@ class CRUDComponent extends Component
|
||||||
if (count($exploded) > 1) {
|
if (count($exploded) > 1) {
|
||||||
$model = $exploded[0];
|
$model = $exploded[0];
|
||||||
$subField = $exploded[1];
|
$subField = $exploded[1];
|
||||||
$associationType = $this->Table->associations()->get($model)->type();
|
$association = $this->Table->associations()->get($model);
|
||||||
if ($associationType == 'oneToMany' || $associationType == 'manyToMany') {
|
$associationType = $association->type();
|
||||||
$fieldToExtract = sprintf('%s.{*}.%s', strtolower($model), $subField);
|
if ($associationType == 'oneToMany') {
|
||||||
} else {
|
$fieldToExtract = $subField;
|
||||||
|
$associatedTable = $association->getTarget();
|
||||||
|
$query = $associatedTable->find()->rightJoin(
|
||||||
|
[$this->Table->getAlias() => $this->Table->getTable()],
|
||||||
|
[sprintf('%s.id = %s.%s', $this->Table->getAlias(), $associatedTable->getAlias(), $association->getForeignKey())]
|
||||||
|
)
|
||||||
|
->where([
|
||||||
|
["${field} IS NOT" => NULL]
|
||||||
|
]);
|
||||||
|
} else if ($associationType == 'manyToOne') {
|
||||||
$fieldToExtract = sprintf('%s.%s', Inflector::singularize(strtolower($model)), $subField);
|
$fieldToExtract = sprintf('%s.%s', Inflector::singularize(strtolower($model)), $subField);
|
||||||
|
$query = $this->Table->find()->contain($model);
|
||||||
|
} else {
|
||||||
|
throw new Exception("Association ${associationType} not supported in CRUD Component");
|
||||||
}
|
}
|
||||||
$query = $this->Table->find()->contain($model);
|
|
||||||
} else {
|
} else {
|
||||||
$fieldToExtract = $field;
|
$fieldToExtract = $field;
|
||||||
$query = $this->Table->find();
|
$query = $this->Table->find();
|
||||||
|
|
Loading…
Reference in New Issue