cakephp 什么是蛋糕SQL在蛋糕控制器PHP

l5tcr1uw  于 11个月前  发布在  PHP
关注(0)|答案(1)|浏览(172)

我需要蛋糕控制器PHP文件中的蛋糕SQL

SELECT Time(NOW()),
       evaluation_start_time,
       AddTime(evaluation_start_time, CONCAT(evaluation_hour_after, ':00:00')) temp_start_time,
       AddTime(evaluation_start_time, CONCAT(evaluation_hour_after, ':14:00')) temp_end_time,

  (SELECT count(id)
   FROM evaluation_user_ratings
   WHERE evaluation_id = evaluation.id) player_rating_given,

  (SELECT count(id)
   FROM evaluations_users_and_team
   WHERE evalution_id = evaluation.id) player_count,

  (SELECT rating_added_by
   FROM evaluation_user_ratings
   WHERE evaluation_id = evaluation.id) AS puser_id,
       evaluation.*
FROM evaluation
WHERE evaluation_date >= date(NOW())
  AND (evaluation_day_after != 0
       OR evaluation_hour_after != 0)
  AND evaluation_status = '1'
HAVING player_count >= player_rating_given
AND Time(NOW()) >= temp_start_time
AND Time(NOW()) <= temp_end_time

字符串

ar5n3qh5

ar5n3qh51#

如果我读你的查询的权利与列播放器_计数(evalution_id)可能错字..;对于Cakephp 4.0你有两个选择,这是使用queryBuilder或连接管理器
对于raw sql来说,最简单的一个是连接管理器,您必须事先在控制器和/或父控制器中调用Lib
1.

use Cake\Datasource\ConnectionManager;

字符串
1.在你的控制器函数中,在保持Cakephp的表/实体结构的同时,围绕着下面的行。
如图所示,

$connection = ConnectionManager::get('default');

$query = "SELECT 
                Time(NOW()),
                Evaluation.evaluation_start_time,
                AddTime(Evaluation.evaluation_start_time, CONCAT(Evaluation.evaluation_hour_after, ':00:00')) temp_start_time,
                AddTime(Evaluation.evaluation_start_time, CONCAT(Evaluation.evaluation_hour_after, ':14:00')) temp_end_time,
                (SELECT count(EvaluationUserRatings.id) FROM evaluation_user_ratings EvaluationUserRatings WHERE EvaluationUserRatings.evaluation_id = Evaluation.id) player_rating_given,
                (SELECT count(EvaluationsUsersAndTeam.id) FROM evaluations_users_and_team EvaluationsUsersAndTeam WHERE EvaluationsUsersAndTeam.evaluation_id = Evaluation.id) player_count,
                (SELECT EvaluationUserRatings.rating_added_by FROM evaluation_user_ratings EvaluationUserRatings WHERE  EvaluationUserRatings.evaluation_id = Evaluation.id) AS puser_id,
                Evaluation.*
            FROM evaluation Evaluation
            WHERE Evaluation.evaluation_date >= date(NOW())
              AND (Evaluation.evaluation_day_after != 0
                   OR Evaluation.evaluation_hour_after != 0)
              AND Evaluation.evaluation_status = '1'
            HAVING player_count >= player_rating_given
            AND Time(NOW()) >= temp_start_time
            AND Time(NOW()) <= temp_end_time";

        $results = $connection->execute($query)->fetchAll('assoc');


如果你决定使用queryBuilder,我相信这是一个接近你的目标的代表和/或例子。

$evaluations = $this->getTableLocator()->get('Evaluation');
    $queryA = $evaluations->find()
        ->selectAllExcept($evaluations,['Evaluation.evaluation_state_time']);
    $evaltest = $queryA
        ->select([
            'time_now'=>'Time(NOW())',
            'Evaluation.evaluation_start_time',
            'temp_start_time'=>'AddTime(Evaluation.evaluation_start_time, CONCAT(Evaluation.evaluation_hour_after, \':00:00\'))',
            'temp_end_time'=>'AddTime(Evaluation.evaluation_start_time, CONCAT(Evaluation.evaluation_hour_after, \':14:00\'))'
            ])
        
        ->select([
            'player_rating_given'=>$evaluations->EvaluationUserRatings->find()
                    ->join([
                        'Evaluation'=>[
                            'table'=>'evaluation',
                            'conditions'=>'EvaluationUserRatings.evaluation_id = Evaluation.id'
                            ]
                        ])
                    ->count(),
            'player_count'=>$evaluations->EvaluationsUsersAndTeam->find()
                    ->join([
                        'Evaluation'=>[
                            'table'=>'evaluation',
                            'conditions'=>'EvaluationsUsersAndTeam.evaluation_id = Evaluation.id'
                            ]
                        ])
                    ->count(),
            'puser_id'=>$evaluations->EvaluationUserRatings->find()
                    ->select(['EvaluationUserRatings.rating_added_by'])
                    ->join([
                        'Evaluation'=>[
                            'table'=>'evaluation',
                            'conditions'=>'EvaluationUserRatings.evaluation_id = Evaluation.id'
                            ]
                        ])->disableHydration()->toArray()[0]['rating_added_by']
            ])
        ->where([
            'Evaluation.evaluation_date >= date(NOW())',
            'Evaluation.evaluation_status = "1"',
            'OR'=>[
                'Evaluation.evaluation_day_after != 0',
                'Evaluation.evaluation_hour_after != 0'
                ]
            ])
        ->having([
            'player_count >= player_rating_given',
            'Time(NOW()) >= Time(temp_start_time)',
            'Time(NOW()) <= Time(temp_end_time)'
            ]);
    print_r($evaltest->disableHydration()->toArray());


这将返回一个对象,您可以简单地通过将**->toArray();**添加到上面的queryBuilder的末尾来将其转换为数组。
结果:

Array
(
    [0] => Array
        (
            [id] => 1
            [evaluation_date] => 2023-11-25 06:09:00
            [evaluation_day_after] => 1
            [evaluation_hour_after] => 0
            [evaluation_status] => 1
            [evaluation_start_time] => 06:08:00
            [time_now] => 06:11:02
            [temp_start_time] => 06:08:00.000000
            [temp_end_time] => 06:22:00.000000
            [player_rating_given] => 1
            [player_count] => 1
            [puser_id] => 3
        )

)


经过一些本地测试,我注意到一些东西可能会派上用场。
1.在模型中,使用冻结日期时间的蛋糕可能是一个问题,所以为了解决这个问题,在EvaluationTable.php中添加到您的公共函数initialize。如果您有任何其他DateTime列,我也会对它们做同样的事情。
$this->getSchema()->setColumnType('evaluation_date','string');
在你的配置文件/app. php中,确保你的defaultTimeZone以及你的系统/服务器的时间是准确的。
我在黑暗中拍摄的模型,但我的测试模型是如下的主要表评估:

TABLE `evaluation` (
  `id` int NOT NULL AUTO_INCREMENT,
  `evaluation_date` datetime DEFAULT NULL,
  `evaluation_day_after` decimal(2,0) DEFAULT NULL,
  `evaluation_hour_after` decimal(2,0) DEFAULT NULL,
  `evaluation_status` decimal(2,0) DEFAULT NULL,
  `evaluation_start_time` time DEFAULT NULL,
  PRIMARY KEY (`id`)
)


希望这对你有帮助。祝你好运。哦,关于queryBuilder还有一件事,我相信它不允许,这是prep-query函数**->selectAllExcept()**作为解决方案的原因。

相关问题