Search code examples
symfonydoctrine-ormsymfony-2.3

What I'm missing in my Entities in order to get content values from product_has_product_detail and label from product detail having product_id


I have this DB model:

enter image description here

I need to get the value of product_has_product_detail.content and product_detail.label just having product.id. This are my entities:

namespace ProductBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * @ORM\Entity
 * @ORM\Table(name="product")
 */
class Product {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    protected $name;

    /**
     * @ORM\Column(type="text")
     */
    protected $description;

    /**
     * @ORM\Column(type="smallint")
     */
    protected $age_limit;

    /**
     * @Gedmo\Timestampable(on="create")
     * @ORM\Column(name="created", type="datetime")
     */
    protected $created;

    /**
     * @Gedmo\Timestampable(on="update")
     * @ORM\Column(name="modified", type="datetime")
     */
    protected $modified;

    /**
     * @ORM\ManyToMany(targetEntity="CategoryBundle\Entity\Category", inversedBy="products")
     * @ORM\JoinTable(name="product_has_category")
     */
    protected $categories;

    /**
     * @ORM\ManyToMany(targetEntity="ProductBundle\Entity\ProductDetail", inversedBy="products_details")
     * @ORM\JoinTable(name="product_has_product_detail")
     */
    protected $details;

    /**
     * @ORM\OneToMany(targetEntity="StockBundle\Entity\KStock", mappedBy="product")
     */
    protected $stocks;

    /**
     * @ORM\OneToMany(targetEntity="ProductBundle\Entity\ProductHasMedia", mappedBy="product")
     */
    protected $medias;

    /**
     * @ORM\ManyToOne(targetEntity="ProductBundle\Entity\NBrand")
     * @ORM\JoinColumn(name="brand", referencedColumnName="id")
     * */
    private $brand;

    public function __construct() {
        $this->categories = new \Doctrine\Common\Collections\ArrayCollection();
        $this->details = new \Doctrine\Common\Collections\ArrayCollection();
        $this->stocks = new \Doctrine\Common\Collections\ArrayCollection();
        $this->medias = new \Doctrine\Common\Collections\ArrayCollection();
    }

    public function getId() {
        return $this->id;
    }

    public function setName($name) {
        $this->name = $name;
    }

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

    public function setDescription($description) {
        $this->description = $description;
    }

    public function getCondition() {
        return $this->condition;
    }

    public function setAgeLimit($age_limit) {
        $this->age_limit = $age_limit;
    }

    public function getAgeLimit() {
        return $this->age_limit;
    }

    public function setMedias(\MediaBundle\Entity\Media $medias) {
        $this->medias[] = $medias;
    }

    public function getMedias() {
        return $this->medias;
    }

    public function setCreated($created) {
        $this->created = $created;
    }

    public function getCreated() {
        return $this->created;
    }

    public function setModified($modified) {
        $this->modified = $modified;
    }

    public function getModified() {
        return $this->modified;
    }

    public function setBrand($brand) {
        $this->brand = $brand;
    }

    public function getBrand() {
        return $this->brand;
    }

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

    public function getDetails() {
        return $this->details;
    }

}


namespace ProductBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="product_has_product_detail")
 */
class ProductHasProductDetail {

    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="ProductBundle\Entity\Product")
     * @ORM\JoinColumn(name="product", referencedColumnName="id")
     */
    protected $product;

    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="ProductBundle\Entity\ProductDetail")
     * @ORM\JoinColumn(name="detail", referencedColumnName="id")
     */
    protected $detail;

    /**
     * @ORM\Column(type="string", length=255)
     */
    protected $content;

    public function setProduct(\ProductBundle\Entity\Product $product) {
        $this->product = $product;
    }

    public function getProduct() {
        return $this->product;
    }

    public function setDetail(\ProductBundle\Entity\ProductDetail $detail) {
        $this->detail = $detail;
    }

    public function getDetail() {
        return $this->detail;
    }

    public function setContent($content) {
        $this->content = $content;
    }

    public function getContent() {
        return $this->content;
    }

}

What I miss?

UPDATE

I've added this to Product entity:

/**
 * @ORM\OneToMany(targetEntity="ProductBundle\Entity\ProductHasProductDetail", mappedBy="detail")
 */
protected $details;

And this is what I've in my Twig template:

 {{ entity.getName }}
 {% for item in entity.getDetails %}
        a {{ item.getDetail.getContent }}
 {% endfor %}

As result it never display getDetails (of course data exists at DB)

UPDATE2

This is the controller action that calls the template where I try to render the values:

/**
     * Get product data
     *
     * @Route("/product/show/{product_id}", name="product_show")
     * @Method("GET")
     */
    public function showAction(Request $request, $product_id) {
        $entity = $this->getDoctrine()->getRepository('ProductBundle:Product')->find($product_id);
        $entitySeller = $this->getDoctrine()->getRepository('StockBundle:KStock')->find($product_id);

        return $this->render("ProductBundle:Default:product_detail.html.twig", array('entity' => $entity, 'entitySeller' => $entitySeller));
    }

Solution

  • What's stored inside entity (in your twig template)? Can you update your question with the Controller code? So we can see what are you passing through entity.

    Btw, a generic answer for your question would be: The entities are ok, I didn't see anything wrong there. But the way you should access to the related tables in your templates should be something like this:

    {{ entity }} {# your whole query result, assuming you hit product_has_product table #}
    {% for item in entity %}
        {{ item.getProduct }} {# the product entity #}
        {{ item.getProduct.getName }} {# the product name #}
        {{ item.getProduct.getDescription }} {# the product description (etc) #}
        {# --- #}
        {{ item.getDetail }} {# the product detail entity #}
        {{ item.getDetail.getLabel }} {# the product detail label #}
    {% endfor %}
    

    EDIT:

    According to the controller's code that you added, I'm pretty sure you have to change that code in order to make the thing work. I will give some examples assuming you are asking for a single product (provided by $product_id).

    Your controller should look something like this:

     $entity = $this->getDoctrine()->getRepository('ProductBundle:ProductHasProductDetail')->findByProduct($product_id);
    

    And your twig template something like this:

    {{ entity.getContent }} {# the product content #}
    {{ entity.getDetail.getLabel }} {# the product detail label #}
    {{ entity.getProduct }} {# your product (in case you want to access other values) #}
    {{ entity.getProduct.getDescription }} {# e.g. the product description #}