0

是否可以使用 Cakephp 中的行为来设置基本变量之一?我希望每个客户有一张不同的桌子。

我想把它写在 setup 函数中,但大多数变量都是空的。我在 app_controller.php 的 beforeFind() 中创建了 $this->subdomainid,我显然不能在模型中使用它。

但我想知道,我可以在行为中选择 useTable 吗?如果我检查 PHP 中的主机名并以此为基础创建子域,我可以使用 subdomain_tablename 作为表吗?我想为此使用 subdomain_recipes、subdomain_ingredients、client_ 等表的命名约定,然后使用正常的命名约定。

目前我只有 3 个单独的子域,但我绝对不想在来自不同子域的表中混合数据。几天来我一直在考虑一个解决方案,欢迎大家一起思考!

4

1 回答 1

1

if you are talking about different tables in the same database you can do something like this:

if you want to change the table in every model with the same prefix

in the app_Model

function __construct($id = false, $table = null, $ds = null) {
        $prefix = getPrefix();
    $this->useTable = $prefix.$useTable;
    parent::__construct($id,$table,$ds);
}

Like this it wiil change every model table before using it.... if you want only the model client to be modified just put this same function in the client model, if you want a couple do the same in this models. If you want all except one do it in app_model like this

function __construct($id = false, $table = null, $ds = null) {
    $prefix = getPrefix();
    if ($this->name != 'Client') 
        $this->useTable = $prefix.$useTable;
    parent::__construct($id,$table,$ds);
}

this is not a behavior though but it solves your problem :S if you want to do it in a bahaviour you will need to do something similar using beforeFind, beforeSave, etc (all the befores doing something in the database) or do it when loading a model (in a controller it will be like this $this->Client->useTable = $prefix.$this->Client->useTable;)

Remember that getPrefix() function is the function that you will have that tells you which prefix to use.

I think you have to always write the var $useTable = 'table'; for this to work.

If you want different databases try this solution post in the bakery

于 2011-09-15T13:21:37.043 回答