作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 CakePHP 2.0,并且我有一个带有“virtualFields”的模型:
国家/地区.php:
var $virtualFields = array(
'path' => "CONCAT_WS('/', dirname, basename)"
);
如果我在使用“用户”的 Controller 中执行此操作
$users = $this->User->find('all');
虚拟字段路径
已设置。
如果我在我的 Controller 中使用它,它也使用“用户”
$options['fields'] = array(
'DISTINCT User.*'
);
$options['joins'] = array(
array(
'table' => 'courses',
'type' => 'inner',
'conditions' => array(
'User.id = courses.user_id'
)
),
array(
'table' => 'times',
'type' => 'inner',
'conditions' => array(
'courses.id = times.course_id'
)
)
);
$options['conditions'] = array(
'times.amount > ' => 0
);
$users = $this->User->find('all', $options);
有了选项,path
字段就没有设置了,当然SQL查询中好像没有包含“CONCAT_WS('/', dirname, basename)”字段,也就是如果我在没有选项的情况下执行 find
操作,则包含在内。
如何使用这些选项来自动包含虚拟字段?当然,我可以在 fields
选项中写入 CONCAT,但这不是很好,特别是如果我更改它。
致以诚挚的问候。
最佳答案
使用分组而不是不同,并完全删除字段选项。
$options['group'] = array(
'User.*'
);
$options['joins'] = array(
array(
'table' => 'courses',
'type' => 'inner',
'conditions' => array(
'User.id = courses.user_id'
)
),
array(
'table' => 'times',
'type' => 'inner',
'conditions' => array(
'courses.id = times.course_id'
)
)
);
$options['conditions'] = array(
'times.amount > ' => 0
);
$users = $this->User->find('all', $options);
关于php - CakePHP 2.0 - virtualFields 不用于带选项的查找操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9264982/
我有一个名为 User 的模型,它有一个 Virtual field名为 full_name。当我在 find() 查询中访问我的模型 User 时,我可以毫无问题地在我的虚拟字段上设置条件,如下所示
我正在使用 CakePHP 2.0,并且我有一个带有“virtualFields”的模型: 国家/地区.php: var $virtualFields = array( 'path' => "
我是一名优秀的程序员,十分优秀!