1

I have this scenario

EntityA related to EntityB into a one-to-many fashion. I have created a form for EntityB and I want to select (with checkboxes) some of EntityA element.

How can I achieve this?

UPDATE

public function viewAction()
{
    $id_struttura = 9;
    $channel_rates = $this->getDoctrine()->getRepository('SestanteChannelManagerBundle:StrutturaCanaleTariffa')->findByStruttura($id_struttura);
    $list = array();
    foreach ($channel_rates as $channel_rate) {
        $list[$channel_rate->getId()] = $channel_rate->getNomeCameraTariffa();
    }
    $view = new View();
    $view->addChannelRate($channel_rates);
    $form = $this->createForm(new ViewType(), $view);
    $request = $this->getRequest();
    if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);
        if ($form->isValid()) {
            $dateRange = new DateRange($date_from, $date_to);
            $channelDisp = $this->get('channel_dispatcher');
            $inventories = $channelDisp->queryAvailabilityRates($dateRange, $channelRateList);
            return $this->render('SestanteCDTestBundle:Main:view_result.html.twig', array('inventories' => $inventories));
        }
    }
    return $this->render('SestanteCDTestBundle:Main:view.html.twig', array('form' => $form->createView()));
}

This is my entity

<?php

namespace Sestante\CDTestBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;

class View
{
    protected $channel_rate;

    function __construct()
    {
        $this->channel_rate = new ArrayCollection();
    }


    /**
     * @return the $date_from
     */
    public function getDateFrom() {
        return $this->date_from;
    }

    /**
     * @return the $date_to
     */
    public function getDateTo() {
        return $this->date_to;
    }

    /**
     * @return the $channel_rate
     */
    public function getChannelRate() {
        return $this->channel_rate;
    }

    /**
     * @param field_type $date_from
     */
    public function setDateFrom($date_from) {
        $this->date_from = $date_from;
    }

    /**
     * @param field_type $date_to
     */
    public function setDateTo($date_to) {
        $this->date_to = $date_to;
    }

    /**
     * @param field_type $channel_rate
     */
    public function addChannelRate($channel_rate) {
          $this->channel_rate[] = $channel_rate;
    }
}

And this is my form

<?php

namespace Sestante\CDTestBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class ViewType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('date_from', 'date', array('widget' => 'single_text','format' => 'yyyy-MM-dd'));
        $builder->add('date_to', 'date', array('widget' => 'single_text','format' => 'yyyy-MM-dd'));
        $builder->add('channel_rate', 'entity', array('class'=>'SestanteChannelManagerBundle:StrutturaCanaleTariffa', 'multiple'=>true, 'expanded'=>true));
    }

    public function getName()
    {
        return 'view';
    }
}

If I add a statement like this (into entity) all works good:

foreach ($channel_rate as $ch)
{
$this->channel_rate[] = $ch;
}

1 Answer 1

5

In your form's buildForm method:

$builder->add('entitya', 'entity', array(
    'class'     => 'YourAwesomeBundle:EntityA',
    'expanded'  => true,
    'multiple'  => true
));

As stated in reference.

EDIT looking at your code:

/**
 * @param field_type $channel_rate
 */
public function addChannelRate($channel_rate) {
      $this->channel_rate[] = $ch;
}

$ch is not defined in this method, you need to write `$this->channel_rate[] = $channel_rate.

Anyway, provide a setChannelRate method, as:

/**
 * @param ArrayCollection $rates
 */
public function setChannelRate($rates) {
      $this->channel_rate = $rates;
}

And retry.

Sign up to request clarification or add additional context in comments.

5 Comments

It doesn't works. It gave me an error like "Collection expected, array given" (sorry I can't report it because now I've choose another way that isn't the best)
Please post your entities code, and your request binding logic as well.
So I have to create a set method and iterate over my array provided into input, as I do into my last "hint" in question??
You don't need to iterate as you are replacing the whole collection - just copy/paste my method.
$rates isn't an array collection.. Is an Array, an this make a problem. So I need to iterate.. But I get what you're saying, so I was on the right way.... ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.