开发者

Cakephp behavior that selects useTable?

Is it possible to use a behavior in Cakephp to set one of the basic variables? I want a different table per client.

I was thinking of writing it in the setup function, but most variables are completely empty. I create $this->subdomainid in a beforeFind() in app_controller.php, which I obviously cannot use in the model.

But I wonder, can I choose useTable in the behavior at all? If I check the hostname in PHP and base the sub开发者_如何学运维domain on that, can I use subdomain_tablename as tables? I want to use for this a naming-convention for tables like subdomain_recipes, subdomain_ingredients, so client_ and then the normal naming convention.

I have only 3 seperate subdomains at the moment, but I absolutely don't want to mix data in tables from different subdomains. I have been thinking about a solution for a few days and some thinking along would be welcome!


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