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

发布于 2024-09-25 12:51:23 字数 413 浏览 1 评论 0原文

刚刚开始 MVC 开发和蛋糕实现,所以我对表命名和 id 引用感到困惑。

我有一个名为资产的表,资产中存储的值是 asset_status_id 字段,它是对表 Asset_Statuses 的引用,该表是资产在任何时候可能处于的可能状态的简单列表(活动、非活动、已售出、维护等)我选择为此列表使用一个表:a)我想我有时需要添加更多选项b)我在这个表中有一个“序列”字段,这样我可以控制它们的排序顺序出现在任何下拉菜单中。

“Cake Bake”似乎将一个名为 Asset_Statuses 的表视为其存在的指示,用于描述 Assets 表和不存在的 Statuses 表之间的关系。现在我知道你的想法就是把桌子称为雕像吧?但我想使用其他“状态”表,例如 Domain_Status 当然不会使用相同的状态列表。

那么我应该使用什么命名约定来使这一切变得容易呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等风也等你 2024-10-02 12:51:23

使用状态表可能会更好,因为它可以用于所有地方,而不仅仅是资产。这可以轻松地添加模型字段(例如其 id、模型、状态)来完成。

那么你的加入只有一个条件。

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')
       )
    );
}

您可以使用一种行为来自动设置 beforeSave 中的模型字段

作为旁注,所有表都应全部小写,而不是您正在使用的有趣的大小写。这可能是事情没有按应有的方式进行的原因。

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.

岁吢 2024-10-02 12:51:23

我不确定资产状态的复数形式是资产状态(您应该检查),并且表格也需要小写,因此 asset_states,而不是 Asset_States。但您可以在模型中使用完全不同的表。检查这个。所以基本上创建一个模型,根据你的喜好命名。即 AssetState 和 put:

var $useTable = 'asset_states';

我认为你不会有问题。只需确保在您的关系 isn'tateTo, hasMany 中您提供了正确的类名,即:

var $belongsTo = array(
   'AssetState' => array(
      'className' => 'AssetState' //Most important
   )
);

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
   )
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文