やったこと

webサービスを作るときに考えたことを垂れ流します

【PHP】idiormで「以上」や「以下」の条件式を使って検索!

簡単にDBを操作できるPHPライブラリ「idiorm」で、whereの条件式を使って「~以上」や「~以下」の値のレコードを検索する方法です。

以下のようなPHPコード実行すると、各条件でレコードを検索できます。

  //idiorm読み込み!
  require_once dirname(__FILE__).'/lib/idiorm/idiorm.php';

  //DBファイル読み込み!
  $db_path = dirname(__FILE__).'/db/hogege.db'; 
  ORM::configure('sqlite:'.$db_path); //DBはSQLiteを使用

  //ageが10よりも小さいデータを取得
  $records = ORM::for_table('tweets')->where_lt('age', 10)->find_many();

  //ageが10よりも大きいデータを取得
  $records = ORM::for_table('tweets')->where_gt('age', 10)->find_many();

  //ageが10以下のデータを取得
  $records = ORM::for_table('tweets')->where_lte('age', 10)->find_many();

  //ageが10以上のデータを取得
  $records = ORM::for_table('tweets')->where_gte('age', 10)->find_many();

以上!