Zend_DB 和 MVC 模型匹配数据库表

发布于 2024-12-11 10:37:40 字数 2118 浏览 3 评论 0原文

我一直在阅读 Bill Karwin(Zend_DB 的创建者)关于模型与数据库表不直接相关的内容。 (我认为一些开发人员的模型是 Zend_tables 的直接扩展,这使得添加对象的 memcached 缓存变得更加困难,这是有道理的。)

所以 Bill Karwin 所说的是模型 has 表并且 不是一个 表,但我仍然认为我的方式是正确的,因为它是以面向对象的方式设计的。

例如(只是一个例子):

A Monster has 1:M Mouth. a Mouth has 1:M Tooth. 

所以在我的数据库中,我有 5 个表:

Monster: id, name, type
MonsterMouth: id, monster_id, mouth_id
Mouth: id, size
MouthTeeth: id, mouth_id, tooth_id
Tooth: id, size, shape, sharpness

然后是 3 个类:

class Model_Monster { 
    private $id, $name, $type, $mouths = array();
    public function __construct ($id) {
        // Set properties from DB for supplied ID
        // Go through DB and add the mouths based on monster ID
    }

    public function countTeeth () {
        // Loop through each $mouths as $mouth and call $mouth->getTotalTeeth();
    }
} 
class Model_MonsterMouth { 
    private $id, $size, $teeth = array(); 
    public function __construct($id) {
        // Set properties from DB for supplied ID
        // Go through DB and add the types of teeth for this mouth ID
    }

    public function getTotalTeeth () {
        // return sizeof($teeth);
    }

}
class Model_Tooth {
    private $id, $size, $shape, $sharpness;
    public function __construct($id) {
        // Populate details based on ID passed
    }
}

然后我猜想计算牙齿和其他东西的方法......

$monsterId = 1;
$monster = new Monster($monsterId);
// Count total teeth
$totalTeeth = $monster->countTeeth();

所以一个怪物可以有很多不同的嘴,而 1 个嘴可以有很多不同类型的牙齿。

写完这篇冗长的文章后,我认为我的理解是正确的,Bill Karwin 正在谈论那些拥有 5 个模型 而不是 3 的人...

我有 5 个表,但只有 3 个模型,因为有两个表来解决 M:M 表关系。

3 个模型中的 2 个 使用组合来存储许多其他类型的对象。

如果一个怪物有 10 张嘴,大约有 9-10k 颗大约 10 种不同类型的牙齿……这会是性能问题吗?我的意思是 PHP 会认为它是这样的:a,a,a,a,a,b,b5*a 和 2*b。如果拥有 1-100k 个对象并将它们迭代添加到复合项目中的速度很慢,那么我想我应该只出现一次该对象,并带有一个数字属性来说明该类型有多少个。

如果我的答案是正确的,那么也许它可能会帮助其他一些遇到此问题的人。

谢谢:D 多姆

I've been reading something from Bill Karwin (creator of Zend_DB) about models not being directly to do with database tables. (I think some devs have their models a direct extension of Zend_tables or so which makes it harder for adding memcached caching of objects, which makes sense.)

So what Bill Karwin was saying is that a Model has tables and isn't a table but I'm still thinking the way I have it is correct as its designed in an object oriented manner.

For instance (just an example):

A Monster has 1:M Mouth. a Mouth has 1:M Tooth. 

So in my database I'd have 5 tables:

Monster: id, name, type
MonsterMouth: id, monster_id, mouth_id
Mouth: id, size
MouthTeeth: id, mouth_id, tooth_id
Tooth: id, size, shape, sharpness

Then the 3 classes:

class Model_Monster { 
    private $id, $name, $type, $mouths = array();
    public function __construct ($id) {
        // Set properties from DB for supplied ID
        // Go through DB and add the mouths based on monster ID
    }

    public function countTeeth () {
        // Loop through each $mouths as $mouth and call $mouth->getTotalTeeth();
    }
} 
class Model_MonsterMouth { 
    private $id, $size, $teeth = array(); 
    public function __construct($id) {
        // Set properties from DB for supplied ID
        // Go through DB and add the types of teeth for this mouth ID
    }

    public function getTotalTeeth () {
        // return sizeof($teeth);
    }

}
class Model_Tooth {
    private $id, $size, $shape, $sharpness;
    public function __construct($id) {
        // Populate details based on ID passed
    }
}

Then I guess methods for counting teeth and stuff...

$monsterId = 1;
$monster = new Monster($monsterId);
// Count total teeth
$totalTeeth = $monster->countTeeth();

So a monster can have many different mouths and 1 mouth can have many different types of teeth.

After writing out this lengthy post I think I've got it right and that Bill Karwin is talking about those who have 5 Models rather than 3...

I have 5 Tables but only 3 Models as two tables are there to solve M:M table relationships.

2 of the 3 Models use composition to store many of the other type of object.

If a monster had 10 mouths of between 9-10k of about 10 different types of teeth... would this be a performance issue? I mean would PHP see it like: a,a,a,a,a,b,b or 5*a and 2*b. If having 1-100k of an object and iteratively adding them to a composite item is slow then I guess I should only have one occurance of it with a number property to say how many there are of that type.

If I've got it correct then maybe it might help some of the other guys who are having problems with this.

Thanks :D Dom

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

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

发布评论

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

评论(1

放手` 2024-12-18 10:37:40

我不想把话放在蜥蜴比尔的嘴里,我认为他更多地谈论的不是多对多,而是你应该将商业模式的抽象概念与其底层表示分开的事实。

例如,重要的是,不要依赖业务模型中的字段与数据库/文档存储/Web 服务中的字段具有相同名称的事实,因为这会使它变得非常脆弱。

还要考虑一下,如果您的模型想要一个像“FullName”这样的字段,它是“FirstName”和“Surname”的简单串联。您可能真的不想将“FullName”与“FirstName”和“Surname”一起存储在数据库中,因为它是重复的数据并且需要更多工作来保持更新,但如果您的模型与底层表示非常紧密地耦合,您可以需要 a) 将“FullName”添加到您的数据库中,或者 b) 在模型中没有“FullName”的情况下运行。

通过保持清晰的分离,您将能够在模型中包含“FullName”以及生成它的一些代码,而在数据库中只包含“FirstName”和“Surname”。当然,如果您需要的话,它还可以更轻松地切换后端数据库技术。

从我的角度来看,关键的见解是模型不仅仅是它包含的字段,还包括业务规则、验证以及您可以对其执行的潜在操作。该表只是存储支持该行为所需字段的方便位置。

Without wishing to put words in Bill the Lizard's mouth, I think that rather than Many-To-Many-ness, he is more talking about the fact that you should keep separate your abstract concept of a business model from the underlying representation of it.

It is important for example, not to rely on the fact that the fields in your business model have the same name as the fields in your database/document store/web service, because that makes it very brittle.

Consider also, if your model wanted to a field like "FullName", which was a simple concatenation of "FirstName" and "Surname". You might not really want to store the "FullName" alongside "FirstName" and "Surname" in the database, because it's duplicate data and more work to keep updated, but if your model is very tightly coupled to the underlying representation, you would either need to a) add "FullName" to your database or b) live without "FullName" in your model.

By keeping a clean separation, you would be able to have "FullName" in your model and some piece of code that generated it, while just having "FirstName" and "Surname" in the database. Of course, it also makes it much easier to switch out the backend database technology, should you ever need to.

The key insight from my point of view, is that a Model, more than just the fields it contains, is also the business rules, the validation and potentially the actions that you can perform on it. The table is just the handy place to store the fields necessary to underpin that behaviour.

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