Skip to content

Commit be66c82

Browse files
dereuromarkclaude
andauthored
Fix findList deprecation in getRelatedInUse and getFieldInUse (#305)
* Fix findList deprecation in getRelatedInUse and getFieldInUse Since CakePHP 5.0, calling `findList` finder with options array is deprecated. Use named arguments instead. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Improve code. * Improve code. --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 0da9778 commit be66c82

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

src/Model/Table/Table.php

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -143,23 +143,38 @@ public function getRelatedInUse($tableName, $groupField = null, $type = 'all', a
143143
}
144144

145145
if ($type === 'list') {
146+
/** @var string $primaryKey */
147+
$primaryKey = $this->$tableName->getPrimaryKey();
148+
$displayField = $this->$tableName->getDisplayField();
149+
$defaults['fields'] = [$tableName . '.' . $primaryKey, $tableName . '.' . $displayField];
146150
$propertyName = $this->getAssociation($tableName)->getProperty();
147-
$defaults['fields'] = [$tableName . '.' . $this->$tableName->getPrimaryKey(), $tableName . '.' . $displayField];
148-
$defaults['keyField'] = $propertyName . '.' . $this->$tableName->getPrimaryKey();
149-
$defaults['valueField'] = $propertyName . '.' . $this->$tableName->getDisplayField();
151+
$keyField = $propertyName . '.' . $primaryKey;
152+
$valueField = $propertyName . '.' . $displayField;
150153

151-
if ($this->$tableName->getPrimaryKey() === $this->$tableName->getDisplayField()) {
152-
$defaults['group'] = [$tableName . '.' . $this->$tableName->getDisplayField()];
154+
if ($primaryKey === $displayField) {
155+
$defaults['group'] = [$tableName . '.' . $displayField];
153156
} else {
154-
$defaults['group'] = [$tableName . '.' . $this->$tableName->getPrimaryKey(), $tableName . '.' . $this->$tableName->getDisplayField()];
157+
$defaults['group'] = [$tableName . '.' . $primaryKey, $tableName . '.' . $displayField];
155158
}
156159

157160
$options += $defaults;
161+
unset($options['keyField'], $options['valueField']);
162+
163+
return $this->find(
164+
$type,
165+
contain: $options['contain'] ?? [],
166+
conditions: $options['conditions'] ?? [],
167+
fields: $options['fields'] ?? [],
168+
order: $options['order'] ?? [],
169+
group: $options['group'] ?? [],
170+
keyField: $keyField,
171+
valueField: $valueField,
172+
);
158173
}
159174

160175
$options += $defaults;
161176

162-
return $this->find($type, $options);
177+
return $this->find($type, ...$options);
163178
}
164179

165180
/**
@@ -182,12 +197,24 @@ public function getFieldInUse($groupField, $type = 'all', array $options = []) {
182197
];
183198
if ($type === 'list') {
184199
$defaults['fields'] = [$this->getPrimaryKey(), $this->getDisplayField(), $groupField];
185-
$defaults['keyField'] = $this->getPrimaryKey();
186-
$defaults['valueField'] = $this->getDisplayField();
200+
/** @var string $keyField */
201+
$keyField = $this->getPrimaryKey();
202+
$valueField = $this->getDisplayField();
203+
$options += $defaults;
204+
205+
return $this->find(
206+
$type,
207+
conditions: $options['conditions'] ?? [],
208+
fields: $options['fields'] ?? [],
209+
order: $options['order'] ?? [],
210+
group: $options['group'] ?? [],
211+
keyField: $keyField,
212+
valueField: $valueField,
213+
);
187214
}
188215
$options += $defaults;
189216

190-
return $this->find($type, $options);
217+
return $this->find($type, ...$options);
191218
}
192219

193220
/**

0 commit comments

Comments
 (0)