使用 SQLite3 使用 Cakephp 3.2 烘焙控制器会在 add() 和 edit() 方法中产生错误,为什么代码生成不正确?
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?
错误是在创建控制器代码时,关联模型的变量留空,并且该模型的名称也留空。为我的所有表创建控制器时会发生错误,但这里是其中一个的完整示例。我在全新安装的 cakephp 3.2.
上重现了这个问题
这是生成的 2 行错误代码,我在下面包含了完整的详细信息:
$ = $this->Customers->->find('list', ['limit' => 200]);
$this->set(compact('customer', ''));
我的数据库配置在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),
]
为 add() 方法创建的代码包含错误创建的代码。
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']);
}
我的数据库存储在应用程序根目录中,据我所知,应用程序可以找到它并正确连接。数据库架构:
-- 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;
我找到了答案。我的数据库 id 列是 _id(来自 android 默认 id 列),cakephp bakery 将其解释为具有空名称意义的相关模型,它正在获取 _id 之前的任何内容并为其创建模型。我已经更新了我所有的数据库表以反映这一点并且它工作正常。
错误是在创建控制器代码时,关联模型的变量留空,并且该模型的名称也留空。为我的所有表创建控制器时会发生错误,但这里是其中一个的完整示例。我在全新安装的 cakephp 3.2.
上重现了这个问题这是生成的 2 行错误代码,我在下面包含了完整的详细信息:
$ = $this->Customers->->find('list', ['limit' => 200]);
$this->set(compact('customer', ''));
我的数据库配置在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),
]
为 add() 方法创建的代码包含错误创建的代码。
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']);
}
我的数据库存储在应用程序根目录中,据我所知,应用程序可以找到它并正确连接。数据库架构:
-- 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;
我找到了答案。我的数据库 id 列是 _id(来自 android 默认 id 列),cakephp bakery 将其解释为具有空名称意义的相关模型,它正在获取 _id 之前的任何内容并为其创建模型。我已经更新了我所有的数据库表以反映这一点并且它工作正常。