私は、RequestDetailsと呼ばれる別のエンティティの親であるRequestsというエンティティを持っています。 RequestDetailsは、数量、手数料、システム内の製品と呼ばれる別のエンティティを指し示す製品などで構成されています。 RequestDetailがより詳細な情報とProductエンティティに接続できるproduct_idで構成されている場合、一般的な情報のみがRequestエンティティに送られることを完全に意味しています。Symfonyのコレクションコレクションから値を表示するには?
ここでデータを表示すると、すべてが製品データの外に表示されます。また、{{dump(detail.product)}}をダンプすると、{{dump(detail.product.name)}}のようなことをしようとすると、製品エンティティの正しい構造が表示されます。仕事はありません。この問題にどうやってアプローチするのですか?私の見解にどのように表示するのですか?ここではスクリーンショットです:ここでは
は私のコードは次のとおりです。
マイビュー:
<tbody>
{% for request in requests %}
<tr class="gradeX">
<td>{{request.name}}</td>
{% for detail in request.details %}
<td>
{{dump(detail.product)}}
</td>
<td>
{{ detail.quantity }}
</td>
<td>
${{ detail.pricePerUnit }}
</td>
<td>
${{ detail.shippingCost }}
</td>
<td>
${{ detail.otherFees }}
</td>
<td>
${{ detail.quantity * detail.pricePerUnit + detail.shippingCost + detail.otherFees }}
</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
RequestForEstimateエンティティ:
<?php
namespace SourcingBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* RequestForEstimate
*
* @ORM\Table(name="request_for_estimate")
* @ORM\Entity(repositoryClass="SourcingBundle\Repository\RequestForEstimateRepository")
*/
class RequestForEstimate
{
/**
* @var int
*
* @ORM\Column(name="request_id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var int
*
* @ORM\Column(name="status", type="integer")
*/
private $status;
/**
* @var \DateTime
*
* @ORM\Column(name="create_time", type="datetime")
*/
private $createTime;
/**
* @var \DateTime
*
* @ORM\Column(name="update_time", type="datetime")
*/
private $updateTime;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @ORM\OneToMany(targetEntity="RequestForEstimateDetail", mappedBy="detail", cascade={"persist", "remove"}, orphanRemoval=true)
*/
private $details;
/**
* Get id
*
* @return int
*/
/**
* Constructor
*/
public function __construct()
{
$this->details = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set status
*
* @param integer $status
*
* @return RequestForEstimate
*/
public function setStatus($status)
{
$this->status = $status;
return $this;
}
/**
* Get status
*
* @return integer
*/
public function getStatus()
{
return $this->status;
}
/**
* Set createTime
*
* @param \DateTime $createTime
*
* @return RequestForEstimate
*/
public function setCreateTime($createTime)
{
$this->createTime = $createTime;
return $this;
}
/**
* Get createTime
*
* @return \DateTime
*/
public function getCreateTime()
{
return $this->createTime;
}
/**
* Set updateTime
*
* @param \DateTime $updateTime
*
* @return RequestForEstimate
*/
public function setUpdateTime($updateTime)
{
$this->updateTime = $updateTime;
return $this;
}
/**
* Get updateTime
*
* @return \DateTime
*/
public function getUpdateTime()
{
return $this->updateTime;
}
/**
* Set name
*
* @param string $name
*
* @return RequestForEstimate
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Add detail
*
* @param \SourcingBundle\Entity\RequestForEstimateDetail $detail
*
* @return RequestForEstimate
*/
public function addDetail(\SourcingBundle\Entity\RequestForEstimateDetail $detail)
{
$this->details->add($detail);
$detail->setDetail($this);
return $this;
}
/**
* Remove detail
*
* @param \SourcingBundle\Entity\RequestForEstimateDetail $detail
*/
public function removeDetail(\SourcingBundle\Entity\RequestForEstimateDetail $detail)
{
$this->details->removeElement($detail);
}
/**
* Get details
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getDetails()
{
return $this->details;
}
}
RequestForEstimateDetail:
<?php
namespace SourcingBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* RequestForEstimateDetail
*
* @ORM\Table(name="request_for_estimate_detail")
* @ORM\Entity(repositoryClass="SourcingBundle\Repository\RequestForEstimateDetailRepository")
*/
class RequestForEstimateDetail
{
/**
* @var int
*
* @ORM\Column(name="request_detail_id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="RequestForEstimate", inversedBy="details")
* @ORM\JoinColumn(name="request_id", referencedColumnName="request_id")
*/
private $detail;
/**
* @ORM\ManyToOne(targetEntity="ProductsBundle\Entity\Product")
* @ORM\JoinColumn(name="product_id", referencedColumnName="product_id")
*/
private $product;
/**
* @var int
*
* @ORM\Column(name="quantity", type="integer")
*/
private $quantity;
/**
* @var string
*
* @ORM\Column(name="price_per_unit", type="decimal", precision=2, scale=0)
*/
private $pricePerUnit;
/**
* @var string
*
* @ORM\Column(name="shipping_cost", type="decimal", precision=2, scale=0)
*/
private $shippingCost;
/**
* @var string
*
* @ORM\Column(name="other_fees", type="decimal", precision=2, scale=0)
*/
private $otherFees;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set product
*
* @param integer $product
*
* @return RequestForEstimateDetail
*/
public function setProduct($product)
{
$this->product = $product;
return $this;
}
/**
* Get product
*
* @return int
*/
public function getProduct()
{
return $this->product;
}
/**
* Set quantity
*
* @param integer $quantity
*
* @return RequestForEstimateDetail
*/
public function setQuantity($quantity)
{
$this->quantity = $quantity;
return $this;
}
/**
* Get quantity
*
* @return int
*/
public function getQuantity()
{
return $this->quantity;
}
/**
* Set pricePerUnit
*
* @param string $pricePerUnit
*
* @return RequestForEstimateDetail
*/
public function setPricePerUnit($pricePerUnit)
{
$this->pricePerUnit = $pricePerUnit;
return $this;
}
/**
* Get pricePerUnit
*
* @return string
*/
public function getPricePerUnit()
{
return $this->pricePerUnit;
}
/**
* Set shippingCost
*
* @param string $shippingCost
*
* @return RequestForEstimateDetail
*/
public function setShippingCost($shippingCost)
{
$this->shippingCost = $shippingCost;
return $this;
}
/**
* Get shippingCost
*
* @return string
*/
public function getShippingCost()
{
return $this->shippingCost;
}
/**
* Set otherFees
*
* @param string $otherFees
*
* @return RequestForEstimateDetail
*/
public function setOtherFees($otherFees)
{
$this->otherFees = $otherFees;
return $this;
}
/**
* Get otherFees
*
* @return string
*/
public function getOtherFees()
{
return $this->otherFees;
}
/**
* Set requestId
*
* @param \SourcingBundle\Entity\RequestForEstimate $requestId
*
* @return RequestForEstimateDetail
*/
public function setRequestId(\SourcingBundle\Entity\RequestForEstimate $requestId = null)
{
$this->requestId = $requestId;
return $this;
}
/**
* Get requestId
*
* @return \SourcingBundle\Entity\RequestForEstimate
*/
public function getRequestId()
{
return $this->requestId;
}
/**
* Set productId
*
* @param \SourcingBundle\Entity\Product $productId
*
* @return RequestForEstimateDetail
*/
public function setProductId(\SourcingBundle\Entity\Product $productId = null)
{
$this->productId = $productId;
return $this;
}
/**
* Get productId
*
* @return \SourcingBundle\Entity\Product
*/
public function getProductId()
{
return $this->productId;
}
/**
* Set detail
*
* @param \SourcingBundle\Entity\RequestForEstimate $detail
*
* @return RequestForEstimateDetail
*/
public function setDetail(\SourcingBundle\Entity\RequestForEstimate $detail = null)
{
$this->detail = $detail;
return $this;
}
/**
* Get detail
*
* @return \SourcingBundle\Entity\RequestForEstimate
*/
public function getDetail()
{
return $this->detail;
}
/**
* Constructor
*/
public function __construct()
{
$this->detail = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add detail
*
* @param \SourcingBundle\Entity\RequestForEstimate $detail
*
* @return RequestForEstimateDetail
*/
public function addDetail(\SourcingBundle\Entity\RequestForEstimate $detail)
{
$this->detail[] = $detail;
return $this;
}
/**
* Remove detail
*
* @param \SourcingBundle\Entity\RequestForEstimate $detail
*/
public function removeDetail(\SourcingBundle\Entity\RequestForEstimate $detail)
{
$this->detail->removeElement($detail);
}
}
RequestForEstimateDetailType:
<?php
namespace SourcingBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
class RequestForEstimateDetailType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('quantity');
$builder->add('pricePerUnit');
$builder->add('shippingCost');
$builder->add('otherFees');
$builder->add('product', EntityType::class, array(
'class' => 'ProductsBundle\Entity\Product',
'choice_label' => function ($product) {
return $product->getName();
}
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'SourcingBundle\Entity\RequestForEstimateDetail',
));
}
}
RequestForEstimateController:
<?php
namespace SourcingBundle\Controller;
use SourcingBundle\Entity\RequestForEstimate;
use SourcingBundle\Entity\RequestForEstimateDetail;
use SourcingBundle\Form\Type\RequestForEstimateType;
use SourcingBundle\Form\Type\RequestForEstimateDetailType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
class RequestForEstimateController extends Controller
{
/**
* @Route("sourcing/request-for-estimate/create", name="request_for_estimate_create")
*/
public function addRequest(Request $request)
{
$RequestForEstimate = new RequestForEstimate();
$form = $this->createForm(RequestForEstimateType::class, $RequestForEstimate);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$request = $form->getData();
$request->setStatus(0);
$request->setupdateTime(new \DateTime());
$request->setcreateTime(new \DateTime());
$em = $this->getDoctrine()->getManager();
$em->persist($request);
$em->flush();
return $this->redirectToRoute('request_for_estimate_view_all');
}
return $this->render('sourcing/requestforestimate/create.html.twig', array(
'form' => $form->createView(),
));
}
/**
* @Route("sourcing/request-for-estimate", name="request_for_estimate_view_all")
*/
public function viewProducts()
{
$requests = $this->getDoctrine()
->getRepository('SourcingBundle:RequestForEstimate')->findAll();
if (!$requests) {
throw $this->createNotFoundException(
'No requests found '
);
}
return $this->render('sourcing/requestforestimate/view.html.twig', array(
'requests' => $requests
));
}
}
Productエンティティ:
<?php
namespace ProductsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Product
*
* @ORM\Table(name="product")
* @ORM\Entity(repositoryClass="ProductsBundle\Repository\ProductRepository")
*/
class Product
{
/**
* @var int
*
* @ORM\Column(name="product_id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var int
*
* @ORM\Column(name="status", type="integer")
*/
protected $status;
/**
* @var \DateTime
*
* @ORM\Column(name="create_time", type="datetime")
*/
protected $createTime;
/**
* @var \DateTime
*
* @ORM\Column(name="update_time", type="datetime")
*/
protected $updateTime;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
protected $name;
/**
* @var string
*
* @ORM\Column(name="description", type="text")
*/
protected $description;
public function getProduct()
{
return $this->product;
}
public function setProduct($product)
{
$this->product = $product;
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set status
*
* @param integer $status
*
* @return Product
*/
public function setStatus($status)
{
$this->status = $status;
return $this;
}
/**
* Get status
*
* @return int
*/
public function getStatus()
{
return $this->status;
}
/**
* Set createTime
*
* @param \DateTime $createTime
*
* @return Product
*/
public function setCreateTime($createTime)
{
$this->createTime = $createTime;
return $this;
}
/**
* Get createTime
*
* @return \DateTime
*/
public function getCreateTime()
{
return $this->createTime;
}
/**
* Set updateTime
*
* @param \DateTime $updateTime
*
* @return Product
*/
public function setUpdateTime($updateTime)
{
$this->updateTime = $updateTime;
return $this;
}
/**
* Get updateTime
*
* @return \DateTime
*/
public function getUpdateTime()
{
return $this->updateTime;
}
/**
* Set name
*
* @param string $name
*
* @return Product
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set description
*
* @param string $description
*
* @return Product
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
}
)あなたの応答と問題を指摘いただきありがとうございます。これらの作業のいずれか:http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/extra-lazy-associations.html? – user1029829
また、あなたの指示に従ってカスタムクエリを作成しました。ダンプ(detail.product)を実行すると実際に値が表示されますが、{{detail.product.name}}を実行するとエラーが再び発生します。これらのプロパティにアクセスするにはどうすればよいですか? – user1029829