Given this code
$mapper->reset();
$mapper->set( 'foo', 'bar' ); // <--- Error here: `foo` does not exists in the table
$mapper->insert();
Where foo
is a column that does not esist in the mapped table I get this error
Internal Server Error
SQLSTATE[42S22]: Column not found:
1054 Unknown column 'bar' in 'field list'
[/var/www/example.com/html/lib/php/fatfreeframework/DB/SQL.php:230]
The error message is misleading in fact the non existent column is foo
, not bar
: the latter is the value that has been attempted to set to the non-existent column.
Why does this happen? Is there a way to fix this?
-
Php 8.1.9
pdo_mysql Client API version => mysqlnd 8.1.9
mysql 8.0.30
By opening a issue on github I got the answer from @ikkez:
This is currently the expected behavior because setting a field not existing = defining an adhoc field.
$mapper->set('count_x',
'SELECT COUNT(id) from x where x.id = y.foreign_key group by x.id');
ref.: https://fatfreeframework.com/3.8/databases#VirtualFields
The way to fix this is to apply a whitelist of fillable fields that are allowed to be set in case you are using something like copyfrom
.
i.e.:
$mapper->copyfrom('POST', function($val) {
return array_intersect_key($val, array_flip(['first_name', 'last_name', 'age']));
});