Orm

ORMはオブジェクト 関係マッピングの略です。 これは、2つのことを行います: オブジェクトにデータベースのテーブルの行をマップし、 それはあなたがそれらのオブジェクト間の関係を確立することができます。
それは Active Record パターンに従いますが、 他のシステムに影響も受けます。

CRUD: 作成、読み取り、更新、削除

一度あなたのモデルを設定したそれは、作成、読み取り、更新、 およびデータベース内のエントリを行います。

作成

// オプション 1
$new = new Model_Example();
$new->property = 'something';
$new->save();

// オプション 2, newの代わりにforgeを使用する。
$new = Model_Example::forge();

save()した後のmodelは、データベースに保存されていると、主キー 主キーにauto_incrementを使用している場合は、自動的に保存された後、インスタンスに保存されます。

配列からモデルのプロパティを設定することができます:

$props = array('property' => 'something');

// "new"を使用する。
$new = new Model_Example($props);
$new->save();

// オプション2, newの代わりにforgeを使用する。
$new = Model_Example::forge($props)->save();

読み取り

Or actually find(). The find method can be used in 3 ways: find a specific id (primary key), find first/last or all entries with conditions, or use method chaining to fetch. All possible selection methods (where, or_where, order_by, etc) can be found at the bottom of the page.

IDから検索する

// ID=2の記事を検索する
$entry = Model_Article::find(2);

// ...もしくは複数の主キーで検索する
$entry = Model_Article::find(array(2, 5));

この例では、IDが見つからなかったときはModel_Articleインスタンスまたはnullを返します。

最初と最後を検索する

// 最初のエントリーを検索する
$entry = Model_Article::find('first');

//  ordered by dateの条件で最後のエントリーを検索する
$entry = Model_Article::find('last', array('order_by' => 'date'));

この例では、IDが見つからなかったときはModel_Articleのインスタンスまたはnullを返します。

すべてを検索する

// すべての記事を検索する。
$entry = Model_Article::find('all');

// find all articles from category 1 order descending by date
$entry = Model_Article::find('all', array(
	'where' => array(
		array('category_id', 1),
	),
	'order_by' => array('date' => 'desc'),
));

この例では、それは常にModel_Articleのインスタンスの配列を返します。

メソッドチェーンを使用して見つける

あなたが、find() メソッドを利用するとき、 Orm\Query オブジェクト使用できる限り エントリを検索するために再利用されます。

$query = Model_Article::find()->where('category_id', 1)->order_by('date', 'desc');

// ページネーションのための記事の総数を知りたい場合。
$number_of_articles = $query->count();

// 1記事を取得
$newest_article = $query->get_one();

// 以前のクエリに復帰制限を加えて、複数の記事を取得するために再使用します。
$all_articles = $query->limit(15)->get();

All these methods are equally valid, the four other methods of find actually use the Query object as well but don't return it.

更新

$entry = Model_Article::find(4);
$entry->title = 'My first edit';
$entry->author = 'Total n00b';
$entry->save();

検索し、そして見つけたもののプロパティを変更して、保存します。

削除

$entry = Model_Article::find(4);
$entry->delete();

Again nothing more to it: Find and delete.

In the previous example the $entry variable and its object still exist. The primary keys are however set to null and the object itself is considered a new instance. If you save it after deletion it will be re-entered into the database and be given a new primary key when auto_increment is switched on.

All selection methods when using find

Method Params Examples
where string $column, [string $operator,] mixed $value
// Single where
Model_Article::find()->where('id', 4);
Model_Article::find('all', array('where' => array('category_id' => 5)));

// Multiple where usage examples
Model_Article::find()->where('id', 4)->where('category_id', '>', 1);
Model_Article::find()->where(array('id' => 4, 'category_id' => 6));
Model_Article::find('all', array('where' => array(array('category_id', '=', 5), array('publish', '<', time()))));
where string $column, [string $operator,] mixed $value Same as where()

More to be written.