CakePHP常见模型

发布于 2024-10-08 08:08:24 字数 410 浏览 4 评论 0原文

我有 3-4 个应用程序运行相同的 Cake 库,每个应用程序都有自己的控制器、模型和视图。

我最近发现模型中的很多方法在项目之间重叠,我想知道是否可以让应用程序模型(而不是 app_model.php)来扩展蛋糕文件中的模型,

例如

app1
    controllers
    models
        model_1.php
        model_2.php
    views
app2
    controllers
    models
        model_1.php
        model_3.php
    views
cake
    controllers
    models
        model_1.php
    views

我希望这是有道理的

I have 3-4 apps running of the same Cake library, each app has its own controllers, models and views.

I have found recently that quite a few methods within the models overlap between projects, i was wondering if it was possible to get the app models (not app_model.php) to extend the models in the cake files

e.g.

app1
    controllers
    models
        model_1.php
        model_2.php
    views
app2
    controllers
    models
        model_1.php
        model_3.php
    views
cake
    controllers
    models
        model_1.php
    views

I hope that makes sense

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

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

发布评论

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

评论(4

羁拥 2024-10-15 08:08:24

您实际上不应该修改 CakePHP 应用程序来从另一个 CakePHP 应用程序读取代码。您的所有应用程序代码都应该封装在该应用程序中。

如果您发现自己在多个应用程序中具有相同的代码,那么这听起来像是将代码提取到 CakePHP 插件中的完美候选者!

查看用于创建插件的CakePHP 文档

插件将拥有自己的控制器、模型、视图,现在(从 CakePHP 1.3 开始)它拥有自己的资产(CSS、Js、图像等)。您可以将该插件添加到您的应用程序中并修改您的路由和链接,以便最终用户注意到您的应用程序中没有任何差异。

Router::connect('/your/routes/*', array('plugin' => 'your_plugin', 'controller' => 'controller_in_plugin'));

echo $this->Html->link('Your Link', array('plugin' => 'your_plugin', 'controller' => 'controller_in_plugin', 'action' => 'action_in_plugins_controller'));

You shouldn't really be modifying your CakePHP applications to read code from another CakePHP application. All of your apps code should be encapsulated within that application.

If you find yourself having the same code across multiple applications, this sounds like a perfect candidate for extracting the code out into a CakePHP plugin!

Check out the CakePHP documentation for creating plugins.

A plugin will have it's own controllers, models, view, and now (as of CakePHP 1.3) it's own assets (CSS, Js, Images, etc). You can add the plugin to your application and modify your routing and links so that the end user notices no difference in your application.

Router::connect('/your/routes/*', array('plugin' => 'your_plugin', 'controller' => 'controller_in_plugin'));

echo $this->Html->link('Your Link', array('plugin' => 'your_plugin', 'controller' => 'controller_in_plugin', 'action' => 'action_in_plugins_controller'));
碍人泪离人颜 2024-10-15 08:08:24

如果您使用的是 CakePHP 1.3,那么您可以修改 bootstrap.php 文件:

App::build(array(
    ...
    'models' =>  array('/var/www/app1/models/', '/var/www/cake/models/'),
    ...
));

但我认为您必须以不同的方式重命名它们,即您不能在两个地方都有 model_1.php。但是你可以在 cake/models 文件夹中包含 vehicle.php,在 cars_app/models 文件夹中包含 car.php 以及bike.php 位于 bikes_app/models 文件夹中,其中包含扩展车辆模型的模型。

我还没有测试过,但试试吧!

If you're using CakePHP 1.3, then you can modify your bootstrap.php file:

App::build(array(
    ...
    'models' =>  array('/var/www/app1/models/', '/var/www/cake/models/'),
    ...
));

But I think you'd have to rename them differently, i.e. you can't have model_1.php in both places. But you could have vehicle.php in your cake/models folder, car.php in your cars_app/models folder & bike.php in your bikes_app/models folder with the models extending the Vehicle model.

I haven't tested it, but give it a shot!

荒芜了季节 2024-10-15 08:08:24

您可以尝试对常见模型使用符号链接。
这样,即使目录中有多个文件,它们也共享相同的根实现。

You could try using symbolic link for the common models.
This way even though there are multiple files in the directories, they all share the same root implementation.

痴情 2024-10-15 08:08:24

正如 RabidFire 所提到的,您首先必须指定一个额外的模型路径。然后,要从这样的模型继承,您必须将相应的模型与子模型一起导入文件中:

App::import('Model', 'ParentModel');

class ChildModel extends ParentModel {
    ...
}

As mentioned by RabidFire, you first have to specify an additional model path. Then, to inherit from such a model, you have to import the respective model in the file with the child model:

App::import('Model', 'ParentModel');

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