私はこのDBモデルを持っています:
の値を取得する必要があり、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;
}
}
何が恋しい?
アップデート
これを Product エンティティに追加しました:
/**
* @ORM\OneToMany(targetEntity="ProductBundle\Entity\ProductHasProductDetail", mappedBy="detail")
*/
protected $details;
そして、これは私がTwigテンプレートに持っているものです:
{{ entity.getName }}
{% for item in entity.getDetails %}
a {{ item.getDetail.getContent }}
{% endfor %}
その結果、表示されることはありませんgetDetails
(もちろんデータはDBに存在します)
更新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));
}