Symfony2 SonataClassificationBundle从数据库查询

时间:2015-04-17 11:11:06

标签: php mysql symfony

我正在尝试查询动态类别侧边栏,我正在使用Sonata ClassificationBundle。我创建了一些类别(父级)和一些子类别(子级)。我能够显示类别,但是我无法理解如何查询特定类别下的子类别。我想我需要检查该类别是否有孩子并显示它?如果没有适当的文档,我不知道怎么做。

这是侧边栏控制器:

<?php

namespace Mp\ShopBundle\Controller;

use Sonata\ClassificationBundle\Entity\Category;
use Sonata\ClassificationBundle\Model\CategoryInterface;
use Sonata\Component\Currency\CurrencyDetector;
use Sonata\Component\Product\Pool;
use Sonata\ProductBundle\Entity\ProductSetManager;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class SidebarController extends Controller
{
    /**
     * @  Route("/hello/{name}")
     * @  Template()
     */
    public function sidebarAction()
    {
        $page        = $this->getRequest()->get('page', 1);
        $displayMax  = $this->getRequest()->get('max', 9);
        $displayMode = $this->getRequest()->get('mode', 'grid');
        $filter      = $this->getRequest()->get('filter');
        $option      = $this->getRequest()->get('option');

        if (!in_array($displayMode, array('grid'))) { // "list" mode will be added later
            throw new NotFoundHttpException(sprintf('Given display_mode "%s" doesn\'t exist.', $displayMode));
        }

        $category = $this->retrieveCategoryFromQueryString();

        return $this->render('sidebar.html.twig', array(
            'display_mode' => $displayMode,         
            'category'   => $this->getCategoryManager()->findBy(
            array('parent' => 1 )),         
            'subcategory' => $this->getCategoryManager()->findBy(
            array('children' => true )),    ////// ERROR You cannot search for the association field 'Application\Sonata\ClassificationBundle\Entity\Category#children', because it is the inverse side of an association. Find methods only work on owning side associations."
            'provider'     => $this->getProviderFromCategory($category),
        ));
    }


             /*  $em = $this->getDoctrine()->getManager();
            $products = $em->getRepository('MpShopBundle:Product')->findAll();
               return $this->render('sidebar.html.twig',  array(
               'products'=>$products       
               )); */

    /**
     * Retrieve Category from its id and slug, if any.
     *
     * @return CategoryInterface|null
     */
    protected function retrieveCategoryFromQueryString()
    {
        $categoryId   = $this->getRequest()->get('category_id');
        $categorySlug = $this->getRequest()->get('category_slug');

        if (!$categoryId || !$categorySlug ) {
            return null;
        }

        return $this->getCategoryManager()->findOneBy(array(
            'id'      => $categoryId,
            'enabled' => true,
        ));
    }

这是我尝试显示侧边栏的方式:

{% for categories in category %}

            {% for subcategories in subcategory %}

                <li class="subMenu"><a> {{ categories.name }} [{{ categories|length }}]</a>
                <ul>

                    <li><a href="{{ path('products') }}">{{ subcategories.name }} ({{ categories|length }})</a></li>

                </ul>
                </li>

            {% endfor %}

            {% endfor %}

类别实体:

<?php

/*
 * This file is part of the Sonata project.
 *
 * (c) Sonata Project <https://github.com/sonata-project/SonataClassificationBundle/>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Sonata\ClassificationBundle\Model;

use Sonata\MediaBundle\Model\MediaInterface;

interface CategoryInterface
{
    /**
     * @param $name
     *
     * @return mixed
     */
    public function setName($name);

    /**
     * Get name
     *
     * @return string $name
     */
    public function getName();

    /**
     * Set enabled
     *
     * @param boolean $enabled
     */
    public function setEnabled($enabled);

    /**
     * Get enabled
     *
     * @return boolean $enabled
     */
    public function getEnabled();

    /**
     * Set slug
     *
     * @param integer $slug
     */
    public function setSlug($slug);

    /**
     * Get slug
     *
     * @return integer $slug
     */
    public function getSlug();

    /**
     * Set description
     *
     * @param string $description
     */
    public function setDescription($description);

    /**
     * Get description
     *
     * @return string $description
     */
    public function getDescription();

    /**
     * @param integer $position
     */
    public function setPosition($position);

    /**
     * @return integer
     */
    public function getPosition();

    /**
     * Add Children
     *
     * @param CategoryInterface $children
     * @param boolean           $nested
     */
    public function addChild(CategoryInterface $children, $nested = false);

    /**
     * Get Children
     *
     * @return \Doctrine\Common\Collections\Collection $children
     */
    public function getChildren();

    /**
     * Set children
     *
     * @param $children
     */
    public function setChildren($children);

    /**
     * Return true if category has children
     *
     * @return boolean
     */
    public function hasChildren();

    /**
     * Set Parent
     *
     * @param CategoryInterface $parent
     * @param boolean           $nested
     */
    public function setParent(CategoryInterface $parent = null, $nested = false);

    /**
     * Get Parent
     *
     * @return CategoryInterface $parent
     */
    public function getParent();

    /**
     * @param MediaInterface $media
     */
    public function setMedia(MediaInterface $media = null);

    /**
     * @return MediaInterface
     */
    public function getMedia();

    /**
     * @param ContextInterface $context
     */
    public function setContext(ContextInterface $context);

    /**
     * @return ContextInterface
     */
    public function getContext();
}

我的想法 好的,因为所有父类别的parent_id都是1,所以我很容易显示它们。子类别的parent_id与父类别的id相同。是否有可能以某种方式查询这种情况?像parent_id = id?

1 个答案:

答案 0 :(得分:0)

您应该在每个父类别上使用getChildren():实体应该是水合的,因此查询已经为您完成。