LoginSignup
0
0

More than 3 years have passed since last update.

cakephp modelメモ

Posted at

cakephpのモデル機能で便利だと思った機能をまとめています。

  • 複雑な条件で取得する場合共通部品化すると便利
class UsersTable extends Table
{
    # 疎通確認済みの条件
    public function findActive(Query $query, array $options)
    {
        return $query->where(['Users.status' => 1]);
    }

    # 有料プランの条件
    public function findPaid(Query $query, array $options)
    {
        return $query->where(['Users.premium' => 1]);
    }
}

# コントローラ内、下記のように利用
# (この時点では SQL 文の作成しか行われず実行されませんが便宜上取得と書きました)

class UsersController extends AppController
{
    # 疎通確認済みのユーザの取得
    $active = $this->Users->find('active');

    # 有料プランのユーザの取得
    $paid = $this->Users->find('paid');

    # 疎通確認済み & 有料プランのユーザの取得
    $premium = $this->Users->find('active')
        ->find('paid');
}

0
0
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
0
0