Help us understand the problem. What is going on with this article?

cakephp modelメモ

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');
}

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away