>我正在尝试连接 2 个具有一对一关系的简单表。我的问题是查询生成器返回的原始结果是一个由 2 种不同类型的对象组成的数组:
Proxies__CG__AzphotosPhotoBundleEntityPhotoCategories
AzphotosPhotoBundleEntityPhotoGallery
我只想在结果中包含AzphotosPhotoBundleEntityPhotoGallery
类型的对象。
表"photo_gallery":
CREATE TABLE `photo_gallery` (
`aid` int(10) NOT NULL AUTO_INCREMENT,
`string_id` varchar(255) DEFAULT NULL,
`title` varchar(255) NOT NULL DEFAULT '',
`main_category_id` int(11) NOT NULL,
`photographer_id` mediumint(9) DEFAULT NULL,
`main_media` varchar(255) DEFAULT NULL,
`content` text NOT NULL,
`date_taken` date DEFAULT NULL,
`place_taken` varchar(255) DEFAULT NULL,
`tags` varchar(255) DEFAULT NULL,
`position` int(11) NOT NULL,
`allow_comments` enum('true','false') NOT NULL DEFAULT 'true',
`active` enum('true','false') NOT NULL DEFAULT 'true',
`views` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`aid`),
KEY `main_category_id` (`main_category_id`),
CONSTRAINT `photo_gallery_ibfk_1` FOREIGN KEY (`main_category_id`) REFERENCES `photo_categories` (`aid`) ON DELETE NO ACTION ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=336 DEFAULT CHARSET=utf8;
表"photo_categories":
CREATE TABLE `photo_categories` (
`aid` int(10) NOT NULL AUTO_INCREMENT,
`string_id` varchar(255) DEFAULT NULL,
`head_category` int(11) NOT NULL,
`title` varchar(255) NOT NULL DEFAULT '',
`state` varchar(255) DEFAULT NULL,
`country` varchar(255) NOT NULL,
`content` text NOT NULL,
`position` int(11) NOT NULL,
`tags` varchar(255) DEFAULT NULL,
`active` enum('true','false') NOT NULL DEFAULT 'true',
PRIMARY KEY (`aid`)
) ENGINE=InnoDB AUTO_INCREMENT=64 DEFAULT CHARSET=utf8;
如您所见photo_gallery.main_category_id 字段是引用 photo_categories.aid 的 FK。PhotoGallery.orm 中的架构片段.xml引用此关系:
<one-to-one field="mainCategory" target-entity="PhotoCategories">
<join-columns>
<join-column name="main_category_id" referenced-column-name="aid"/>
</join-columns>
</one-to-one>
来自照片库实体的片段:
/**
* PhotoGallery
*
* @ORMTable(name="photo_gallery", indexes={@ORMIndex(name="main_category_id", columns={"main_category_id"})})
* @ORMEntity(repositoryClass="AzphotosPhotoBundleEntityPhotoGalleryRepository")
*/
class PhotoGallery
{
/**
* @var AzphotosPhotoBundleEntityPhotoCategories
*
* @ORMOneToOne(targetEntity="AzphotosPhotoBundleEntityPhotoCategories")
* @ORMJoinColumns({
* @ORMJoinColumn(name="main_category_id", referencedColumnName="aid")
* })
*/
private $mainCategory;
我有一个 PhotoGalleryRepository 类,我在其中使用查询构建器来连接这两个表:
public function findLatest($params, $keyword = false, $filter_by = false) {
[...some irrelevant code here...]
$qb = $this->createQueryBuilder('photoGallery');
$qb->select(array('photoGallery', 'photoCat'))
->innerJoin(
'AzphotosPhotoBundleEntityPhotoCategories',
'photoCat',
DoctrineORMQueryExprJoin::WITH,
'photoGallery.mainCategory = photoCat.aid'
)
->where('photoGallery.active = ?1')
->andWhere('photoCat.active = ?2');
[...some irrelevant code here...]
$qb->setParameter(1, 'true')
->setParameter(2, 'true')
->orderBy($params['orderBy'], 'DESC');
if (isset($params['offset']) && isset($params['limit'])) {
$qb->setFirstResult($params['offset'])->setMaxResults($params['limit']);
}
try {
$result = $qb->getQuery()->getResult();
$resultRevised = array();
foreach ($result AS $photo) {
//this is the lame part
if (get_class($photo) == 'AzphotosPhotoBundleEntityPhotoGallery') {
$resultRevised[] = $photo;
}
}
return $resultRevised;
} catch (DoctrineORMNoResultException $e) {
return null;
}
}
如您所见,我正在循环$result = $qb->getQuery()->getResult()
;只包含AzphotosPhotoBundleEntityPhotoGallery
类型的对象。
当我查看使用Symfony2分析器运行的查询时,我发现本机MySQL查询是绝对正确的。
为什么我的原始结果被Proxies__CG__AzphotosPhotoBundleEntityPhotoCategories
物体污染,我在这里做错了什么?
任何帮助都非常感谢。
据我所知,当我们为该存储库类(在您的示例中为照片库存储库)编写 DQL 时,具有联接关联的实体,关联的实体(在您的情况下是照片类别)是延迟加载到代理类上。因此,这可能就是您获得代理类的原因。