类别和产品之间的关系

时间:2016-09-13 12:19:16

标签: php mysql symfony doctrine-orm twig

我有两个链接到表格帖子的类别。每个类别都包含机场和城市名称列表。

一个城市可以拥有多个机场,我希望当用户点击前面的类别:[巴黎 - 纽约]时,这两个城市之间的所有航班(机场)都必须是显示。

现在,如果用户点击[巴黎 - 纽约],我所做的只有从纽约到各个方向的所有航班都会显示,我不想要那样。

这就是我的CategoriesArrivalCategoriesDeparture的样子:

--------------------------------------------------
| id | name       | city                         |
--------------------------------------------------
|  1  |Kennedy    | New York                     |
--------------------------------------------------
|  2  |La Guardia | New York                     |
--------------------------------------------------
|  3  |Charles de Gaulle| Paris                  |
--------------------------------------------------
|  4  |Orly       | Paris                        |
--------------------------------------------------
|  5  |Cape Town  | Cape Town                    |
--------------------------------------------------

CategoriesArrival.php

<?php

namespace FLY\BookingsBundle\Entity;
use FLY\BookingsBundle\Entity\Post;
use Doctrine\ORM\Mapping as ORM;

/**
 * CategoriesArrival
 *
 * @ORM\Table(name="CategoriesArrival", indexes={@ORM\Index(name="name", columns={"name"})})
 * @ORM\Entity(repositoryClass="FLY\BookingsBundle\Entity\CategoriesArrivalRepository")
 * @ORM\Entity
 */
class CategoriesArrival
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\OneToMany(targetEntity="Post", mappedBy="airport1")
     *
     */
    private $ItemCategories;
    /**
     * @var string
     *
     * @ORM\Column(name="city", type="string", length=255)
     */
    private $city;
    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=125)
     */
    private $name;
    /**
     * @var string
     *
     * @ORM\Column(name="code", type="string", length=4, nullable=true)
     */
    private $code;

    public function __toString()
    {
        return $this->name;
    }

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->ItemCategories = new \Doctrine\Common\Collections\ArrayCollection();
    }









    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return CategoriesArrival
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set city
     *
     * @param string $city
     * @return CategoriesArrival
     */
    public function setCity($city)
    {
        $this->city = $city;

        return $this;
    }

    /**
     * Get city
     *
     * @return string 
     */
    public function getCity()
    {
        return $this->city;
    }



    /**
     * Set code
     *
     * @param string $code
     * @return CategoriesArrival
     */
    public function setCode($code)
    {
        $this->code = $code;

        return $this;
    }

    /**
     * Get code
     *
     * @return string 
     */
    public function getCode()
    {
        return $this->code;
    }



    /**
     * Add ItemCategories
     *
     * @param \FLY\BookingsBundle\Entity\Post $itemCategories
     * @return CategoriesArrival
     */
    public function addItemCategory(\FLY\BookingsBundle\Entity\Post $itemCategories)
    {
        $this->ItemCategories[] = $itemCategories;

        return $this;
    }

    /**
     * Remove ItemCategories
     *
     * @param \FLY\BookingsBundle\Entity\Post $itemCategories
     */
    public function removeItemCategory(\FLY\BookingsBundle\Entity\Post $itemCategories)
    {
        $this->ItemCategories->removeElement($itemCategories);
    }

    /**
     * Get ItemCategories
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getItemCategories()
    {
        return $this->ItemCategories;
    }
}

CategoriesDeparture.php外观像CategoriesArrival.php

post.php中

/**
 *
 *
 * @ORM\ManyToOne(targetEntity="CategoriesDeparture", inversedBy="ItemCategories")
 * @ORM\joinColumn(referencedColumnName="id")
 *
 *
 */
private $airport;



/**
 *
 *
 * @ORM\ManyToOne(targetEntity="CategoriesArrival", inversedBy="ItemCategories")
 * @ORM\joinColumn(referencedColumnName="id")
 *
 *
 */
private $airport1;

PostController.php

public function categoryAction($entity)
{

    $em = $this->getDoctrine()->getManager();
    $entities = $em->getRepository('FLYBookingsBundle:Post')->byCategory($entity);
    return $this->render('FLYBookingsBundle:Post:category.html.twig', array('entities' => $entities));
}


public function category1Action($id)
{
    $em = $this->getDoctrine()->getManager();
    $entities = $em->getRepository('FLYBookingsBundle:CategoriesArrival')->findBy(array('id' => $id));
    return $this->render('FLYBookingsBundle:Post:category1.html.twig', array('entities' => $entities));
}

category1.twig.html

   {% for entity in entities %}


        <li>
            <a class="cd-nav-item" href="{{ path('categorieProducts', { 'entity' : entity.id, 'slug':entity.city}) }}">
                <img src="{{  asset('bundles/flyplatform/img/blog1.jpg')}}">
                <h3>{{ entity.city }}</h3>
            </a>
        </li>

    {% endfor %}

index.html.twig

{% render (controller('FLYBookingsBundle:Post:category1', { 'id':  2 })) %}

的routing.yml

categorieProducts:
     pattern:  /category/{entity}
     defaults: { _controller: FLYBookingsBundle:Post:category }

PostRepository.php

public function byCategory($entity)
{
    $qb = $this->createQueryBuilder('u')
        ->select('u')
        ->where('u.airport1 = :airport1')
        ->orderBy('u.id')
        ->setParameter('airport1', $entity);
    return $qb->getQuery()->getResult();
}

ADD:

Model

0 个答案:

没有答案