Search code examples
cakephpsqlitecakephp-bakecakephp-3.2

Baking a Controller with Cakephp 3.2 using SQLite3 produces an error in the add() and edit() methods, Why is the code being generated incorrectly?


The error is that when creating the controller code a variable for an associated model is left blank, and that Model's name is left blank. The error happens when creating the controllers for all of my tables, but here is a complete example of one of them. I've replicated the problem on a fresh install of cakephp 3.2.

Here is the 2 lines of incorrect code generated, I've included the full details below:

$ = $this->Customers->->find('list', ['limit' => 200]);
    $this->set(compact('customer', ''));

My database configuration in config/app.php:

'default' => [
     'className' => 'Cake\Database\Connection',
     'driver' => 'Cake\Database\Driver\Sqlite',
     'persistent' => false,
     'host' => 'localhost',
     //'port' => 'non_standard_port_number',
     'username' => null,
     'password' => null,
     'database' => 'tgr.db',
     'encoding' => 'utf8',
     'timezone' => 'UTC',
     'flags' => [],
     'cacheMetadata' => true,
     'log' => false,
     'quoteIdentifiers' => false,
     'url' => env('DATABASE_URL', null),
]

The code created for the add() method which contains the incorrectly created code.

public function add(){
    $customer = $this->Customers->newEntity();
    if ($this->request->is('post')) {
        $customer = $this->Customers->patchEntity($customer, $this->request->data);
        if ($this->Customers->save($customer)) {
            $this->Flash->success(__('The customer has been saved.'));
            return $this->redirect(['action' => 'index']);
        } else {
            $this->Flash->error(__('The customer could not be saved. Please, try again.'));
        }
    }
    $ = $this->Customers->->find('list', ['limit' => 200]);
    $this->set(compact('customer', ''));
    $this->set('_serialize', ['customer']);
}

My database is stored int the application root and the app finds it and connects correctly from what I can tell. Database Schema:

-- Text encoding used: UTF-8
--
PRAGMA foreign_keys = off;
BEGIN TRANSACTION;

-- Table: customers
CREATE TABLE customers (_id INTEGER PRIMARY KEY,district_key INTEGER NOT NULL, name TEXT UNIQUE NOT NULL, short_name TEXT UNIQUE NOT NULL, job_count INTEGER, report_count INTEGER,  FOREIGN KEY (district_key) REFERENCES districts (_id)  );

COMMIT TRANSACTION;
PRAGMA foreign_keys = on;

Solution

  • I found the answer. My database id columns were _id (from the android default id column) and cakephp bakery was interpriting this to be a related Model with an empty name sense it was taking whatever comes before _id and creating a model for it. I've updated all of my database tables to reflect this and it is working correctly.