Doctrine2一对多 /多一对一的关系提供了重复的记录



我有三个表格1)产品类别(一个类别可以拥有许多产品)2)产品(一种产品应属于一个类别,可以具有0或许多图像)3)产品图像(一个图像应属于一个产品)

现在,我试图使产品从偏移0中施加8的限制。我的数据库中有6种产品。我想要的是,每个产品都有类别和图像的细节。为此,我使用学说2关联映射。

我的实体如下: -

    <?php
    namespace NitinTestBundleEntity;
    use DoctrineORMMapping as ORM;
    use SymfonyComponentValidatorConstraints as Assert;
    /**
     * NitinTestBundleEntityProduct
     *
     * @ORMTable(name="product", indexes={@ORMIndex(name="fk_product_cat", columns={"fk_product_cat"})})
     * @ORMEntity(repositoryClass="NitinTestBundleRepositoryProductRepository")
     */
    class Product
    {
        /**
         * @var integer
         *
         * @ORMColumn(name="id", type="integer", nullable=false)
         * @ORMId
         * @ORMGeneratedValue(strategy="IDENTITY")
         */
        private $id;
        /**
         * @var string
         *
         * @ORMColumn(name="product_title", type="string", length=250, nullable=false)
         * @AssertNotBlank(message = "Please enter valid product title.")
         */
        private $productTitle;
        /**
         * @var string
         *
         * @ORMColumn(name="description", type="text", nullable=true)
         */
        private $description;
        /**
         * @var float
         *
         * @ORMColumn(name="price", type="float", precision=10, scale=0, nullable=false)
         * @AssertNotBlank
         * AssertType(type="float", message="The value should have decimal points.") 
         */
        private $price;
        /**
         * @var DateTime
         *
         * @ORMColumn(name="created_date", type="datetime", nullable=false)
         */
        private $createdDate;
        /**
         * @var DateTime
         *
         * @ORMColumn(name="modified_date", type="datetime", nullable=false)
         */
        private $modifiedDate;
        /**
         * @var boolean
         *
         * @ORMColumn(name="featured", type="boolean", precision=0, scale=0, nullable=false, unique=false)
         */
        private $featured;
        /**
         * @var NitinTestBundleEntityProductCategory
         *
         * @ORMManyToOne(targetEntity="NitinTestBundleEntityProductCategory", cascade={"persist", "remove"})
         * @ORMJoinColumns({
         *   @ORMJoinColumn(name="fk_product_cat", referencedColumnName="id",  onDelete="CASCADE")
         * })
         * @AssertNotBlank(message = "Please Select Product Category.")
         */
        private $fkProductCat;
        /**
         * @var DoctrineCommonCollectionsCollection
         * 
         * @ORMOneToMany(targetEntity="NitinTestBundleEntityProductImages", mappedBy="fkProduct", cascade={"persist"})
         * 
         */
        private $images;

产品类别实体的代码是: -

    <?php
    namespace NitinTestBundleEntity;
    use DoctrineORMMapping as ORM;
    use SymfonyComponentValidatorConstraints as Assert;
    /**
     * NitinTestBundleEntityProductCategory
     *
     * @ORMTable(name="product_category", indexes={@ORMIndex(name="fk_parent", columns={"fk_parent"})})
     * @ORMEntity(repositoryClass="NitinTestBundleRepositoryProductCategoryRepository")
     */
    class ProductCategory
    {
        /**
         * @var integer
         *
         * @ORMColumn(name="id", type="integer", nullable=false)
         * @ORMId
         * @ORMGeneratedValue(strategy="IDENTITY")
         */
        private $id;
        /**
         * @var string
         *
         * @ORMColumn(name="category_name", type="string", length=250, nullable=false)
         * @AssertNotBlank
         */
        private $categoryName;
        /**
         * @var string
         *
         * @ORMColumn(name="description", type="text", nullable=false)
         */
        private $description;
        /**
         * @var DateTime
         *
         * @ORMColumn(name="created_date", type="datetime", nullable=true)
         */
        private $createdDate;
        /**
         * @var DateTime
         *
         * @ORMColumn(name="modified_date", type="datetime", nullable=false)
         */
        private $modifiedDate;
        /**
         * @var NitinTestBundleEntityProductCategory
         *
         * @ORMManyToOne(targetEntity="NitinTestBundleEntityProductCategory", cascade={"persist", "remove"})
         * @ORMJoinColumns({
         *   @ORMJoinColumn(name="fk_parent", referencedColumnName="id", onDelete="CASCADE")
         * })
         */
        private $fkParent;
        /**
         * @var DoctrineCommonCollectionsCollection
         *
         * @ORMOneToMany(targetEntity="NitinTestBundleEntityProductCategory", mappedBy="fkParent", cascade={"persist"})
         */
        private $child;

产品图像代码实体是

    <?php
    namespace NitinTestBundleEntity;
    use DoctrineORMMapping as ORM;
    use SymfonyComponentHttpFoundationFileUploadedFile;
    use SymfonyComponentValidatorConstraints as Assert;
    /**
     * ProductImages
     *
     * @ORMTable(name="product_images", indexes={@ORMIndex(name="fk_product", columns={"fk_product"})})
     * @ORMEntity
     * @ORMHasLifecycleCallbacks
     */
    class ProductImages {
        /**
         * @var integer
         *
         * @ORMColumn(name="id", type="integer", nullable=false)
         * @ORMId
         * @ORMGeneratedValue(strategy="IDENTITY")
         */
        private $id;
        /**
         * @var string
         *
         * @ORMColumn(name="title", type="string", length=255, nullable=false)
         */
        private $title;
        /**
         * @ORMColumn(type="string", length=255, nullable=true)
         */
        private $path;
        /**
         * @var DateTime
         *
         * @ORMColumn(name="created_date", type="datetime", nullable=false)
         */
        private $createdDate;
        /**
         * @var DateTime
         *
         * @ORMColumn(name="modified_date", type="datetime", nullable=false)
         */
        private $modifiedDate;
        /**
         * @var NitinTestBundleEntityProduct
         * 
         * @ORMManyToOne(targetEntity="NitinTestBundleEntityProduct", inversedBy="id", cascade={"merge", "remove"})
         * @ORMJoinColumns({
         *   @ORMJoinColumn(name="fk_product", referencedColumnName="id", onDelete="CASCADE")
         * })
         */
        private $fkProduct;
        /**
         * @AssertFile(maxSize="6000000")
         */
        private $file;

为了获得结果,我正在通过存储库中的自定义函数使用学说查询构建器: -

            <?php
    namespace NitinTestBundleRepository;
    use DoctrineORMEntityRepository;
    /**
     * ProductRepository
     *
     * This class was generated by the Doctrine ORM. Add your own custom
     * repository methods below.
     */
    class ProductRepository extends EntityRepository {
    public function getAllProduct($offset = 0, $limit = 10, $arrayResult = true) {
            $qb = $this->_em->createQueryBuilder();
            $qb->select(array('p', 'pc', 'img'))
                    ->from($this->_entityName, 'p')
                    ->leftJoin('p.fkProductCat', 'pc')
                    ->leftJoin('p.images', 'img')
                    //->add('where ', $qb->expr()->countDistinct('p.id'))
                    ;

            //Pagination logic
            $from = (int) $offset;
            $start = ($from == 1) ? ($from - 1) : (($from - 1) * $limit );
            $start = ($start < 0) ? 0 : $start;
            $qb->setFirstResult($start);
            $qb->setMaxResults($limit);
            //echo $qb->getQuery()->getSQL();die;
            if (TRUE === $arrayResult) {
                return $qb->getQuery()->getArrayResult();
            }
            return $qb->getQuery()->getResult();
        }

并在这样的控制器中调用函数: -

    <?php
    namespace NitinTestBundleController;
    use SymfonyBundleFrameworkBundleControllerController;
    class IndexController extends Controller
    {
        private $data = array();
        public function indexAction()
        {
            $em = $this->getDoctrine()->getEntityManager();
            $this->data['latestProduct'] = $em->getRepository('BitcoinAdminBundle:Product')->getAllProduct(0, 8);
            return $this->render('BitcoinSiteBundle:Default:index.html.twig', $this->data);
        }
    }

我期望有6个记录,但只能得到3。

所以,任何人都可以建议我在哪里犯错。

谢谢

尝试在setMaxResults();之后立即添加return $paginator = new Paginator($qb->getQuery, $fetchJoinCollection = true);。加入集合时,这是一个常见的问题。

相关内容

  • 没有找到相关文章