cakephp/cakephp · error · BadMethodCallException
Unknown method ` ` called on
Error message
Unknown method `%s` called on `%s`
What it means
Table::__call() throws this BadMethodCallException when an undefined method is invoked on a Table instance that is neither a loaded behavior's method nor matches the findXxxByYyy() magic-finder pattern. It is the generic catch-all for typos and unsupported method names on table objects.
Solutions
- Check the spelling and existence of the method on the table class or its behaviors.
- Load the behavior that provides the intended method: $this->addBehavior('Xyz').
- Use explicit query builder calls (find(), updateQuery(), deleteQuery(), deleteAll()) instead of nonexistent magic methods.
- Only magic finders of the form findXxxByField() are supported — rewrite other patterns accordingly.
Example fix
// before $articles->deleteByUserId($userId); // BadMethodCallException // after $articles->deleteAll(['user_id' => $userId]);
Defensive patterns
Strategy: type-guard
Validate before calling
if (!method_exists($table, $method)) {
// log and refuse before calling the table
} Type guard
function tableHasCallable(\Cake\ORM\Table $t, string $method): bool {
return method_exists($t, $method) || str_contains($method, 'By');
} Try / catch
try {
$table->$method(...$args);
} catch (\BadMethodCallException $e) {
if (str_contains($e->getMessage(), 'Unknown method')) {
// fallback to query builder or surface error to developer
}
} Prevention
- Rely on IDE autocomplete against the concrete table class.
- Remember only findXxxByYyy() magic methods exist — not delete/update variants.
- Load behaviors in initialize() before calling their methods.
When it happens
Trigger: Calling $table->something() that does not exist as a real method, a behavior method, or a find...By magic finder, e.g. misspelled findAllBySlug() or nonexistent deleteByUserId().
Common situations: Typos in method names; assuming magic methods exist for operations other than find (delete/update/count); calling a behavior method without loading the behavior; outdated IDE stubs.
Related errors
- Cannot call ` `, it does not belong to any attached…
- Unknown finder method
- All primary key value(s) are needed for updating
- Argument 2 is expected to have a `repository` key that…
- Argument 2 is expected to match one of the…
AI-assisted analysis of cakephp/cakephp@1128eba9b0 (2026-09-12).
Data as JSON: /api/errors/e5872c88727cea8d.
Report an issue: GitHub.
Appendix: source
Thrown at src/ORM/Table.php:3000
*
* If your Table uses any behaviors you can call them as if
* they were on the table object.
*
* @param string $method name of the method to be invoked
* @param array $args List of arguments passed to the function
* @return mixed
* @throws \BadMethodCallException
*/
public function __call(string $method, array $args): mixed
{
if ($this->_behaviors->hasMethod($method)) {
return $this->_behaviors->call($method, $args);
}
if (preg_match('/^find(?:\w+)?By/', $method) > 0) {
return $this->_dynamicFinder($method, $args);
}
throw new BadMethodCallException(
sprintf('Unknown method `%s` called on `%s`', $method, static::class),
);
}
/**
* Returns the association named after the passed value if exists, otherwise
* throws an exception.
*
* @param string $property the association name
* @return \Cake\ORM\Association
* @throws \Cake\Database\Exception\DatabaseException if no association with such name exists
*/
public function __get(string $property): Association
{
$association = $this->_associations->get($property);
if (!$association) {
throw new DatabaseException(sprintf(
'Undefined property `%s`. ' .View on GitHub (pinned to 1128eba9b0)