简体   繁体   English

示例 Inheritance Cakephp

[英]Example Inheritance Cakephp

Im planning to make a website using Cakephp, and one of the 'requerimients' is using inheritance in the database.我计划使用 Cakephp 创建一个网站,其中一个“要求”是在数据库中使用 inheritance。

The parent Entity pretends to have common properties but most important an id (key attribute) that is passed to its subentities, which of course have their own set of attributes.父实体假装具有共同的属性,但最重要的是传递给其子实体的 id(关键属性),这些子实体当然有自己的一组属性。

Does Cakephp support this feature? Cakephp 是否支持此功能? If it does, can anybody provide an easy example of using inheritance?如果是这样,任何人都可以提供一个使用 inheritance 的简单示例吗? (not multiple inheritance btw) (不是多个 inheritance 顺便说一句)

I'll appreciate your answers.我会很感激你的回答。

while I don't think it supports it directly you can do that with some Set class magic.虽然我认为它不直接支持它,但您可以使用一些 Set class 魔法来做到这一点。 one of the most awesome classes around.周围最棒的课程之一。

http://book.cakephp.org/view/1487/Set http://book.cakephp.org/view/1487/Set

and

http://book.cakephp.org/view/1508/merge http://book.cakephp.org/view/1508/merge

if it is quite simple stuff you are doing, array_merge() will do it for you too如果你正在做的事情很简单,array_merge() 也会为你做

You would easily be able to create a behavior that does this in the afterFind() callback so that its transparently done in one place and all ready for your view just doing a normal $this->Model->find('all') etc您将能够轻松地在 afterFind() 回调中创建一个执行此操作的行为,以便它在一个地方透明地完成,并为您的视图做好准备,只需执行正常的 $this->Model->find('all') 等

You'll need to implement this behavior via CakePHP's built-in relational mapping (hasOne):您需要通过 CakePHP 的内置关系映射 (hasOne) 来实现此行为:

http://book.cakephp.org/#!/view/1039/Associations-Linking-Models-Together http://book.cakephp.org/#!/view/1039/Associations-Linking-Models-Together

You can then modify the AppModel class and implement an afterFind method (to work with all models) and massage/merge the data.然后,您可以修改 AppModel class 并实现 afterFind 方法(适用于所有模型)并按摩/合并数据。

You might consider using Doctrine as an ORM, which supports inheritance through column aggregation: http://dqminh.com/integrate-doctrine-orm-with-cakephp-part-1 You might consider using Doctrine as an ORM, which supports inheritance through column aggregation: http://dqminh.com/integrate-doctrine-orm-with-cakephp-part-1

http://bakery.cakephp.org/articles/taylor.luk/2008/10/22/inheritable-behavior-missing-link-of-cake-model http://bakery.cakephp.org/articles/taylor.luk/2008/10/22/inheritable-behavior-missing-link-of-cake-model

This is a behavior written some time ago - it might be old but it should give you a very good foundation for understanding how to manage STI (single table inheritance) using Cake.这是前段时间写的一个行为——它可能已经过时了,但它应该为您理解如何使用 Cake 管理 STI(单表继承)打下一个很好的基础。

** EDITS ** ** 编辑 **

From the article I linked - which might not be current or working code.从我链接的文章中 - 这可能不是当前或工作代码。 I mentioned it as a starting point for you to work out your own behavior code.我提到它是您制定自己的行为代码的起点。

The type is inferred by the class type - and the structure you are talking about only applies to the single table inheritance type of setup.该类型由 class 类型推断 - 您所说的结构仅适用于单表 inheritance 类型的设置。 Make absolutely sure you are doing the import of the parent class like the examples show.确保您正在导入父 class,如示例所示。 IE: IE:

<?php
    ...
    App::import('Model', 'Person');
    class Manager extends Person
    { 
        ...
    }
?>

I'm new to CakePHP (as in one week's worth of experience) but have run into the same issue.我是 CakePHP 的新手(就像一周的经验一样),但遇到了同样的问题。 As has been hinted at already here, it seems that composition is the most conventional way of getting toward Model extension (as you mentioned in a comment using Has-A instead of Is-A).正如这里已经暗示的那样,组合似乎是获得 Model 扩展的最传统方式(正如您在使用 Has-A 而不是 Is-A 的评论中提到的)。

Here's what I'm trying and hoping works for my relatively simple set of use cases:这是我正在尝试并希望适用于我相对简单的一组用例的方法:

//'Super Class'
class Log extends AppModel {
     var $name = 'Log';    
}

//'Sub Class'
class SharedLog extends AppModel { 
    var $name = 'SharedLog';
    var $hasOne = 'Log';
    var $validate = array(
        'log_id' => array(
            'logRule-1' => array(
                'rule' => 'notEmpty',  
            ),
            'logRule-2' => array(
                'rule' => 'isUnique',  
            )  
        )
    );
}

I'm hoping this get's close to what one would need a subclass to do.我希望这接近于需要一个子类来做的事情。 When you explicitly ask for SharedLogs you'll have access to the Log attributes.当您明确要求 SharedLogs 时,您将有权访问 Log 属性。 If you make the request from the perspective of a Log you won't have access to any SharedLog extended attributes (but that makes sense because if you're going through a collection of Logs you wouldn't know which are the super class and which are the sub class).如果您从 Log 的角度提出请求,您将无法访问任何 SharedLog 扩展属性(但这是有道理的,因为如果您正在查看 Logs 集合,您将不知道哪些是超级 class 和哪些是子类)。

I think the validate variable is enough to prevent orphans and duplicates in the database.我认为 validate 变量足以防止数据库中的孤儿和重复。

Speaking of the database, I went out on a limb made it so that the shared_logs table doesn't have an id column and instead uses log_id as its primary key.说到数据库,我做了一个肢体,使 shared_logs 表没有 id 列,而是使用 log_id 作为其主键。 I'd love to get some feedback on this setup because I'm completely new to the framework and don't have that sixth sense for when code "just feels wrong" in this context.我很想获得有关此设置的一些反馈,因为我对框架完全陌生,并且在这种情况下代码“感觉不对”时没有第六感。

EDIT: I was just thinking that assuming I'm not completely going about this the wrong way (and I'd like to think I'm not) this solution offers a pretty straight forward way to make an instance of the super class into an instance of the subclass.编辑:我只是想假设我没有完全以错误的方式解决这个问题(我想我不是)这个解决方案提供了一种非常直接的方法来将超级 class 的实例变成子类的实例。 Since a Log may not be shared until after it is created.因为在创建日志之前可能不会共享日志。 This might just make the belabored point that I should have been thinking composition from the beginning.这可能只是说明我应该从一开始就考虑作文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM