错误在于,在创建控制器代码时,关联模型的变量留空,而模型的名称留空。为我的所有表创建控制器时会发生错误,但这是其中一个的完整示例。我已经在全新安装的 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;