草庐IT

关于 mysql:Converting custom sql query in to a dataprovider yii?

codeneng 2023-03-28 原文

Converting custom sql query in to a dataprovider yii?

我正在尝试将以下查询转换为数据提供程序,以便它可以显示在 CGridView 中。我曾尝试使用 CArrayDataProvider,但到目前为止还没有任何运气,任何帮助将不胜感激!

这里是查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public function getTeamsByLevelIdAndCompetitionId($levelId, $competitionId)
{
    $query ="SELECT t.*,
         (SELECT COUNT(*)
    FROM tbl_competition_teams ct
    WHERE ct.team = t.id
    AND ct.competition = :competitionId) AS 'inCompetition'
    FROM tbl_teams t
    WHERE t.level = :levelId"
;

    $params = array(
        'levelId' => $levelId,
        'competitionId' => $competitionId
    );

    $result = array();
    $teams = $this->findAllBySQL($query, $params);
    return $teams;
}

这就是我尝试将其放入 CArrayDataProvider 的方式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public function getTeamsByLevelIdAndCompetitionId($levelId, $competitionId)
{
    $rawData = Yii::app()->db->createCommand("SELECT t.*,
         (SELECT COUNT(*)
    FROM tbl_competition_teams ct
    WHERE ct.team = t.id
    AND ct.competition = :competitionId) AS 'inCompetition'
    FROM tbl_teams t
    WHERE t.level = :levelId"
)->queryAll();

    $params = array(
        'levelId' => $levelId,
        'competitionId' => $competitionId
    );

    return new CArrayDataProvider($rawData, array(
    'id'=>'id',
        'sort'=>array(
        'attributes'=>array(
         'id', 'title', 'club', 'level', 'inCompetition',
    ),
),

));
}

但这给了我错误"CDbCommand未能执行SQL语句:SQLSTATE [HY093]:无效参数号:未绑定参数。"

这是我的团队表

1
2
3
4
5
6
7
CREATE TABLE `tbl_teams` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(35) NOT NULL DEFAULT '',
  `level` int(10) unsigned DEFAULT NULL,
  `club` int(10) unsigned DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2952 ;

和我的参赛队桌

1
2
3
4
5
6
7
CREATE TABLE `tbl_competition_teams` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `competition` int(10) unsigned NOT NULL DEFAULT '0',
  `team` int(10) unsigned NOT NULL DEFAULT '0',
  `seasonId` int(11) NOT NULL DEFAULT '3',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=126320 ;

提前一百万感谢您的任何建议!


也许问题是你 queryAll() 执行查询。此时您没有将值分配给您的参数

1
2
3
4
5
6
7
$rawData = Yii::app()->db->createCommand("SELECT t.*,
         (SELECT COUNT(*)
    FROM tbl_competition_teams ct
    WHERE ct.team = t.id
    AND ct.competition = $competitionId) AS 'inCompetition'
    FROM tbl_teams t
    WHERE t.level = $levelId"
)->queryAll();

您也可以尝试重写您的查询。

1
2
3
4
5
6
7
8
9
10
11
12
$query ="SELECT t.*,
         (SELECT COUNT(*)
            FROM tbl_competition_teams ct
            WHERE ct.team = t.id
            AND ct.competition = :competitionId) AS 'inCompetition'
        FROM tbl_teams t
        WHERE t.level = :levelId"
;

$command= Yii::app()->db->createCommand($query);
$command->bindValue(':levelId', $levelId);
$command->bindValue(':competitionId', $competitionId);
$rawData = $command->queryAll();

  • 谢谢,它现在像 $rawData = Yii::app()->db->createCommand('SELECT t.*, \\t\\t (SELECT COUNT(*) FROM tbl_competition_teams ct WHERE ct.team = t.id AND ct.competition = '.$competitionId.') AS inCompetition \\t\\tFROM tbl_teams t \\t\\tWHERE t.level = '.$levelId)->queryAll();

