dongxianrang9269 2014-11-20 12:35
浏览 15
已采纳

CakePHP / MySQL:查找模型本身和指定模型的条件

Consider, we have users and the users have accounts (one-to-many). Now I am completely stuck at the following problem: we provide a $query and want to return all users:

  • whose fullname LIKE %$query% OR
  • whose email LIKE %$query% OR
  • who have accounts, of which organisation LIKE %$query%

I already tried joins and containable behavior but no success. With joins we could achieve this, but the problem is, the data is not structured in the cake way (we get only one row pro match even if same user occurs multiple times). Resorting the data will not be OK because this controller must be very fast.

Help!

  • 写回答

2条回答 默认 最新

  • douliang1900 2014-11-20 12:42
    关注

    Try with -

    $this->User->find(
        'all',
        array(
         'conditions' => array(
                          'OR' => array(
                                   'User.fullname LIKE' => '%'.$query.'%',
                                   'User.email LIKE' => '%'.$query.'%',
                                   'Account.organisation LIKE' => '%'.$query.'%',
                                  ),
                         ),
         'contain'    => 'Account',
        )
    );
    

    Using alias -

    $qry = "select * from users as u, accounts as a where u.fullname like %$query% or u.emaillike %$query% or a.organisation like %$query%";
    $results = $this->query($qry);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码