1

我有这个数据库模型:

在此处输入图像描述

我需要获得的价值product_has_product_detail.contentproduct_detail.label只是拥有product.id。这是我的实体:

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;
    }

}

我想念什么?

更新

我已将此添加到产品实体中:

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

这就是我在 Twig 模板中的内容:

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

结果它永远不会显示getDetails(当然数据存在于数据库中)

更新2

这是调用模板的控制器操作,我尝试在其中呈现值:

/**
     * 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));
    }
4

2 回答 2

1

里面存储了什么entity(在你的树枝模板中)?你能用控制器代码更新你的问题吗?所以我们可以看到你正在通过什么entity

顺便说一句,您的问题的通用答案是:实体没问题,我没有看到任何问题。但是您应该访问模板中相关表的方式应该是这样的:

{{ 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 %}

编辑:

根据您添加的控制器代码,我很确定您必须更改该代码才能使事情正常工作。假设您要求单一产品(由 提供$product_id),我将给出一些示例。

您的控制器应如下所示:

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

你的树枝模板是这样的:

{{ 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 #}
于 2013-08-23T20:41:40.213 回答
0

您可能会发现这很相关:http ://docs.doctrine-project.org/en/latest/reference/association-mapping.html

为什么多对多关联不太常见?因为您经常希望将附加属性与关联关联,在这种情况下您需要引入关联类。因此,直接的多对多关联消失了,取而代之的是三个参与类之间的一对多/多对一关联。
{{ product.getName }}
{% for productDetail in product.getProductDetails %}
    {{ productDetail.content }}
    {{ productDetail.getDetail.label }}
 {% endfor %}

看起来您已经放弃了 ManyToMany,这很好。

于 2013-08-23T23:17:16.807 回答