Idiormが軽量のORマッパー。PHP5用
https://github.com/j4mie/idiorm
Idiormを使って軽量なActive Recordを実装したのがParis。
https://github.com/j4mie/paris
参考) PHPの薄いDBライブラリ「Idiorm」を使ってみた
http://qiita.com/naga3/items/87fef230ac86aeec1eea
ドキュメントが充実している。
Idiormのドキュメント
http://idiorm.readthedocs.org/en/latest/index.html
Parisのドキュメント
http://paris.readthedocs.org/en/latest/index.html
文字コードの指定方法 等
ORM::configure(‘mysql:host=localhost;dbname=mydb’);
ORM::configure(‘username’, ‘root’);
ORM::configure(‘password’, ‘root’);
ORM::configure(‘driver\_options’, array(PDO::MYSQL\_ATTR\_INIT\_COMMAND => ‘SET NAMES utf8’));
アソシエーションについて
http://paris.readthedocs.org/en/latest/associations.html
Has-one
デフォルトでは、関連テーブルの中に外部キーとして元のテーブル名に「_id」を付けたカラムがあることを想定している。
他の名前が付いている場合は、has_one()の第2引数にカラム名を指定する。
<?php
class Profile extends Model {
}
class User extends Model {
public function profile() {
return $this->has_one(‘Profile’);
}
}
外部キーがデフォルトのuser_idではなくabc_idだったら
<?php
class Profile extends Model {
}
class User extends Model {
public function profile() {
return $this->has\_one(‘Profile’, ‘abc\_id’);
}
}
<?php
// Select a particular user from the database
$user = Model::factory('User')->find\_one($user\_id);
// Find the profile associated with the user
$profile = $user->profile()->find_one();
Has many
<?php
class Post extends Model {
}
class User extends Model {
public function posts() {
return $this->has_many(‘Post’); // Note we use the model name literally – not a pluralised version
}
}
<?php
// Select a particular user from the database
$user = Model::factory('User')->find\_one($user\_id);
// Find the posts associated with the user
$posts = $user->posts()->find_many();
Belongs to
<?php
class Profile extends Model {
public function user() {
return $this->belongs_to(‘User’);
}
}
class User extends Model {
}
<?php
// Select a particular profile from the database
$profile = Model::factory('Profile')->find\_one($profile\_id);
// Find the user associated with the profile
$user = $profile->user()->find_one();
Has many through
多対多。以下は、複数の著者を持つかもしれないBookと複数の本を書いているかもしれない著者の関係。
デフォルトとして想定されている中間テーブル名はAuthorBookで、2つのテーブル名をアルファベット順で繋げたもの。
<?php
class Author extends Model {
public function books() {
return $this->has\_many\_through(‘Book’);
}
}
class Book extends Model {
public function authors() {
return $this->has\_many\_through(‘Author’);
}
}
class AuthorBook extends Model {
}
<?php
// Select a particular book from the database
$book = Model::factory('Book')->find\_one($book\_id);
// Find the authors associated with the book
$authors = $book->authors()->find_many();
// Get the first author
$first_author = $authors[0];
// Find all the books written by this author
$first\_author\_books = $first\_author->books()->find\_many();
テーブル名やカラム名が想定されるものと違う場合は、デフォルト値をオーバーライドする。
第1引数: 関連テーブル名
第2引数: 中間テーブル名
第3引数: 中間テーブル上の基本テーブルを示すカラム名
第4引数: 中間テーブル上の関連テーブルを示すカラム名
第5引数: 基本テーブルの外部キー名
第6引数: 関連テーブルの外部キー名
limit, offset等の指定方法
Userテーブルから、
typeがa、配列$idsに含まれるidを除いて、10件オフセット、id降順で20件取得する例
$user = Model::factory(‘User’)
->where_equal(
‘type’, ‘a’
)
->where\_not\_in(
‘id’,
$ids
)
->limit(20)
->offset(10)
->order\_by\_desc(‘id’)
->find_many();
GROUP BYを使う
->group\_by(‘session\_id’)