继承 Cakephp 示例

发布于 2024-11-09 11:26:08 字数 193 浏览 0 评论 0原文

我计划使用 Cakephp 制作一个网站,“要求”之一是在数据库中使用继承。

父实体假装具有共同属性,但最重要的是 id(关键属性) 它被传递给它的子实体,这些子实体当然有自己的一组属性。

Cakephp支持这个功能吗?如果确实如此,有人可以提供一个使用继承的简单示例吗? (不是多重继承顺便说一句)

我会很感激你的回答。

Im planning to make a website using Cakephp, and one of the 'requerimients' is using inheritance in the database.

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.

Does Cakephp support this feature? If it does, can anybody provide an easy example of using inheritance? (not multiple inheritance btw)

I'll appreciate your answers.

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

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

发布评论

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

评论(5

羁客 2024-11-16 11:26:08

虽然我不认为它直接支持它,但你可以使用一些 Set 类魔法来做到这一点。周围最棒的课程之一。

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

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

如果它是非常简单的东西,你正在做的事情, array_merge() 也会为你做这件事

你可以轻松地创建一个在 afterFind() 回调中执行此操作的行为,以便它在一个地方透明地完成,并且只需执行正常的 $this 就可以为你的视图做好准备->模型->查找('all')等

while I don't think it supports it directly you can do that with some Set class magic. one of the most awesome classes around.

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

and

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

if it is quite simple stuff you are doing, array_merge() will do it for you too

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

╭ゆ眷念 2024-11-16 11:26:08

您需要通过 CakePHP 的内置关系映射 (hasOne) 来实现此行为:

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

然后您可以修改 AppModel 类并实现afterFind 方法(适用于所有模型)并处理/合并数据。

You'll need to implement this behavior via CakePHP's built-in relational mapping (hasOne):

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.

热情消退 2024-11-16 11:26:08

您可以考虑使用 Doctrine 作为 ORM,它支持通过列聚合进行继承:
http://dqminh.com/integrate-doctrine-orm-with-cakephp -第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

梦魇绽荼蘼 2024-11-16 11:26:08

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

这是前段时间写的行为 - 它可能很旧,但是它应该为您理解如何使用 Cake 管理 STI(单表继承)奠定良好的基础。

** 编辑 **

来自我链接的文章 - 这可能不是当前或工作代码。我提到它是您制定自己的行为准则的起点。

该类型是由类类型推断的 - 您所讨论的结构仅适用于设置的单表继承类型。绝对确保您正在导入父类,如示例所示。 IE:

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

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.

** 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. Make absolutely sure you are doing the import of the parent class like the examples show. IE:

<?php
    ...
    App::import('Model', 'Person');
    class Manager extends Person
    { 
        ...
    }
?>
预谋 2024-11-16 11:26:08

我是 CakePHP 的新手(一周的经验),但遇到了同样的问题。正如这里已经暗示的那样,组合似乎是实现模型扩展的最传统方式(正如您在评论中提到的使用 Has-A 而不是 Is-A)。

这是我正在尝试并希望适用于我相对简单的一组用例的方法:

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

我希望这接近于需要子类所做的事情。当您明确请求 SharedLogs 时,您将有权访问 Log 属性。如果您从日志的角度发出请求,您将无法访问任何 SharedLog 扩展属性(但这很有意义,因为如果您正在浏览日志集合,您将不知道哪些是超类,哪些是超类)是子类)。

我认为验证变量足以防止数据库中出现孤儿和重复项。

说到数据库,我竭尽全力使shared_logs表没有id列,而是使用log_id作为其主键。我很想获得有关此设置的一些反馈,因为我对这个框架完全陌生,并且没有第六感来判断代码在这种情况下“只是感觉错误”。

编辑:我只是想假设我没有完全以错误的方式处理这个问题(而且我想认为我没有)这个解决方案提供了一种非常直接的方法来将超类的实例变成子类的实例。由于日志只有在创建后才能共享。这可能只是强调了我应该从一开始就考虑构图的观点。

I'm new to CakePHP (as in one week's worth of experience) but have run into the same issue. 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).

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. 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).

I think the validate variable is enough to prevent orphans and duplicates in the database.

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. 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. 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.

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