【问题标题】:Cakephp 3 best coding practice for model functionsCakephp 3 模型函数的最佳编码实践
【发布时间】:2016-07-06 19:09:51
【问题描述】:

我有一个 current_day_users 表和 users 表。从 current_day_users 中提取数据的以下更好的编码实践是什么:

1.UsersTable.php代码

   $this->CurrentDayUsers->find()->where(['user_id'=>$userId,'created'=>$dateToday])->first();
  1. UsersTable.php 代码

    $this->CurrentDayUsers->findUser($userId,$dateToday);

CurrentDayusersTable.php 代码

public function findUser($userId,$date){
    return  $this->find()->where(['user_id'=>$userId,'created'=>$date])->first();
}

【问题讨论】:

    标签: php oop cakephp cakephp-3.0 cakephp-3.1


    【解决方案1】:

    如果您已经有 CurrentDayUsers 表,我建议您采用第二种方法:

    public function findUser($userId,$date){
       return  $this->find()->where(['user_id'=>$userId,'created'=>$date])->first();
    }
    

    这提供了简洁的代码,而不是使用用户表中的查询生成器。

    【讨论】:

      猜你喜欢
      • 2016-11-05
      • 2011-04-07
      • 1970-01-01
      • 1970-01-01
      • 2018-08-29
      • 1970-01-01
      • 2014-07-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多