在cakePHP的find()中,我如何包含一个常量? [英] In cakePHP's find(), how do I include a constant?
问题描述
我正在建立一个SQL查询:
I'm looking to build a SQL query something like:
"SELECT email.member_id, 1 FROM email ... "
我正在使用
$this->Email->find('list', array(
'fields' => array('Email.member_id', '1'), ...
CakePHP正在生成:
CakePHP is generating:
SELECT `Email`.`member_id`, `Email`.`1` FROM `emails` AS `Email` ...
如何指定1是常数而不是数据库字段?
How do I specify that the 1 is a constant and not a database field?
我想这样做
我基本上想要返回一个关联数组,键的成员member_id和值为1.它会更好,只是得到一个直数组member_ids然后转换成我想要的数据结构?我想这会更快。
I basically want to return an associative array with keys of member_id and values of 1. Would it be better to just get a straight array of member_ids and then transform that into my wanted data structure? I thought this would be faster.
推荐答案
蛋糕不容易允许在你的具体情况下,我会想知道为什么你想有一个常量字段。但一般来说,你应该使用虚拟字段:
Cake does not easily allow that. In your specific case I would be wondering why you want to have a constant field anyways. But in general do to stuff like that you are supposed to use Virtual Fields:
http://book.cakephp.org/view/1608/Virtual-fields
基本上你会修改模型电子邮件添加:
Basically you would modify model Email by adding this:
var $virtualFields = array(
'one' => '1'
);
现在你可以这样进行查询:
Now you can do your query like this:
$this->Email->find('list', array(
'fields' => array('Email.member_id', 'Email.one'), ...
你添加了你应该得到一个直接的数组ids并填充它后不仅会更快,但也更少hacky =>更容易理解你可以轻松地用'array_fill_keys()'。
这篇关于在cakePHP的find()中,我如何包含一个常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!