简体   繁体   English

模型可以在CakePHP中有多个表吗?

[英]Can a model have multiple tables in CakePHP?

模型可以在CakePHP中有多个表吗?

Guessing from this sentence in the manual: 从手册中的这句话猜测:

A model is generally an access point to the database, and more specifically, to a certain table in the database. 模型通常是数据库的访问点,更具体地说,是数据库中的某个表。 By default, each model uses the table who's name is plural of its own, ie a 'User' model uses the 'users' table. 默认情况下,每个模型使用其名称为多个的表,即“用户”模型使用“用户”表。

I don't think so, but you can make relationships maybe thats what you need. 我不这么认为,但你可以建立关系也许就是你所需要的。

Check this 检查一下

Sure it can, it is convenient when you have a lot identical tables. 当然可以,当你有很多相同的表时很方便。

class SomeModel extends Model
{
    var $useTable = false;

    public function ReadData()
    {
        // select table
        if($a == 1)
            $this->setSource('tableName1');
        else if($a == 2)
            $this->setSource('tableName2');
        // ...
        else if($a == N)
            $this->setSource('tableNameN');

        // now perform operations with selected table
        return $this->find('all');
    }
}

Technically, based on the way you're asking the question, not that I know of. 从技术上讲,根据你提问的方式,而不是我所知道的。 Often, though, I'll use relationships to something that might be similar to what you're after. 但是,通常情况下,我会将关系用于可能与您所追求的相似的事物。 For example, a person has address information that can be dropped in the people table easily enough, but I usually prefer to pull that out because other entities can also have addresses (a business, etc.). 例如,一个人可以很容易地在人员表中删除地址信息,但我通常更愿意将其拉出来,因为其他实体也可以拥有地址(业务等)。

Same idea if you want to implement some kind of pseudo-inheritance model in your DB. 如果要在数据库中实现某种伪继承模型,也一样。 For example, volunteers are people, but so are contractors, vendors and employees. 例如,志愿者是人,但承包商,供应商和员工也是如此。 The all share certain properties that you might want to store in a people table and others that are unique to the type of person they are. all共享您可能希望存储在人员表中的某些属性以及其他人类所特有的属性。

In each case you have two models, but they work together seamlessly via their associations. 在每种情况下,您都有两个模型,但它们通过它们的关联无缝地协同工作。

If that's the kind of scenario you're thinking about then a similar approach might work for you although it's not about a model having multiple tables. 如果那是您正在考虑的那种情况,那么类似的方法可能对您有用,尽管它不是关于具有多个表的模型。

I guess you want to implement some sort of inheritance in the database (that requires to join the data stored in the parent table when retrieving information from the child table). 我想你想在数据库中实现某种继承(这需要在从子表中检索信息时加入存储在父表中的数据)。 My approach to solve this was using afterFind callback in the child model. 我解决这个问题的方法是在子模型中使用afterFind回调。 I redefined the callback as follows: 我重新定义了回调如下:

function afterFind($results) {
    foreach ($results as $key => $val) {
                $fieldRetrieved=$this->query("SELECT *field* FROM *parent_table* WHERE id={$val['*ChildModelName*']['id']}");
                $results[$key]['*ChildModelName*']['*field*']=$fieldRetrieved[0]['*parent_table*']['*field*'];
    }
    return $results;
}

In this way I was including the field/s from the parent table to the results obtained from the child table. 通过这种方式,我将父表中的字段/ s包含在从子表中获得的结果中。 In this case I've assumed that both tables are index with a field called id and that field, in the child table, is also a foreign key to the parent table (thus creating the pseudo-inheritance). 在这种情况下,我假设两个表都是带有一个名为id的字段的索引,并且子表中的该字段也是父表的外键(从而创建伪继承)。

It can't have multiple tables at the same time..., but you might be able to modify the Model::useTable property to switch the model's table to a different one. 它不能同时具有多个表...,但您可以修改Model :: useTable属性以将模型的表切换到另一个表。 Give it a whirl and let us know if it works. 给它一个旋转,让我们知道它是否有效。

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

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