0

I have a table in mydatabase. I want to create two data modals. Is it possible in cakephp?

Additional Information

There is a Table for users. User may be a teacher or a student. If user is Teacher there is different modal and for student there is also a different data modal.

Kritik
  • 210
  • 2
  • 14

3 Answers3

0

Of course you can create different models using the same table name. To do so, link each model with specific table with $useTable property (not $table):

class Teacher extends AppModel {
    public $useTable = 'User';
}

Find more detailed solutions here and also here

Community
  • 1
  • 1
prats1411
  • 162
  • 12
0

In CakePHP2, you can use the $useTable attribute of a model to set the table to be used:

class Example extends AppModel {
    public $useTable = 'exmp'; // This model uses a database table 'exmp'
}

It looks like it might need to be done differently in CakePHP 3 (or v3 adds a different way of doing it) (see this page)

use Cake\ORM\TableRegistry;

$users = TableRegistry::get('Users');
$user = $users->newEntity(['email' => 'mark@example.com']);
$users->save($user);
gabe3886
  • 4,235
  • 3
  • 27
  • 31
0

In CakePHP2

class Teacher extends AppModel {
    public $useTable = 'users';
}

But you should be using relationships to distinguish your different user types, you can do this by simply having the Users table with the core information, then create a UserType table linked to specify the type, i.e.

Users Table

id, user_type_id, username, password, created, modified

1, 1, Stephen, somehash, 2015-04-15 00:00:00, 2015-04-15 00:00:00

2, 2, Alex, somehash, 2015-04-15 00:00:00, 2015-04-15 00:00:00

3, 2, Jane, somehash, 2015-04-15 00:00:00, 2015-04-15 00:00:00

UserType Table

id, name, created, modified

1, Teacher, 2015-04-15 00:00:00, 2015-04-15 00:00:00

2, Student, 2015-04-15 00:00:00, 2015-04-15 00:00:00

Looking at the above data example, you will be able to see that Stephen is a Teacher, Alex is a Student and Jane is a Student.

This keeps it central, you won't need to write model methods in more than one place, these links have more info on relationships.

CakePHP2: http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html

CakePHP3: http://book.cakephp.org/3.0/en/orm/associations.html

HelloSpeakman
  • 810
  • 9
  • 22