Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cakephp 3模型函数的最佳编码实践_Php_Oop_Cakephp_Cakephp 3.0_Cakephp 3.1 - Fatal编程技术网

Cakephp 3模型函数的最佳编码实践

Cakephp 3模型函数的最佳编码实践,php,oop,cakephp,cakephp-3.0,cakephp-3.1,Php,Oop,Cakephp,Cakephp 3.0,Cakephp 3.1,我有一个current_day_users表和users表。在从current_day_用户提取数据时,以下两者之间的编码实践更好: 1.UsersTable.php代码 $this->CurrentDayUsers->find()->where(['user_id'=>$userId,'created'=>$dateToday])->first(); public function findUser($userId,$date){ retur

我有一个current_day_users表和users表。在从current_day_用户提取数据时,以下两者之间的编码实践更好:

1.UsersTable.php代码

   $this->CurrentDayUsers->find()->where(['user_id'=>$userId,'created'=>$dateToday])->first();
public function findUser($userId,$date){
    return  $this->find()->where(['user_id'=>$userId,'created'=>$date])->first();
}
  • UsersTable.php代码

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

  • CurrentDayusersTable.php代码

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

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

    public function findUser($userId,$date){
       return  $this->find()->where(['user_id'=>$userId,'created'=>$date])->first();
    }
    
    这是一个简洁的代码,而不是在Users表中使用查询生成器