QLSTATE[42S22]: Column not found: 1054 Unknown column 'testcase.testdaten' in 'field list'",
....
"args": [
"SELECT `testcase`.`id` AS `id`, `testcase`.`name` AS `name`, `testcase`.`testdaten` AS `testdaten`, `testcase`.`json` AS `json`, `testcase`.`angelegt` AS `angelegt` FROM `testcase` LIMIT 10 OFFSET 0"
Funny enough, without knowing that you just asked this question about a custom extra column some hours ago, I wrote an email to John asking exactly this. Still waiting for response. But looking forward in solving this issue.
First of all welcome @obiwanke and @xyz to our forums . Ok, there is actually a frustration and I can explain (and at the end also give a solution).
Grocery CRUD datagrid work primarily with complicated database queries. So any field should unfortunately be in the database or create a custom Model to manipulate that. However as I can understand that it is a bit of non-sense to create a custom Model only for that there is an undocumented (that I will document soon) work-around for that.
There is a function with name “mapColumn” in order to map a field with another one. Actually I will give an example to be more clear. Let’s say that we have a new field name “whatever”. As the “whatever” field doesn’t exist we need to map it with another field, let’s say “email”. This will look like this:
$crud->columns(['customer_name','test_choices', 'country', 'email', 'whatever']);
$crud->fieldTypeColumn('whatever', 'varchar');
$crud->mapColumn('whatever', 'email');
$crud->callbackColumn('whatever', function ($value) {
// The $value here is the 'email' value
return "<div style='background:red'>$value</div>";
});
Keep in mind that if you use a field only on column it is better to use fieldTypeColumn instead of fieldType as it is specified only for the initial datagrid.
Last but not least, I am keeping a note to create a new field type with name “fake” to solve this issue without a work-around in the future.
thanks for this work-around. It works as described.
It’s doing it by now, but the problem is, you always have to put all column names in the array of the $crud-columns call to get this extra “whatever” column. At the moment I had no $crud->columns method call, but with this work-araound I have to enumerate all the names of all the columns in the table.
Anyway, thanks
Regards
Obiwanke
I’ve kept a note so I can include the “fake” field type but I can’t promise any dates for that so till then you can use a helper function for Codeigniter 4. For example, you can use a helper that will look like this:
function getAllDatabaseColumns($tableName) {
// Code for Codeigniter 4
$db = db_connect();
$query = $db->query('SHOW FIELDS FROM `' . $tableName . '`');
$result = $query->getResult();
$columnNames = [];
foreach ($result as $row) {
$columnNames[] = $row->Field;
}
return $columnNames;
};
And you can simply get all the columnNames by using:
getAllDatabaseColumns('yourTableName')
If you would also like to add the extra columns you can use the array_merge of PHP. So at the above example this will look like this:
Also keep in mind that I always recommend to use your common functions into one place. For example, if you would like to have an extra column with all the fields, you can always have a function like this:
function groceryCRUDAddExtraColumn($crud, $columnName) {
$crud->fieldTypeColumn($columnName, 'varchar');
$crud->mapColumn($columnName, 'id');
}