Question

I'am trying to make a form for entity content with a OneToMany relation to URL. I want to display all the URL's in a radio input field.

so the form will look like this:

//an input field for content
url:
o url1
o url2

the user can choose one of the url's linked to the content to be the canonical url.

content entity

namespace Application\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity
 * @ORM\InheritanceType("JOINED")
 * @ORM\HasLifecycleCallbacks*
 */
 class Content
{
/**
 * @var integer
 *
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;
/**
 * SeoUrl's from page
 *
 * @var ArrayCollection
 * @ORM\OneToMany(targetEntity="Application\Entity\Url", mappedBy="content", cascade={"remove", "persist"})
 */
protected $urls;

URL entity

namespace Application\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Entity Class representing our Url module.
 *
 * @ORM\Entity
 * @ORM\Table(name="url")
 */
class Url {

/**
 * @ORM\Id
 * @ORM\Column(type="integer");
 * @ORM\GeneratedValue(strategy="AUTO")
 * @var int
 */
protected $id;

/**
 * Content with Url
 *
 * @ORM\ManyToOne(targetEntity="Application\Entity\Content", inversedBy="urls")
 * @ORM\JoinColumns({
 * @ORM\JoinColumn(name="content_id", referencedColumnName="id", onDelete="CASCADE")
 * })
 */
protected $content;

/**
 * @ORM\Column(type="string")
 * @var string
 */
protected $url;

My formfield

namespace Content\Form\Fieldset;

use Content\Entity\Content;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;

class ContentFieldset
{
protected $em;

public function __construct()
{
    parent::__construct('content');
    $this->setObject(new Content());
}

public function init()
{
    $this->em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
    $this->setHydrator(new DoctrineHydrator($this->em));

    $this->add(
        array(
            'name' => 'id',
            'type' => 'Zend\Form\Element\Hidden'
        )
    );

    $this->add(
        array(
            'name' => 'url',
            'type' => 'DoctrineModule\Form\Element\ObjectRadio',
            'options' => array(
                'label' => 'Url',
                'object_manager' => $this->em,
                'target_class'   => 'Application\Entity\Url',
                'property'       => 'url',
                'is_method'      => true,
                'find_method'    => array(
                    'name'   => 'findBy',
                    'params' => array(
                        'criteria' => array('content' => 1),// I need this to be the content id
                        'orderBy'  => array(),
                    ),
                )
            )
        )
    );
}

I want only the Url's that are related so i found find_method, but i can only use hard coded cricteria. My question is, how can i give the ObjectRadio form the id of content to search for in the url entity.

I tried to use $this->getObject->getId() , but no luck.

Side note: in the Url Entity there will be a field the hold the information of the url is canonical or not. And will be used to check it's ObjectRadio.

Was it helpful?

Solution

My form

$this->add(
        array(
            'name' => 'url',
            'type' => 'Zend\Form\Element\Radio',
            'options' => array(
                'value_options' => array(
                    '' => ''
                )
            )
        )
    );

and then i overwrite the value_options in my view like this

$options = array();
$urls = $content->get('urls')->getFieldsets();
foreach($urls as $url) {
     $url_id = $url->getObject()->getId();
     $url_url = $url->getObject()->getUrl();

     $options[$url_id] = $url_url;
}

echo $this->formRow($content->get('url')->setAttributes(
     array(
          'options' => $options
     )
));

I hope someone in the future can use this answer. Beter answers are allways welcome.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top