■データ取得

 ・IDで取得

  $entry = Model_Article::find(2);

  // 複合主キーの場合

  $entry = Model_Article::find(array(2, 'foo'));


 ・最初と最後を取得

  // 最初

  $entry = Model_Article::find('first');

  // 最後

  $entry = Model_Article::find('last', array('order_by' => 'date'));


 ・全てを取得

  $entrys = Model_Article::find('all');

  // where, order_by指定

  $entrys = Model_Article::find('all', array(

        'where' => array(

            array('category_id', 1),

            'or' => array(

                array('category_id', 2),

            ),

        ),

        'order_by' => array('date' => 'desc'),));


 ・クエリで取得

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

  // 件数

  $count = $query->count();

  // 最大のID

  $max_id = $query->max('id');

  // 1件取得

  $article = $query->get_one();

  // 条件を追加して複数件取得

  $articles = $query->limit(15)->get();


 ・関連テーブルも取得

  $query = Model_Article::query()

        ->related(array('author', 'comments'));


 ・複雑なwhere

  WHERE 'author'=16 AND ('date' < 1348404127 OR 'draft'=1)

  $query = Model_Article::query()

        ->where('author' '=', 16)

        ->and_where_open()

          ->where('date', '<', time())

          ->or_where('draft', '=', 1)

        ->and_where_close();


■データ登録

 ・方法1

  $new = new Model_Example();

  $new->property = 'something';

  $new->save();


 ・方法2

  // forge()でインスタンス生成

  $new = Model_Example::forge();

  $new->property = 'something';

  $new->save();


 ・方法3

  // 配列でプロパティ設定

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

  $new = new Model_Example($props);

  $new->save();


■データ更新

 ・方法1

  $entry = Model_Article::find(4);

  $entry->title = 'My first edit';

  $entry->author = 'tiger';

  $entry->save();


 ・方法2

  $entry = Model_Article::find(4);

  $entry->set(array(

        'title' => 'My first edit',

        'author' => 'tiger'));

  $entry->save();


■データ削除

 $entry = Model_Article::find(4);

 $engry->delete();


■リレーションテーブル

 ・リレーションタイプ

  Belongs To : 多対1

  Has One : 1 対1

  Has Many : 1 対多

  Many to Many : 多対多


 ・Belongs To

  Model_Post と Model_Comment があるとする。

  posts : comments = 1 : 多

  comments.post_id が存在する。


  ・Model_Comment に下記設定をする。

   protected static $_belongs_to = array('post');


  ・使用例

   $comment = new Model_Comment();

   $comment->post = new Model_Post();

   $comment->save();

   $comment = Model_Comment:find(6);

   $comment->post = Model_Post::find(1);

   $comment->save();


 ・Has One

  Model_User と Model_Profile があるとする。

  users : profiles = 1 : 1

  profiles.user_id が存在する。


  ・Model_User に下記設定をする。

   protected static $_has_one = array('profile');


  ・使用例

   $user = new Model_User();

   $user->profile = new Model_Profile();

   $user->save();

   $user = Model_User::find(6);

   $user->profile = Model_Profile::find(1);

   $user->save();


 ・Has Many

  Model_Post と Model_Comment があるとする。

  posts : comments = 1 : 多

  comments.post_id が存在する。


  ・Model_Post に下記設定をする。

   protected static $_has_many = array('comments');


  ・使用例

   $post = new Model_Post();

   $post->comments[] = new Model_Comment();

   $post->save();

   $post = Model_Post::find(1);

   $post->comments[6] = Model_Comment::find(6);

   $post->save();

   $post = Model_Post::find(1);

   unset($post->comments[6]);

   $post->save();


 ・Many to Many

  Model_Post と Model_User があるとする。

  posts : users = 多 : 多

  中間テーブル posts_users に、post_id と user_id が存在する。


  ・Model_Post に下記設定をする。

   protected statice $_many_many = array('users');


  ・使用例

   $post = new Model_Post();

   $post->users[] = new Model_User();

   $post->save();

   $user = Model_User::find(8);

   $user->posts[1] = Model_Post::find(1);

   $user->save();

   $post = Model_Post::find(1);

   unset($post->users[8]);

   $post->save();



via System Development Tips
Your own website,
Ameba Ownd