1. Go to this page and download the library: Download lyhty/macros library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
lyhty / macros example snippets
$query = User::query()->selectKey();
$query->toSql(); // "select `id` from `users`"
use Lyhty\Macros\SearchPattern;
$query = User::query()
->searchLike('name', 'Matti Suo', SearchPattern::Left)
->orSearchLike('name', 'ranie', SearchPattern::Both)
->orSearchLike('name', 'mi', SearchPattern::Right)
->orSearchLike('name', 'Mat%i%emi', SearchPattern::Custom);
$query->toSql();
// "select * from `users` where (`users`.`name` LIKE ?) or (`users`.`name` LIKE ?) or (`users`.`name` LIKE ?) or (`users`.`name` LIKE ?)"
// First ? being "Matti Suo%", second "%ranie%", third "%mi" and fourth being the given search pattern as the search term ("Mat%i%emi" in this case)
$query = User::query()->searchLike('games.name', 'Apex Leg', SearchPattern::Right);
$query->toSql();
// select * from `users` where (exists
// (select * from `games` where `users`.`id` = `games`.`user_id` and `games`.`name` LIKE ?)
// )
// ? being "Apex Leg%"
// The pattern defaults to SearchPattern::Both, which means the given string will be wrapped with '%'.
$query = User::query()->searchLike(['games.console.name', 'games.platforms.name'], 'Xbox');
$query->toSql();
// select * from `users` where (exists (select * from `games` where `users`.`id` = `games`.`user_id` and (exists
// (select * from `consoles` where `games`.`console_id` = `consoles`.`id` and (`consoles`.`name` LIKE ?)) or exists
// (select * from `platforms` inner join `platform_game` on `platforms`.`id` = `platform_game`.`platform_id` where
// `games`.`id` = `platform_game`.`game_id` and (`platforms`.`name` LIKE ?)))))
// ? being "Xbox"