LoginSignup
3
2

More than 1 year has passed since last update.

CakePHP4でusers Tableへ別々に保存した姓と名を結合して、ニックネームと同時にLike検索する

Last updated at Posted at 2023-03-20

baserCMS5ではユーザテーブルに名前に関する情報として、姓と名とニックネームカラムがあります。
姓と名は結合して検索したいよねって場合もよくありますよね。

そんなときは下記みたいなコードで取得することができますよ。

$concat = $query->func()->concat([
    'Users.real_name_1' => 'identifier',
    'Users.real_name_2' => 'identifier',
]);
$query->where(function ($exp) use ($concat, $keyword) {
    return $exp
        ->setConjunction('OR')
        ->like('Users.nickname', '%' . $keyword . '%')
        ->like($concat, '%' . $keyword . '%');
});
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2