用PHPcake烘焙正确的数据库命名

刚刚进入MVC开发和蛋糕实现,所以我对表命名和id引用感到困惑。</ p>

我有一个名为Assets的表,资产中的值sotered是asset_status_id字段,它是对表的引用Asset_Statuses这个表是资产可能在任何状态下的简单列表 一次(Active,Inactive,Sold,Maintenace等)我选择使用这个列表作为a)我想我需要在某个时候添加更多选项b)我在这个表中有一个'sequence'字段 因此,我可以控制它们在任何下拉中的排序顺序。</ p>

'Cake Bake'似乎看到一个名为Asset_Statuses的表作为一种表示存在于描述之间的关系 资产表和不存在的状态表。 现在我知道你的想法只是称表格状态正确吗? 但我有一个我想使用的'Status'表,例如Domain_Status,当然不会使用相同的状态列表。 </ p>

那么我应该使用什么命名约定来使这一切都很容易?</ p>
</ div>

展开原文

原文

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?

php

2个回答

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.

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
   )
);
duan0802
duan0802 这就是我提到它最重要的原因 - 对于非惯例(因为Elijha说它使用的是AssetStatuses :)
接近 10 年之前 回复
du0204
du0204 还说className是最重要的是不正确的,代码$ belongsTo = array('AssetState'); 假设在APP / models目录(或db中的asset_states表)中有一个asset_state.php模型,只有在使用插件/非约定时才重要的时间才能正常工作
接近 10 年之前 回复
douzhao5656
douzhao5656 真正的模型是单数,这是一个错字。 但是这个人需要的是使用asset_statuses表,因为已经使用了状态表。 事实上这个问题非常混乱所以我和你猜测:)我会改变模型命名(尽管它不需要,因为它是一个例子)。
接近 10 年之前 回复
dongyuan1870
dongyuan1870 特别是在创建新表时,不需要$ useTable。 我会说,当您使用旧数据库构建系统时,它保持最佳状态。 另一个注意事项是所有模型都是单数,而不是你所建议的复数。
接近 10 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问