[bug] Mysql view error on read: Trying to access array offset on value of type null

I’m getting the error below while using a mysql/mariadb view as table:

//  mysql view "table"
$crud->setTable('view_name');

// "primary key" on view 
$crud->setPrimaryKey('id', 'view_name');

I know Johnny that you’ve corrected this bug previously for the ‘add’, ‘edit’ and ‘delete’ operations. Could you please apply the same patch to ‘read’?

TIA.
Bernhard Enders

CRITICAL - 2023-07-26 16:22:57 --> Trying to access array offset on value of type null
in VENDORPATH\grocery-crud\enterprise\src\GroceryCrud\Core\Model.php on line 320.
 1 VENDORPATH\grocery-crud\enterprise\src\GroceryCrud\Core\Model.php(320): CodeIgniter\Debug\Exceptions->errorHandler()
 2 VENDORPATH\grocery-crud\enterprise\src\GroceryCrud\Core\Model.php(295): GroceryCrud\Core\Model->_getPrimaryKey()
 3 VENDORPATH\grocery-crud\enterprise\src\GroceryCrud\Core\State\StateAbstract.php(1214): GroceryCrud\Core\Model->getPrimaryKeyField()
 4 VENDORPATH\grocery-crud\enterprise\src\GroceryCrud\Core\State\ReadFormState.php(34): GroceryCrud\Core\State\StateAbstract->getPrimaryKeyName()
 5 VENDORPATH\grocery-crud\enterprise\src\GroceryCrud\Core\State\ReadFormState.php(53): GroceryCrud\Core\State\ReadFormState->initialize()
 6 VENDORPATH\grocery-crud\enterprise\src\GroceryCrud\Core\GroceryCrud.php(2867): GroceryCrud\Core\State\ReadFormState->render()
 7 APPPATH\Controllers\Admin\Users.php(162): GroceryCrud\Core\GroceryCrud->render()
 8 SYSTEMPATH\CodeIgniter.php(932): App\Controllers\Admin\Users->index()
 9 SYSTEMPATH\CodeIgniter.php(497): CodeIgniter\CodeIgniter->runController()
10 SYSTEMPATH\CodeIgniter.php(366): CodeIgniter\CodeIgniter->handleRequest()
11 FCPATH\index.php(67): CodeIgniter\CodeIgniter->run()

Hello @bgeneto ,

Missed that for version .10 but included for the next version (.11). Till then since this is a one-liner code fix, I am posting the diff code here:

diff --git a/src/GroceryCrud/Core/State/ReadFormState.php b/src/GroceryCrud/Core/State/ReadFormState.php
--- a/src/GroceryCrud/Core/State/ReadFormState.php
+++ b/src/GroceryCrud/Core/State/ReadFormState.php
@@ -25,6 +25,7 @@ class ReadFormState extends StateAbstract {
 
     public function initialize()
     {
+        $this->setInitialData();
         $model = $this->gCrud->getModel();
 
         if ($this->gCrud->getWhere() !== null) {