CakePHP 在运行时更改虚拟字段
我有一个多站点应用程序的产品模型。
根据域(站点)我想加载不同的数据。
例如,我的数据库中没有 name
和 description
字段,而是有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,如果我将它放在构造函数中而不是
beforeFind
回调中,它似乎可以工作:但是,我不确定这是否是
CakePHP
否否那能回来咬我吗?Well if I put it in the constructor instead of the
beforeFind
callback it seems to work:However, I am not sure if this is a
CakePHP
no no that can come back to bite me?似乎问题可能在于模型关联是一个动态构建的模型。例如 AppModel
尝试执行 pr(get_class($this->Relation));在代码中查看输出是什么,它应该是您的模型名称而不是 AppModel。
还尝试使用:
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: