如何在cakephp3中查询涉及CONCAT的查询

时间:2016-05-09 13:37:15

标签: mysql sql cakephp cakephp-3.0

我手头有这个特殊的查询,我想把它写成一个cakephp查询,但有很多具体的事情,我尝试过的任何东西对我都没有:

function searchPeople()
{
    var requestUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/search/query?querytext='*'&sourceid='b09a7990-05ea-4af9-81ef-edfab16c4e31'";
    return $.getJSON(requestUrl);
}



function getUserProperties(account){
    var encName = encodeURIComponent(account);
    var requestUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=@v)?@v='" + encName + "'";
    return $.getJSON(requestUrl);
}


searchPeople()
.then(function(result){

   //prepare promises 
   var promises = result.PrimaryQueryResult.RelevantResults.Table.Rows.map(function(r){
       var accountName = r.Cells[3].Value;
       return getUserProperties(accountName);
   });

   //resolve for each of the promises
   $.when.apply($, promises).then(function () {
      for (var i = 0; i < arguments.length; i++) {
        var profileProperties = arguments[i][0];  
        console.log(profileProperties.DisplayName);
      }
   });

})
.fail(function(error){
   console.log(JSON.stringify(error));  
});

我真的迷失了如何将这个查询字面翻译成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;

2 个答案:

答案 0 :(得分:3)

您显然没有尝试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]);

答案 1 :(得分:0)

最后一行是别名

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