binoy
binoy

Reputation: 1768

Cakephp different model and controller with same table

May be a stupid question.

Since my controller and model is too big (also I don't want to mess up with old modules), I want to create a new controller and model and I want to separate the code from existing..That is, I want to move some code from users controller and user model to billing controller and billing model (there is no billings table, all info is storing is users table, so I given useTable='users'). I don't want to execute any of the code written in user model, while executing the billings controller, only BillingModel code needs to be executed.

BusinessController

class BillingsController extends AppController 
{

    function add()
    {
        if (!empty($this->request->data)) 
        {
            $this->Billing->create();
            if ($this->Billing->save($this->request->data)) 
            {
                echo 'Saved'; // Message, redirect etc
            }
        }
    }
}

BillingModel

class Billing extends AppModel {


    public $useTable = 'users';

        public $hasOne = array('Profile');

public $validate = array(
        'email' => array(
            'email' => array(
                'rule' => 'email',
                'message' => 'Please provide a valid email address.',
                'last' => true,
            ),
                 'first_name' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Please enter your name',
            ),
                    )
        );

}

All Form fields will have data[Business] as prefix.

The data is saving correctly into the database. I just want to know, anything wrong with this approach ?

Upvotes: 1

Views: 625

Answers (1)

Greg Motyl
Greg Motyl

Reputation: 2545

In this case, as I understand you are duplicating some code in both models (validation, relations ect.) In my opinion better solution would be to extend Users model with Billing

class Billing extends Users {

}

the same situation is with controller:

class BillingsController extends UsersController 
{

}

Upvotes: 3

Related Questions