Take the 2-minute tour ×

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.


class BillingsController extends AppController 

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


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 ?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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 

share|improve this answer
Great tip... How cakephp is inserting the data correctly to users table ? Is it because of useTable variable ? My all form fields are like data[Billing][name].. –  binoy Sep 25 '12 at 10:28
yes, useTable variable tells cake wchich table should be used for a model, prefix 'Billing' is in application layer and is translated/maped to defined table before cake saved the data –  Grzegorz Motyl Sep 25 '12 at 10:33

Your Answer


