【问题标题】:How to do querys in cakephp3 involving CONCAT如何在 cakephp 3 中进行涉及 CONCAT 的查询
【发布时间】:2016-05-09 13:37:14
【问题描述】:

我手头有这个特殊的查询,我想把它写成一个 cakephp 查询,但是有太多特殊的东西,我没有尝试对我有用:

select concat(c.name,' - ',e.name) as workplace 
from vacancies v, states e, citys c 
where c.idstate = e.id 
and v.idstate = c.idstate 
and v.idcity = c.id;

我真的不知道如何将这个查询字面翻译成 cakephp 风格,有点像:

$workplace = $this->Vacancies->Cities->find(...)->where(...);

【问题讨论】:

标签: mysql sql cakephp cakephp-3.0


【解决方案1】:

你显然没有尝试read the official documentation

搜索“concat”会将此显示为第一次点击。让我为您复制并粘贴手册:

CakePHP 的 ORM 为一些常用的 SQL 函数提供了抽象。使用抽象允许 ORM 选择您想要的功能的平台特定实现。例如,concat 在 MySQL、PostgreSQL 和 SQL Server 中的实现方式不同。使用抽象可以让您的代码具有可移植性:

[...]

concat() 将两个值连接在一起。除非标记为文字,否则参数将被视为绑定参数。

甚至还有一个例子:

$query = $articles->find()->innerJoinWith('Categories');
$concat = $query->func()->concat([
    'Articles.title' => 'identifier',
    ' - CAT: ',
    'Categories.name' => 'identifier',
    ' - Age: ',
    '(DATEDIFF(NOW(), Articles.created))' => 'literal',
]);
$query->select(['link_title' => $concat]);

【讨论】:

  • 我试过了,但我就是无法理解最后一行“$query->select(['link_title' => $concat]);”。它到底有什么作用??
【解决方案2】:

最后一行是别名

concat(Articles.title, ' ', Categories.name) AS link_title

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-05
    • 1970-01-01
    • 2017-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    相关资源
    最近更新 更多