0
votes

Thanks in Advance,

When i run bellow Code its showing following error while i add a User.

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'bookstore.users' doesn't exist

I have user Controller in \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);
    }
}

And Model for Users Table is in following file \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');
    }
}

And Model for User Table's Entity is in following file \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 Structure:-

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 
1

1 Answers

2
votes

You are loading/accessing the model in the wrong way. While you can use slashes in the alias to match a subfolder structure, this will create a property with that exact name, ie including the slash.

Loading a model using an alias with special characters will require that you access the property via {}, like $this->{'Admin/Users'}, which is pretty ugly, and should better be avoided.

Also in your add() action you are using the Users alias, which will not look in the subfolder, and hence either pick up the wrong file, or none at all, causing an auto-table to be used (an instance of \Cake\ORM\Table instead of a concrete subclass thereof). Similarly the controllers model autoloading functionality will also look for Users by default.

So long story short, if you want to use loadModel(), then use its return value, either directly in your actions:

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

or by passing it to a property, which you could do in beforeFilter() if you need that model in all actions:

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