dpr26232 2011-05-25 15:29
浏览 11
已采纳

CakePHP中的虚拟字段上的子串+ Concat - 可能吗?

This seems like a simple question, but I can't get it to work. I've created a virtual field that combines a name and address:

var $virtualFields = array(
    'name_address' => 'CONCAT(Venue.name, " - ", Venue.address)'
);

Is there a way to use SUBSTR on this? (PHP or MYSQL is fine - whichever is best/works).

I've tried this (to no avail):

'name_address' => 'SUBSTR(CONCAT(Venue.name, " - ", Venue.address),0,50)'

I've also tried SUBSTR on the individual fields within the CONCAT.

  • 写回答

1条回答 默认 最新

  • dongyi8416 2011-05-25 19:48
    关注

    This is absolutely no problem. Your only error is, that you thought of the SUBSTRING-function in MySQL working like the one in PHP where the count starts at zero. But since this isn't PHP you have to start your counting from 1 and all will work fine.

    Just use your virtualField like this and it should work just like you want:

    var $virtualFields = array(
        'name_address' => 'SUBSTRING(CONCAT(Venue.name, " - ", Venue.address),1,50)'
    );
    

    Also, have a read on the SUBSTRING-function in the MySQL manual

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan