Doctrine Query Builder返回意外的Proxies \ __ CG __ \ objects

时间:2015-03-08 11:19:46

标签: symfony doctrine-orm

我试图加入两个具有一对一关系的简单表格。我的问题是查询生成器返回的原始结果是一个由2种不同类型的对象组成的数组:

Proxies\__CG__\Azphotos\PhotoBundle\Entity\PhotoCategories
Azphotos\PhotoBundle\Entity\PhotoGallery

我只想在结果中使用Azphotos\PhotoBundle\Entity\PhotoGallery类型的对象。

表" 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字段是FK引用的photo_categories.aid。 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实体的片段:

/**
 * PhotoGallery
 *
 * @ORM\Table(name="photo_gallery", indexes={@ORM\Index(name="main_category_id", columns={"main_category_id"})})
 * @ORM\Entity(repositoryClass="Azphotos\PhotoBundle\Entity\PhotoGalleryRepository")
 */
class PhotoGallery
{

    /**
     * @var \Azphotos\PhotoBundle\Entity\PhotoCategories
     *
     * @ORM\OneToOne(targetEntity="Azphotos\PhotoBundle\Entity\PhotoCategories")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(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(
            'Azphotos\PhotoBundle\Entity\PhotoCategories',
            'photoCat',
            \Doctrine\ORM\Query\Expr\Join::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) == 'Azphotos\PhotoBundle\Entity\PhotoGallery') {
                $resultRevised[] = $photo;
            }
        }

        return $resultRevised;
    } catch (\Doctrine\ORM\NoResultException $e) {
        return null;
    }
}

如你所见,我循环遍历$result = $qb->getQuery()->getResult();仅包含Azphotos\PhotoBundle\Entity\PhotoGallery类型的对象。

当我查看使用Symfony2探查器运行的查询时,我发现本机MySQL查询绝对正确。

为什么我的原始结果被Proxies\__CG__\Azphotos\PhotoBundle\Entity\PhotoCategories个对象污染了,我在这里做错了什么?

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

据我所知,当我们为该存储库类(在您的案例中为Photo Gallery Repository)编写DQL时,具有连接关联的实体,关联实体(在您的情况下为Photo Categories)在代理类上延迟加载。所以,这可能就是你获得代理类的原因。