Help us understand the problem. What is going on with this article?

軽量ORMのidiormで調べた事まとめ

More than 1 year has passed since last update.

仕事で、軽量ORMのidiormを使っていて、
こちらの素晴らしい記事をいつも参考にさせていただいております。

PHPの薄いDBライブラリ「Idiorm」を使ってみた
PHPでORM「idiorm」を使えばかんたんにDBを操作できる

ただ、公式ドキュメントなどを調べないとわかない事などもあったため、
調べた内容をまとめておきたいと思います。

where in

<?php
$param = ['山田', '田中', '鈴木'];
$people = ORM::for_table('person')->where_in('name', $param)->find_many();

//実行されるSQL
SELECT * FROM `person` WHERE 'name' IN ('山田', '田中', '鈴木');

where like

<?php
$people = ORM::for_table('person')->where_like('name', '%山田%')->find_many();

//実行されるSQL
SELECT * FROM `person` where 'name' like ('%山田%');

MIN、MAX、SUM、AVG、COUNTなどの関数の利用

<?php
$count = ORM::for_table('person')->min("age");

//実行されるSQL
SELECT MIN(age) AS 'min' FROM `person` LIMIT 1;

同じ要領でMIN、、MAX、SUM、AVGも取得できます。

OFFSET、LIMIT

<?php
$count = ORM::for_table('person')->offset(2)->limit(3)->find_many();

//実行されるSQL
SELECT * FROM `person` LIMIT 3 OFFSET 2;

関数による値の更新

<?php
$person = ORM::for_table('person')->findOne(1);
$person->set_expr('up_date', 'NOW()');
$person->save();

//実行されるSQL
UPDATE `person` SET 'up_date' = NOW() WHERE `id` = 1;

関数による値の並び替え

<?php
$people = ORM::for_table('person')->order_by_expr('LENGTH(`name`)')->find_many();

//実行されるSQL
SELECT * FROM `person` ORDER BY LENGTH(`name`)

idiormで実行したSQLログの確認

<?php
ORM::configure('logging', true);
$sqls = ORM::get_query_log() ;
var_dump($sqls);

まとめ

idiormに慣れるととっても楽です!
でもSQLが書けなくなりそう💦

nagahama
dt-media
通販ビジネスを最大化させるためのノウハウと具体的なソリューションのすべてをお客様にワンストップで提供
https://dt-media.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした