我试图返回一个mysql字段,该字段在返回Cake之前已经应用了mysql REPLACE
函数。我实际上是在尝试从字段中删除文本,如果它与我的变量匹配。
从理论上讲,我想要:
'fields' => array('REPLACE(E.title, "'.$data['Make']['name'].'", "") AS E.title', ...)
我该怎么做?
由于
答案 0 :(得分:1)
尝试在模型中使用Virtual Fields
class ModelName extends AppModel {
public $virtualFields = array(
"my_virtual_field_title" => "REPLACE(yourfield,'OLD STR','NEW STR')"
);
}
使用:
$your_query= $this->ModelName->find('first');
echo $your_query['ModelName']['my_virtual_field_title'];
或者您可以直接设置虚拟场:
$this->ModelName->virtualFields['my_virtual_field_title'] = "
CASE
WHEN title = 'custom text' THEN 'custom result'
.
.
.
ELSE ''
END
";
更多信息: http://book.cakephp.org/2.0/en/models/virtual-fields.html