Since there was no reaction to my initial post, let me rephrase the
question, I use a ZF2 Form to add a Doctrine Entity.
I want to use this form to also pass the name of a related Entity.
What would it look like.
<?php
/**
* Product
*
* @ORM\Table(name="products")
* @ORM\Entity(repositoryClass="\Entities\ProductRepository")
*/
class Product implements InputFilterAwareInterface
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", precision=0, scale=0,
nullable=false, unique=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=100, precision=0,
scale=0, nullable=false, unique=false)
*/
private $name;
/**
* @var \Entities\Label
*
* @ORM\ManyToOne(targetEntity="Entities\Label", inversedBy="products")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="label_id", referencedColumnName="id",
nullable=true)
* })
*/
private $label;
}
/**
* Label
*
* @ORM\Table(name="labels")
* @ORM\Entity(repositoryClass="\Entities\LabelRepository")
*/
class Label implements InputFilterAwareInterface
{
/**
*
* @var type \Zend\InputFilter\InputFilter
*/
private $inputFilter;
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", precision=0, scale=0,
nullable=false, unique=true)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=100, precision=0,
scale=0, nullable=false, unique=false)
*/
private $name;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\OneToMany(targetEntity="Entities\Product", mappedBy="label")
*/
private $products;
/**
* Constructor
*/
public function __construct()
{
$this->products = new
\Doctrine\Common\Collections\ArrayCollection();
}
}
class ProductForm extends Form implements ObjectManagerAwareInterface
{
protected $objectManager;
public function __construct(ObjectManager $objectManager)
{
$this->setObjectManager($objectManager);
parent::__construct('ProductAttributes');
$this->setHydrator(new
DoctrineEntity($this->getObjectManager()))->setObject(new Product());
$this->addElements();
}
public function addElements()
{
$this->add(array(
'name' => 'id',
'type' => 'Hidden',
));
$this->add(array(
'name' => 'name',
'type' => 'Text',
'options' => array(
'label' => 'Name',
),
));
$this->add(array(
'name' => 'label',
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'options' => array(
'object_manager' => $this->getObjectManager(),
'target_class' => '\Entities\Label',
'property' => 'name',
'is_method' => true,
'find_method' => array(
'name' => 'findAll',
),
),
));
$this->add(array(
'name' => 'label',
'type' => 'Text',
'options' => array(
'label' => 'Label',
),
));
$this->add(array(
'name' => 'submit',
'type' => 'Submit',
'attributes' => array(
'value' => 'Go',
'id' => 'submitbutton',
),
));
}
public function setObjectManager(ObjectManager $objectManager)
{
$this->objectManager = $objectManager;
}
public function getObjectManager()
{
return $this->objectManager;
}
}
As you see in the ProductForm, I've added two options for adding the
related label (ObjectSelect works fine), but I just want to enter the name
of the related Label and find the related label to relate it to the product.
What is the best and most efficient way to implement this?
Thanks for the help!
Op zondag 30 november 2014 13:53:06 UTC+1 schreef Antenne:
>
> Hello,
>
> I have a Form for a Product and since Product is one of the central
> Entities in my application there are various related Entities.
> One of them is Label and can contain so many rows that an ObjectSelect is
> not appropriate.
> Therefore I would like to implement something more or less similar but
> with Search functionality.
> I would like to search for the name of the Label and have the Id added.
>
> My ObjectSelect works fine and looks like this:
>
> class ProductForm extends Form implements ObjectManagerAwareInterface
> {
>
> protected $objectManager;
>
> public function __construct(ObjectManager $objectManager)
> {
>
> $this->setObjectManager($objectManager);
>
> parent::__construct('ProductAttributes');
> $this->setHydrator(new
> DoctrineEntity($this->getObjectManager()))->setObject(new Product());
>
> $this->addElements();
> }
>
> public function addElements()
> {
> // Add various elements
>
> $this->add(array(
> 'name' => 'label',
> 'type' => 'DoctrineModule\Form\Element\ObjectSelect',
> 'options' => array(
> 'object_manager' => $this->getObjectManager(),
> 'target_class' => '\Entities\Label',
> 'property' => 'name',
> 'is_method' => true,
> 'find_method' => array(
> 'name' => 'findAll',
> ),
> ),
> ));
>
>
> $this->add(array(
> 'name' => 'submit',
> 'type' => 'Submit',
> 'attributes' => array(
> 'value' => 'Go',
> 'id' => 'submitbutton',
> ),
> ));
> }
>
> public function setObjectManager(ObjectManager $objectManager)
> {
> $this->objectManager = $objectManager;
> }
>
> public function getObjectManager()
> {
> return $this->objectManager;
> }
>
> }
>
> Is there an easy way to implement similar functionality but with a search
> feature?
>
> Thanks!
>
--
You received this message because you are subscribed to the Google Groups
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.