我有两个简单的实体:
My\Entity\Coupon:
type: entity
table: coupon
id:
id:
type: integer
generator:
strategy: AUTO
fields:
name:
type: string
length: 255
nullable: false
value:
type: integer
default: 0
My\Entity\CouponUsers:
type: entity
table: coupon_users
id:
id:
type: integer
length: 11
nullable: false
generator:
strategy: AUTO
fields:
coupon_id:
type: integer
length: 11
nullable: false
user_id:
type: integer
现在,我想显示已用优惠券的简单统计信息。
在 phpMyAdmin 中运行此 SQL:
SELECT c.name, count( * ) AS mycount
FROM coupon c
LEFT JOIN coupon_users u ON c.id = u.coupon_id
GROUP BY c.id
ORDER BY mycount DESC
按预期工作正常,返回:
name1 54
name2 120
然后,我尝试从 Doctrine 2 做同样的事情:
$queryBuilder = $this->_em->createQueryBuilder()
->select('c.name, COUNT(*) as co')
->from('My\Entity\Coupon', 'c')
->leftJoin('My\Entity\CouponUsers', 'u',
\Doctrine\ORM\Query\Expr\Join::ON, 'c.id = u.coupon_id')
->where('u.coupon_id = c.id')
->groupBy('c.id');
$dql = $queryBuilder->getDQL();
var_dump($dql);
SELECT c.name,
COUNT(*) as co
FROM My\Entity\Coupon c
LEFT JOIN My\Entity\CouponUsers u
ON c.id = u.coupon_id
WHERE u.coupon_id = c.id
GROUP BY c.id
到目前为止,还不错。但是当我这样做时:
$queryBuilder->getQuery()->getResult();
我得到错误:
[Syntax Error] line 0, col 88: Error: Expected Doctrine\ORM\Query\Lexer::T_DOT, got 'u'
怎么了?我该如何解决这个问题?
最佳答案
以下是 Doctrine 手册建议如何编码您的查询:
$querybuilder = $this->_em->createQueryBuilder()
->select(array('c.name', 'COUNT(c.id) as co')
->from('My\Entity\Coupon', 'c')
->leftJoin('c.users', 'u')
->groupBy('c.id');
要在 QueryBuilder 中执行此连接,您需要在两个实体之间配置双向关联,您似乎还没有设置。
我为我的实体使用注释,但我认为 YAML 看起来像这样:
My\Entity\Coupon:
manyToOne:
users:
targetentity: CouponUsers
inversed-by: coupon
My\Entity\CouponUsers:
onetoMany:
coupon:
targetEntity: Coupon
mapped-by: users
如果用户可以拥有多张优惠券,那么关系将是双向 ManyToMany 而不是 manytoOne/oneToMany。有关如何配置的详细信息,请参见 here .
关于mysql - 左加入学说 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8159069/
Arel3.0.2提供了两个类来指定连接类型:Arel::Nodes::InnerJoin和Arel::Nodes::OuterJoin并使用InnerJoin默认。foo=Arel::Table.new('foo')bar=Arel::Table.new('bar')foo.join(bar,Arel::Nodes::InnerJoin)#innerfoo.join(bar,Arel::Nodes::OuterJoin)#outerfoo.join(bar,???)#left如果要生成左连接,如何连接两个表? 最佳答案 你可以使用
文章目录一、概述简介原理模块二、配置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
我看到其他人也遇到过类似的问题,但没有一个解决方案对我有用。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
我已经开始使用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
我正在尝试绕过rails配置这个极其复杂的迷宫。到目前为止,我设法在ubuntu上设置了rvm(出于某种原因,ruby在ubuntu存储库中已经过时了)。我设法建立了一个Rails项目。我希望我的测试项目使用mysql而不是mysqlite。当我尝试“rakedb:migrate”时,出现错误:“!!!缺少mysql2gem。将其添加到您的Gemfile:gem'mysql2'”当我尝试“geminstallmysql”时,出现错误,告诉我需要为安装命令提供参数。但是,参数列表很大,我不知道该选择哪些。如何通过在ubuntu上运行的rvm和mysql获取rails3?谢谢。
目录1、yum安装mysql修改密码(1)在mysql里面修改(2)第二种方式,利用mysqladmin修改密码2、没有密码,登录mysql修改密码3、mysql的安全设置1、yum安装mysql在CentOS中默认安装有MariaDB(MySQL的一个分支),安装完成之后可以直接覆盖MariaDB。rpm-qa|grepmariadb查询是否安装了mariadbrpm-e--nodepsmariadb-libs-5.5.60-1.el7_5.x86_64卸载mariadwgethttp://dev.mysql.com/get/mysql57-community-release-el7-11.
我是Ruby的新手。我安装了DataMapper并且正在尝试安装dm-mysql-adapter-1.0.2gem。但是当我尝试安装时,出现以下错误。我正在使用ubuntu操作系统。vinoth@vinoth-laptop:~/Downloads$geminstalldm-mysql-adapter-1.0.2----with-mysql-lib=/usr/lib/mysql----with-mysql-conf=/usr/bin/mysqlWARNING:Installingto~/.gemsince/home/vinoth/gemsand/home/vinoth/gems/bina
我目前正在构建一个需要mysql2gem的RoR项目。我成功安装了gem。因为它出现在我的gem列表中。[root@vc2cmmka035538nsimple_cms]#gemlist***LOCALGEMS***actionmailer(3.2.3)actionpack(3.2.3)activemodel(3.2.3)activerecord(3.2.3)activeresource(3.2.3)activesupport(3.2.14,3.2.3)arel(3.0.2)bigdecimal(1.1.0)builder(3.2.2,3.0.0)bundler(1.1.5)c2c_li
我想使用托管在我自己服务器上的mysql数据库。我已经更改了DATABASE_URL和SHARED_DATABASE_URL配置变量以指向我的服务器,但它仍在尝试连接到heroku的amazonaws服务器。我该如何解决? 最佳答案 根据Herokudocumentation,更改DATABASE_URL是正确的方法。Ifyouwouldliketohaveyourrailsapplicationconnecttoanon-Herokuprovideddatabase,youcantakeadvantageofthissamemec
使用mysql2做查询总是得到警告/usr/local/lib/ruby/gems/1.9.1/gems/mysql2-0.2.6/lib/active_record/connection_adapters/mysql2_adapter.rb:463:warning::database_timezoneoptionmustbe:utcor:local-defaultingto:local我确实看到了时区选项Mysql2现在支持两个时区选项::database_timezone-thisisthetimezoneMysql2willassumefieldsarealreadystored