tags:

views:

25

answers:

2

Just gettign into MVC developement and cakes implimentation, so I'm getting confused with table naming and id references.

I hava a table called Assets and value sotered in Assets is asset_status_id field which is meant to be a reference to the table Asset_Statuses this table is a simple list of possible statuses the assets could be in at any one time (Active, Inactive, Sold, Maintenace etc) I have chosen to use a table for this list as a) I think I'll need to add more options at some time b) I have a 'sequence' field in this table so I can control the sort order they will apear in in any drop downs.

'Cake Bake'ing seems to see a table called Asset_Statuses as an indication it exists to descibe a relations ship between the Assets table and a non-existent Statuses table. Now I know what your thinking just call the table Statues right? But I have oither 'Status' tables I'd like to use such as a Domain_Status which wont use the same status lists of course.

So what s the naming convention I should be using to make this all work easily?

A: 

I am not sure that the plural of Asset Status is Asset States (you should check) and also the table need to be lower case, so asset_states, not Asset_States. But you can use a totally different table in your models. Check this. So basically create a model, name it as you like. I.e. AssetState and put:

var $useTable = 'asset_states';

and I think you won't have problems. Just be sure that in your relation belongsTo, hasMany you provide the proper class name i.e.:

var $belongsTo = array(
   'AssetState' => array(
      'className' => 'AssetState' //Most important
   )
);
Nik
$useTable is not needed especially when creating a new table. i would say its best kept for the times when you are building a system using a legacy database. Another note is that all models are Singular, not plural as you have suggested.
dogmatic69
True models are singular, it's a typo. But what the guy needed is to use asset_statuses table because status table is already used. In fact the question is really messy so both me and you are guessing :) I will change the model naming anyway (although it's not needed, because it's an example).
Nik
also saying className is the most important is incorrect, the code $belongsTo =array('AssetState'); would work just the same, assuming there is a asset_state.php model in the APP/models dir (or asset_states table in the db) only time that is important is when using plugins / non-conventions
dogmatic69
That's why I mentioned it's most important - for non-conventions (because Elijha said it's using AssetStatuses :)
Nik
A: 

you might just be better off using a statuses table, as it can be used all over and not just for the assets. this can be done easily adding a model field, so its id, model, status.

then your join just has a condition.

class Asset extends AppModel{
    var $belongsTo = array(
       'Status' => array(
          'className' => 'Status' // not needed with the proper conventions and not in a plugin,
          'conditions' => array('Status.model' => 'Asset')
       )
    );
}

you can use a behavior to automate the setting of the model field in beforeSave

As a side note, all tables should be all lower case, not the funny case you are using. This may be the cause of things not working the way they should.

dogmatic69