有关关于 mysql:Converting custom sql query in to a dataprovider yii?的更多相关文章

  1. 使用canal同步MySQL数据到ES - 2

    文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co

  2. ruby-on-rails - 无法安装 mysql2 0.3.14 gem - 2

    我看到其他人也遇到过类似的问题,但没有一个解决方案对我有用。0.3.14gem与其他gem文件一起存在。我已经完全按照此处指示完成了所有操作:https://github.com/brianmario/mysql2.我仍然得到以下信息。我不知道为什么安装程序指示它找不到include目录,因为我已经检查过它存在。thread.h文件存在,但不在ruby​​目录中。相反,它在这里:C:\RailsInstaller\DevKit\lib\perl5\5.8\msys\CORE\我正在运行Windows7并尝试在Aptana3中构建我的Rails项目。我的Ruby是1.9.3。$gemin

  3. ruby-on-rails - 关于 Ruby 的一般问题 - 2

    我在我的rails应用程序中安装了来自github.com的acts_as_versioned插件,但有一段代码我不完全理解,我希望有人能帮我解决这个问题class_eval我知道block内的方法(或任何它是什么)被定义为类内的实例方法,但我在插件的任何地方都找不到定义为常量的CLASS_METHODS,而且我也不确定是什么here,并且有问题的代码从lib/acts_as_versioned.rb的第199行开始。如果有人愿意告诉我这里的内幕,我将不胜感激。谢谢-C 最佳答案 这是一个异端。http://en.wikipedia

  4. ruby - 如何使用 ruby​​ mysql2 执行事务 - 2

    我已经开始使用mysql2gem。我试图弄清楚一些基本的事情——其中之一是如何明确地执行事务(对于批处理操作,比如多个INSERT/UPDATE查询)。在旧的ruby-mysql中,这是我的方法:client=Mysql.real_connect(...)inserts=["INSERTINTO...","UPDATE..WHEREid=..",#etc]client.autocommit(false)inserts.eachdo|ins|beginclient.query(ins)rescue#handleerrorsorabortentirelyendendclient.commi

  5. ruby - 我怎样才能更好地了解/了解更多关于 Ruby 的知识? - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。我最近开始学习Ruby,这是我的第一门编程语言。我对语法感到满意,并且我已经完成了许多只教授相同基础知识的教程。我已经写了一些小程序(包括我自己的数组排序方法,在有人告诉我谷歌“冒泡排序”之前我认为它非常聪明),但我觉得我需要尝试更大更难的东西来理解更多关于Ruby.关于如何执行此操作的任何想法?

  6. ruby - 关于 Ruby 中 Dir[] 和 File.join() 的混淆 - 2

    我在Ruby中遇到了一个关于Dir[]和File.join()的简单程序,blobs_dir='/path/to/dir'Dir[File.join(blobs_dir,"**","*")].eachdo|file|FileUtils.rm_rf(file)ifFile.symlink?(file)我有两个困惑:首先,File.join(@blobs_dir,"**","*")中的第二个和第三个参数是什么意思?其次,Dir[]在Ruby中有什么用?我只知道它等价于Dir.glob(),但是,我对Dir.glob()确实不是很清楚。 最佳答案

  7. elasticsearch源码关于TransportSearchAction【阶段三】 - 2

    1.回顾.TransportServicepublicclassTransportServiceextendsAbstractLifecycleComponentTransportService:方法:1publicfinalTextendsTransportResponse>voidsendRequest(finalTransport.Connectionconnection,finalStringaction,finalTransportRequestrequest,finalTransportRequestOptionsoptions,TransportResponseHandlerT>

  8. 关于Qt程序打包后运行库依赖的常见问题分析及解决方法 - 2

    目录一.大致如下常见问题:(1)找不到程序所依赖的Qt库version`Qt_5'notfound(requiredby(2)CouldnotLoadtheQtplatformplugin"xcb"in""eventhoughitwasfound(3)打包到在不同的linux系统下,或者打包到高版本的相同系统下,运行程序时,直接提示段错误即segmentationfault,或者Illegalinstruction(coredumped)非法指令(4)ldd应用程序或者库,查看运行所依赖的库时,直接报段错误二.问题逐个分析,得出解决方法:(1)找不到程序所依赖的Qt库version`Qt_5'

  9. ruby-on-rails - 当我通过 rvm 使用 rails3 时,如何在 ubuntu 上安装 mysql2 gem? - 2

    我正在尝试绕过rails配置这个极其复杂的迷宫。到目前为止,我设法在ubuntu上设置了rvm(出于某种原因,ruby在ubuntu存储库中已经过时了)。我设法建立了一个Rails项目。我希望我的测试项目使用mysql而不是mysqlite。当我尝试“rakedb:migrate”时,出现错误:“!!!缺少mysql2gem。将其添加到您的Gemfile:gem'mysql2'”当我尝试“geminstallmysql”时,出现错误,告诉我需要为安装命令提供参数。但是,参数列表很大,我不知道该选择哪些。如何通过在ubuntu上运行的rvm和mysql获取rails3?谢谢。

  10. ruby - 关于 Ruby/ChefSpec 编码风格的反馈 - 2

    我是Ruby的新手,但过去两周我一直在对Chef测试进行大量研究。该测试使用ChefSpec和Fauxhai,但它看起来不是很“像ruby”,我希望社区能给我一些编码风格的建议。有没有更好的方法来编写这样的嵌套循环?Recipe/foo/recipes/default.rbpackage"foo"doaction:installendRecipe/foo/spec/default_spec.rbrequire'chefspec'describe'foo::default'doplatforms={"debian"=>['6.0.5'],"ubuntu"=>['12.04','10.04

随机推荐