我有以下情况:名为HRMgmt的表包含由另一个用户引起的发生在用户身上的事件,所以在我的实体类中我有:
/**
* @ORM\ManyToOne(targetEntity="User")
*/
protected $user;
/**
* @ORM\ManyToOne(targetEntity="User")
*/
protected $changed_by;
我想进行双连接以获取与此事件相关的用户的id,name和surname以及导致HRMgmt事件的用户的id,name和surname(名为changed_by)
我的查询构建如下:
$events = $this->getEntityManager()
->createQuery(
'SELECT u1.id currentuserid, u1.name currentusername, u1.surname currentusersurname, u2.id changedbyuserid, u2.name changedbyusername, u2.surname changedbyusersurname, h.is_active, h.timestamp, h.started_at, h.finished_at, r.name rolename FROM AppBundle:HRMgmtEvent h, AppBundle:Role r
INNER JOIN h.user u1
INNER JOIN h.changed_by u2
WHERE h.project = :projectid
AND h.role = r.id
ORDER BY h.timestamp DESC, h.is_active DESC'
)
->setParameter('projectid', $project->getId())
->getResult();
但抛出以下错误 未找到列:1054未知列' h2_.user_id'在' on条款'
联接是否正确执行?我在这里缺少什么?
问题类似于以下内容:JOIN DQL (symfony2) multiple joins between two tables
实际上我的情况更简单,因为我不能有空值......
我的数据库与运行的实体同步:
php app/console doctrine:schema:update --force
编辑:
这是HRMgmt的架构:
CREATE TABLE `hrmgmt` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`project_id` int(11) DEFAULT NULL,
`changed_by_id` int(11) DEFAULT NULL,
`started_at` datetime DEFAULT NULL,
`finished_at` datetime DEFAULT NULL,
`is_active` tinyint(1) NOT NULL,
`timestamp` datetime NOT NULL,
`role_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `IDX_B3C74883A76ED395` (`user_id`),
KEY `IDX_B3C74883828AD0A0` (`changed_by_id`),
KEY `IDX_B3C74883166D1F9C` (`project_id`),
KEY `IDX_B3C74883D60322AC` (`role_id`),
CONSTRAINT `FK_B3C74883166D1F9C` FOREIGN KEY (`project_id`) REFERENCES `projects` (`id`),
CONSTRAINT `FK_B3C74883828AD0A0` FOREIGN KEY (`changed_by_id`) REFERENCES `fos_user` (`id`),
CONSTRAINT `FK_B3C74883A76ED395` FOREIGN KEY (`user_id`) REFERENCES `fos_user` (`id`),
CONSTRAINT `FK_B3C74883D60322AC` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1042 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
以下是用户:
CREATE TABLE `fos_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
...several other fields...
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`surname` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UNIQ_957A647992FC23A8` (`username_canonical`),
UNIQUE KEY `UNIQ_957A6479A0D96FBF` (`email_canonical`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
谢谢!
答案 0 :(得分:0)
这两个属性都映射到数据库user_id
上的相同字段,这是DocTine在ManyToOne关联(model_id)上的默认映射。要解决这个问题,您应该为每个属性选择不同的列名,如下所示:
/**
* @ORM\ManyToOne(targetEntity="User")
* @ORM\JoinColumn(name="user_id")
*/
protected $user;
/**
* @ORM\ManyToOne(targetEntity="User")
* @ORM\JoinColumn(name="changed_by_id")
*/
protected $changed_by;
之后你应该能够加入。