LoginSignup
32
38

More than 5 years have passed since last update.

PHPでORM「idiorm」を使えばかんたんにDBを操作できる

Posted at

idiormを使えば、PDOでSQLをゴリゴリ記述しなくてよくなる

どういうイメージかというと、素のJavaScriptでゴリゴリ記述するか、jQueryでスッキリ記述するか、その違いに似ています。もちろんidiormがjQueryの方。

idiormの基本的な使い方

https://github.com/j4mie/idiorm
からファイルをダウンロード。idiorm.phpだけあれば動きます。

config.php
require_once 'idiorm.php';

ORM::configure('mysql:host=localhost;dbname=test');
ORM::configure('username', 'root');
ORM::configure('password', 'root');
ORM::configure('driver_options', [
    PDO::MYSQL_ATTR_INIT_COMMAND       => 'SET NAMES utf8',
    PDO::ATTR_EMULATE_PREPARES         => false,
    PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
]);

idiorm.phpを読み込んでDBの読込設定。

かんたんな例

$person = ORM::for_table('person')->where('name', '山田')->find_one();

for_tableにテーブル名、
whereにフィールド名と検索したい文字列、
最後にfind_oneでひとつだけ検索,find_manyで複数できる。

上の例は
SELECT * FROM person WHERE name = "山田"
と同じです。

サンプル

詳しくはドキュメントにバッチリ書かれていますがご紹介。
http://idiorm.rtfd.org/

update

sample.php
<?php
$people = ORM::for_table('person')->find_many();
foreach ($people as $person) {
    $person->name = '山田太郎';
    $person->save();
}
?>

nameフィールドを全て山田太郎にしている。
下のコードも同じことをしていて、よりシンプル。

sample.php
<?php
ORM::for_table('person')->find_result_set()
->set('name', '山田太郎')
->save();
?>

insert

sample.php
<?php
$person = ORM::for_table('person')->create();
$person->name = 'Joe Bloggs';
$person->age = 40;
$person->save();
?>

count

sample.php
<?php
echo count(ORM::for_table('person')->find_result_set());
?>

and 検索

sample.php
<?php
$people = ORM::for_table('person')
            ->where(array(
                'name' => '山田太郎',
                'フィールド名' => '値'
            ))
            ->find_many();
?>

or 検索

sample.php
<?php
$people = ORM::for_table('person')
            ->where_any_is(array(
                array('name' => 'Joe', 'age' => 10),
                array('name' => 'Fred', 'age' => 20)))
            ->find_many();

// 下のSQLと同じ
SELECT * FROM `widget` WHERE (( `name` = 'Joe' AND `age` = '10' ) OR ( `name` = 'Fred' AND `age` = '20' ));
?>

○以上○未満

sample.php
<?php
//より下
$people = ORM::for_table('person')->where_lt('age', 10)->find_many();
//より上
$people = ORM::for_table('person')->where_gt('age', 5)->find_many();
//未満
$people = ORM::for_table('person')->where_lte('age', 10)->find_many();
//以上
$people = ORM::for_table('person')->where_gte('age', 5)->find_many();
?>

like , not like

sample.php
<?php
$people = ORM::for_table('person')->where_like('name', '%fred%')->find_many();
$people = ORM::for_table('person')->where_not_like('name', '%bob%')->find_many();
?>

order

sample.php
<?php
$people = ORM::for_table('person')->order_by_asc('gender')->order_by_desc('name')->find_many();
?>

group

sample.php
<?php
$people = ORM::for_table('person')->where('gender', 'female')->group_by('name')->find_many();
?>

as

sample.php
<?php
$people = ORM::for_table('person')->select('name', 'person_name')->find_many();

//下のSQLと同じ
SELECT `name` AS `person_name` FROM `person`;

$people_count = ORM::for_table('person')->select_expr('COUNT(*)', 'count')->find_many();

//下のSQLと同じ
SELECT COUNT(*) AS `count` FROM `person`;
?>

join

sample.php
<?php
$results = ORM::for_table('person')->join('person_profile', array('person.id', '=', 'person_profile.person_id'))->find_many();
?>

transaction

sample.php
<?php
// Start a transaction
ORM::get_db()->beginTransaction();

// Commit a transaction
ORM::get_db()->commit();

// Roll back a transaction
ORM::get_db()->rollBack();
?>
32
38
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
32
38