我有以下的情況:一個名爲HRMgmt表包含事件發生在用戶,造成其他用戶,所以在我的實體類,我有:的Symfony2/Doctrine2雙重加入
/**
* @ORM\ManyToOne(targetEntity="User")
*/
protected $user;
/**
* @ORM\ManyToOne(targetEntity="User")
*/
protected $changed_by;
我希望做一個雙重加入讓這個事件與和ID,姓名和誰造成的HRMgmt事件(被稱爲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'on子句'中的未知列'h2_.user_id'
連接是否正常執行?我在這裏錯過了什麼?
的問題是類似以下內容:JOIN DQL (symfony2) multiple joins between two tables
其實我的情況是即使我不能空值簡單...
我的DB是同步的具有運行我的實體:
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
謝謝!
你能告訴我們你的數據庫架構'AppBundle:HRMgmtEvent'和'AppBundle:User'實體嗎? –