我有这个数据库模型:
我需要获得的价值product_has_product_detail.content
和product_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));
}