CakePHP 基础 Table 未找到视图

CakePHP Base Table View not Found

提前致谢,

当我 运行 添加用户时,代码显示以下错误。

SQLSTATE[42S02]:未找到基础 table 或视图:1146 Table 'bookstore.users' 不存在

我在 \src\Controller\Admin\UsersController.php

中有用户控制器
<?php
namespace App\Controller\Admin;

use Cake\Core\Configure;
use Cake\Network\Exception\ForbiddenException;
use Cake\Network\Exception\NotFoundException;
use Cake\View\Exception\MissingTemplateException;
use Cake\Event\Event;

class UsersController extends AppController
{
    public function beforeFilter(Event $event)
    {
        parent::beforeFilter($event);
        $this->loadModel('Admin/Users');
    }

    public function index()
    {
        $this->set('headerinfo', array('seoTitle' => 'Dash Board', 'pageTitle' => 'Dashboard', 'breadcrumbs' => null));
        $this->set('users', $this->Users->find('all'));
    }

    public function add()
    {
        $breadcrumbs = array(
            array('', 'Users')
        );
        $this->set('headerinfo', array('seoTitle' => 'Add User', 'pageTitle' => 'Add User', 'breadcrumbs' => null));

        $this->loadModel('Users');
        $user = $this->Users->newEntity();

        if ($this->request->is('post')) {
            $user = $this->Users->patchEntity($user, $this->request->getData());
            if ($this->Users->save($user)) {
                $this->Flash->success(__('User inserted successfully.'));
                return $this->redirect(['action' => 'add']);
            }
            $this->Flash->error(__('Unable to add the user, please contact support'));
        }
        $this->set('user', $user);
    }
}

用户模型 Table 在以下文件中 \src\Model\Table\Admin\UsersTable.php

<?php

namespace App\Model\Table\Admin;

use App\Model\Entity\Admin\User;
use Cake\ORM\Table;
use Cake\ORM\Query;
use Cake\Routing\Router;
use Cake\Validation\Validator;

class UsersTable extends Table
{
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->table('admin_users');
        $this->primaryKey('auID');
    }
}

用户 Table 的实体模型在以下文件中 \src\Model\Entity\Admin\User.php

<?php

namespace App\Model\Entity\Admin;

use Cake\ORM\Entity;
use Cake\Auth\DefaultPasswordHasher;

class User extends Entity {

    protected $_accessible = [
        '*' => true,
        'auID' => false,
    ];

    protected function _setAuPass($password){
        return (new DefaultPasswordHasher)->hash($password);
    }
}

Table结构:-

admin_users

Column  Type    Null    Default Comments    MIME
auID    int(11) No           
auName  varchar(75) No           
auPass  varchar(255)    No           
auFullName  varchar(75) No           
auStatus    varchar(2)  Yes     A   A=Active, I=Inactive     
createdOn   datetime    Yes     NULL         
modifiedOn  datetime    Yes     NULL         
Indexes
Keyname Type    Unique  Packed  Column  Cardinality Collation   Null    Comment
PRIMARY BTREE   Yes No  auID    1   A   No  
auName  BTREE   No  No  auName  1   A   No  
auStatus    BTREE   No  No  auStatus    1   A   Yes 

你是loading/accessing模特的错误方式。虽然您可以在别名中使用斜杠来匹配子文件夹结构,但这将创建一个具有该确切名称的 属性,即包括斜杠。

使用带有特殊字符的别名加载模型将要求您通过 {} 访问 属性,例如 $this->{'Admin/Users'},这非常难看,最好避免。

此外,在您的 add() 操作中,您使用的是 Users 别名,它不会在子文件夹中查找,因此要么选择了错误的文件,要么根本 none ,导致使用 auto-table(\Cake\ORM\Table 的实例而不是其具体子类)。同样,控制器模型自动加载功能也会默认查找 Users

长话短说,如果你想使用 loadModel(),那么直接在你的操作中使用它的 return 值:

$Users = loadModel('Admin/Users');

或将其传递给 属性,如果您在所有操作中都需要该模型,您 可以 beforeFilter() 中执行此操作:

$this->Users = loadModel('Admin/Users');