Code: Select all
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Product
*
* @ORM\Table(name="products")
* @ORM\Entity(repositoryClass="Pas\ShopTestBundle\Entity\ProductRepository")
*/
class Product
{
/**
* @var ArrayCollection
*
* @ORM\OneToMany(targetEntity="Description", mappedBy="product")
*/
private $descriptions;
// MORE CODE BELOW....
/**
* Creates Constructor for ArrayCollection
*/
public function __construct()
{
$this->descriptions = new ArrayCollection();
}
MORE CODE...
/**
* Add descriptions
*
* @param \Pas\ShopTestBundle\Entity\Description $descriptions
* @return Product
*/
public function addDescription(\Pas\ShopTestBundle\Entity\Description $descriptions)
{
$this->descriptions[] = $descriptions;
return $this;
}
/**
* Remove descriptions
*
* @param \Pas\ShopTestBundle\Entity\Description $descriptions
*/
public function removeDescription(\Pas\ShopTestBundle\Entity\Description $descriptions)
{
$this->descriptions->removeElement($descriptions);
}
/**
* Get descriptions
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getDescriptions()
{
return $this->descriptions;
}
/**
* Converts Product Name to a Viewable String
* @return String
*/
public function __toString()
{
return $this->getName();
return $this->getDescriptions();
}
}
< /code>
Ich versuche, die Beschreibung in meiner 'ShowAction' zu erweisen, die zu zeigen.html.twig. In dieser Funktion habe ich: < /p>
/**
* Finds and displays a Product entity.
*
* @Route("/{id}", name="product_show")
* @Method("GET")
* @Template()
*/
public function showAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('PasShopTestBundle:Product')->find($id);
// $descriptionInfo = $em->getRepository('PasShopTestBundle:Description')
// ->find($id)
// ->getProductDesciption();
//get description to appear on show page
if (!$entity) {
throw $this->createNotFoundException('Unable to find Product entity.');
} else {
$productInfo = $entity->getDescriptions();
}
$deleteForm = $this->createDeleteForm($id);
return array(
'entity' => $entity,
'delete_form' => $deleteForm->createView(),
);
}
< /code>
Wie Sie sehen können, habe ich ein paar Dinge ausprobiert. Ich bin mir jedoch nicht sicher, ob es richtig ist.
Description
{% for description in descriptions %}
{{ entity.description }}
{% endfor %}
{# --------- Need Description Show.html.twig to go to above ------------------ #}
Variable "Beschreibungen" existiert nicht in Src/Pas/ShopTestBundle/Ressourcen/Ressourcen/Produkt/produkt/show. 22 ...) < /p>
< /blockquote>
In Beschreibungen Entität und dessen Controller funktioniert alles. Ich kann eine Beschreibung mit einer ID eingeben, die der ID von Product Entity/Controller entspricht. Insgesamt möchte ich, dass die Beschreibung, die ich dort eingehe, im Produkt erscheint. (Ich hoffe, das macht Sinn) < /p>
Beschreibung Entität: < /p>
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collection\ArrayCollection;
/**
* Description
*
* @ORM\Table(name="descriptions")
* @ORM\Entity(repositoryClass="Pas\ShopTestBundle\Entity\DescriptionRepository")
*/
class Description
{
/**
* @var Product
*
* @ORM\ManyToOne(targetEntity="Product", inversedBy="descriptions")
* @ORM\JoinColumn(name="product_id", referencedColumnName="id")
*/
private $product;
< /code>
Ich bin mir sicher, dass ich nah dran bin, aber ich kann es nicht herausfinden. Jede Hilfe wird geschätzt