cakephp:模型忽略数字前 table 名称中的下划线

cakephp: model ignoring underscore in table name before number

我制作了一个 cakephp 应用程序,但我使用的数据库是 crm,我无法更改 table 名称,因此这是一个限制。我的 table 名字是 emails_users_1_c。当我试图在模型中调用这个 table 名称时。

它将 1 附加到用户并使其成为 documents_leads1_c 我不知道为什么会这样,尽管我在 SO 和文档中进行了搜索但无法找到答案。

Table 'crm.emails_users1_c' doesn't exist".

这就是我访问 table 的方式。

public function makeDocument($data){
        $docuTable = TableRegistry::get('emails_users_1_c');

        $docuData = $docuTable->newEntity($data);

        $docuTable->save($docuData);
    }

不要使用这样的数据库 table 名称作为 table class 名称/别名,这不适用于内部应用的变形(驼峰化后跟下划线这个具体案例)。使用符合 CakePHP 命名约定的有意义的名称,这将帮助您长 运行.

理想情况下创建具体的 table class 具有适当名称的实体,例如 UserEmailsTable,并通过 \Cake\ORM\Table::setTable() 配置数据库 table 名称:

namespace App\Model\Table;

use Cake\ORM\Table;

class UserEmailsTable extends Table
{
    public function initialize(array $config)
    {
        $this->setTable('emails_users_1_c'); // $this->table() before CakePHP 3.4
        // ...
    }
}

如果由于某种原因无法使用具体的 table classes't/shouldn,请即时配置别名,以便您仍然可以轻松地重用它们:

TableRegistry::config('UserEmails', [
    'table' => 'emails_users_1_c'
]);

在任何情况下,您都应该能够通过 UserEmails 别名检索 table class 实例:

$UserEmails = TableRegistry::get('UserEmails');

另见