CakePHP 在运行时更改虚拟字段

发布于 2024-10-19 01:00:06 字数 1243 浏览 2 评论 0原文

我有一个多站点应用程序的产品模型。

根据域(站点)我想加载不同的数据。

例如,我的数据库中没有 namedescription 字段,而是有 posh_name、cheap_name、posh_description 和 Cheap_description。

如果我设置如下:

class Product extends AppModel 
{
    var $virtualFields = array(
        'name' => 'posh_name',
        'description' => 'posh_description'
    );
}

那么它总是有效,无论是直接从模型访问还是通过关联访问。

但我需要虚拟字段根据域的不同而不同。首先,我创建 2 组:

var $poshVirtualFields = array(
    'name' => 'posh_name',
    'description' => 'posh_description'
);

var $cheapVirtualFields = array(
    'name' => 'cheap_name',
    'description' => 'cheap_description'
);

这是我的 2 组,但如何根据域分配正确的一组?我确实有一个名为 isCheap() 的全局函数,它可以让我知道我是否位于低端域。

所以我尝试了这个:

var $virtualFields = isCheap() ? $this->cheapVirtualFields : $this->poshVirtualFields;

这给了我一个错误。显然你不能像这样在类定义中分配变量。

因此,我将其放入我的产品模型中:

function beforeFind($queryData)
{
    $this->virtualFields = isCheap() ? $this->cheapVirtualFields : $this->poshVirtualFields;

    return $queryData;
}

仅当直接从模型访问数据时才有效,当通过模型关联访问数据时则不起作用。

必须有一种方法可以让它正常工作。如何?

I have a Product model for a multi site application.

Depending on the domain(site) I want to load different data.

For example instead of having a name and description fields in my database I have posh_name, cheap_name, posh_description, and cheap_description.

if I set something up like this:

class Product extends AppModel 
{
    var $virtualFields = array(
        'name' => 'posh_name',
        'description' => 'posh_description'
    );
}

Then it always works, whether accessed directly from the model or via association.

But I need the virtual fields to be different depending on the domain. So first I creating my 2 sets:

var $poshVirtualFields = array(
    'name' => 'posh_name',
    'description' => 'posh_description'
);

var $cheapVirtualFields = array(
    'name' => 'cheap_name',
    'description' => 'cheap_description'
);

So these are my 2 sets, but how do I assign the correct one based on domain? I do have a global function called isCheap() that lets me know if I am on the lower end domain or not.

so I tried this:

var $virtualFields = isCheap() ? $this->cheapVirtualFields : $this->poshVirtualFields;

This gives me an error. Apparently you cannot assign variables in a Class definition like this.

So I put this in my Product model instead:

function beforeFind($queryData)
{
    $this->virtualFields = isCheap() ? $this->cheapVirtualFields : $this->poshVirtualFields;

    return $queryData;
}

This works ONLY when the data is accessed directly from the model, DOES NOT work when the data is accessed via model association.

There has got to be a way to get this to work right. How?

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

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

发布评论

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

评论(2

不必在意 2024-10-26 01:00:06

好吧,如果我将它放在构造函数中而不是 beforeFind 回调中,它似乎可以工作:

class Product extends AppModel 
{
    var $poshVirtualFields = array(
        'name' => 'posh_name',
        'description' => 'posh_description'
    );

    var $cheapVirtualFields = array(
        'name' => 'cheap_name',
        'description' => 'cheap_description'
    );

    function  __construct($id = false, $table = null, $ds = null) {
        parent::__construct($id, $table, $ds);
        $this->virtualFields = isCheap() ? $this->cheapVirtualFields : $this->poshVirtualFields;
    }
}

但是,我不确定这是否是 CakePHP 否否那能回来咬我吗?

Well if I put it in the constructor instead of the beforeFind callback it seems to work:

class Product extends AppModel 
{
    var $poshVirtualFields = array(
        'name' => 'posh_name',
        'description' => 'posh_description'
    );

    var $cheapVirtualFields = array(
        'name' => 'cheap_name',
        'description' => 'cheap_description'
    );

    function  __construct($id = false, $table = null, $ds = null) {
        parent::__construct($id, $table, $ds);
        $this->virtualFields = isCheap() ? $this->cheapVirtualFields : $this->poshVirtualFields;
    }
}

However, I am not sure if this is a CakePHP no no that can come back to bite me?

鱼窥荷 2024-10-26 01:00:06

似乎问题可能在于模型关联是一个动态构建的模型。例如 AppModel

尝试执行 pr(get_class($this->Relation));在代码中查看输出是什么,它应该是您的模型名称而不是 AppModel。

还尝试使用:

var $poshVirtualFields = array(
    'name' => 'Model.posh_name',
    'description' => 'Model.posh_description'
);

var $cheapVirtualFields = array(
    'name' => 'Model.cheap_name',
    'description' => 'Model.cheap_description'
);

seems like the issue could be that the model association is a model that is built on the fly. eg AppModel

try and do pr(get_class($this->Relation)); in the code and see what the output is, it should be your models name and not AppModel.

also try and use:

var $poshVirtualFields = array(
    'name' => 'Model.posh_name',
    'description' => 'Model.posh_description'
);

var $cheapVirtualFields = array(
    'name' => 'Model.cheap_name',
    'description' => 'Model.cheap_description'
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文