在 CAKEPHP 中,我们可以动态更改链接到特定模型的表吗?

发布于 2024-10-11 17:03:23 字数 1376 浏览 6 评论 0原文

假设我有 2 个具有相同结构的相同表(称为“tableA”和“tableB”)。

我想将某些数据保存在表“A”上,将某些数据保存在表“B”上。

现在我想对两个表使用相同的模型。

我想更改与模型(例如“ModelM”)链接的表,以根据控制器的条件动态更改。

例如


在控制器中:- //示例代码

function saveProduct(){

    $this->loadModel('ModelM');

    if(condition){

        $this->ModelM->useTable = 'A';

    }else{

        $this->ModelM->useTable = 'B';

     }
     $this->ModelM->save($this->data);

}

2011 年 1 月 14 日添加

以下是我正在处理的代码的复制/粘贴:

function experiment(){

    $tableName = 'temp_table'.'1234';

    mysql_query('CREATE TABLE '.$tableName.' LIKE temp_home_masters');

    $sql = $this->createInsertQuery($new_arr,$tableName);

    $status = mysql_query($sql);

    if($status){
        echo "saved successfully";
    }else{
        echo "error";
    }

    $this->NewHomeMaster->setSource($tableName);//NewHomeMaster was previously attached to a different table , here I want to change the tableName the model linked with dynamically.Model 'NewHomeMaster' already exists and uses a table ...Here I am willing to link this model to the newly created tempory table.//

    $home_details=$this->paginate('NewHomeMaster',array($new_conditions));

    mysql_query('DROP table '.$tableName);

}

不幸的是,这不起作用...

Suppose I have 2 identical table having same structure(Call it 'tableA' & 'tableB').

I want to save certain data on table 'A' and certain data on table 'B'.

NOW I want to use the same MODEL for both the table.

I want to change the table linked with the Model(say 'ModelM') to change dynamically based on condition at the controller.

e.g.


In controller:- //sample code

function saveProduct(){

    $this->loadModel('ModelM');

    if(condition){

        $this->ModelM->useTable = 'A';

    }else{

        $this->ModelM->useTable = 'B';

     }
     $this->ModelM->save($this->data);

}

ADDITION ON 14th JANUARY 2011

Following is the copy/paste of code I am working on:

function experiment(){

    $tableName = 'temp_table'.'1234';

    mysql_query('CREATE TABLE '.$tableName.' LIKE temp_home_masters');

    $sql = $this->createInsertQuery($new_arr,$tableName);

    $status = mysql_query($sql);

    if($status){
        echo "saved successfully";
    }else{
        echo "error";
    }

    $this->NewHomeMaster->setSource($tableName);//NewHomeMaster was previously attached to a different table , here I want to change the tableName the model linked with dynamically.Model 'NewHomeMaster' already exists and uses a table ...Here I am willing to link this model to the newly created tempory table.//

    $home_details=$this->paginate('NewHomeMaster',array($new_conditions));

    mysql_query('DROP table '.$tableName);

}

UNFORTUNATELY THIS DOES NOT WORK...

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

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

发布评论

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

评论(3

时光礼记 2024-10-18 17:03:23

我本来是想解决你的问题,但越想越觉得你的逻辑有问题。

我可以看到这些表相似或相同的事实如何导致您决定使用单个模型与两个表进行交互。然而,当您查看模型应该是什么时(在 CakePHP 中基本上是表的接口),来回切换是没有意义的。

CakePHP 文档解释了这样的模型:

在面向对象编程中,数据模型是代表“事物”的对象,例如汽车、人或房屋。

在您的示例中,您确实有两个看起来完全相同的独立“事物”。因此,他们应该有自己的模式。

如果您的模型确实将具有完全相同的方法,那么“CakePHP 方式”将定义一个自定义封装共享方法的行为。然后将行为附加到两个模型。

然后就可以在Controller条件下加载你需要的模型了:

private $DynamicModel;

public function saveProduct() {
    if (condition) {
        App::import('Model', 'ModelZ');
        $this->DynamicModel = new ModelZ;
    } else {
        App::import('Model', 'ModelY');
        $this->DynamicModel = new ModelY;
    }
    $this->DynamicModel->save($this->data);
}

I originally set out to figure a solution to your problem, but the more I think about it, I believe your logic is flawed.

I can see how the fact that the tables are similar or identical can lead you to the decision of using a single model to interact with both tables. However, when you look at a what a model is supposed to be (In CakePHP basically an interface to a table), it doesn't make sense to switch back and forth.

The CakePHP docs explain models like this:

In object-oriented programming a data model is an object that represents a "thing", like a car, a person, or a house.

In your example, you really have two separate "things" that look exactly the same. Therefore, they should have their own models.

If your models are really going to have the exact same methods, then "the CakePHP Way" would be to define a custom Behavior that encapsulates your shared methods. Then attach the behavior to both models.

Then you can load the model you need in the Controller condition:

private $DynamicModel;

public function saveProduct() {
    if (condition) {
        App::import('Model', 'ModelZ');
        $this->DynamicModel = new ModelZ;
    } else {
        App::import('Model', 'ModelY');
        $this->DynamicModel = new ModelY;
    }
    $this->DynamicModel->save($this->data);
}
梅倚清风 2024-10-18 17:03:23

在过滤器函数之前在控制器上执行此操作:

$this->CakePHPModelName->setSource('table_name');

这将使用不同的表。

资料来源:http://www.mainelydesign.com/blog /view/change-cakephps-model-usetable-on-fly

Do this on your controllers before filter function:

$this->CakePHPModelName->setSource('table_name');

This will use different table.

Source: http://www.mainelydesign.com/blog/view/changing-cakephps-model-usetable-on-fly

唱一曲作罢 2024-10-18 17:03:23

正如 Stephen 所描述的,情况很棘手,因为您的方法在某种程度上违反了 MVC 约定。

然而,如果您愿意尝试自定义 hack 的黑暗面,您可以考虑在 CakePHP 中创建您自己的自定义数据源来为您处理此类逻辑。一种选择是使用您自己的自定义逻辑来扩展给定的数据源(可能是 MySQL 数据源),该逻辑旨在在与数据库交互之前执行一些初步过滤/调节。不是那么干净,因为逻辑被放置在错误的范围内,但可以工作。从这里开始: http://book.cakephp.org/view/1075/DataSources或者

,您可以创建两个不同的模型并使用行为使它们共享相同的逻辑。这有点限制您在流程的早期选择模型(因此不仅影响数据存储的位置),但可能是一种可能性。

The situation is tricky, as Stephen describes it, because your approach somewhat violates the MVC conventions.

However, if you're willing to go to the dark side of custom hacks, you could consider creating you own customized Datasource in CakePHP that handles this kind of logic for you. An option is to extend a given Datasource (presumably the MySQL one) with you own custom logic that aims to perform some prelimary filtering/conditioning before interacting with the database. Not that clean because the logic is placed in the wrong scope, but could work. Have a look here for a start: http://book.cakephp.org/view/1075/DataSources

Alternatively, you could create two different models and make them share the same logic using a behavior. This kinda limits you to take the choice of model earlier in the flow (and thus doesn't only affect the location of data storage), but might be a possibility.

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