Is there anyway I can assign an entity and edit one of it's properties in a single Symfony2 form?
Example:
Tasks have many Activities.
When I create an Activity I assign a Task through an Entity form field. I also want to update the Task remainingEstimateInSeconds when the entity is created.
Activity Entity:
<?php
namespace AppBundle\Entity;
/**
* Activity
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="AppBundle\Entity\ActivityRepository")
*/
class Activity
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @Assert\NotNull()
* @ORM\ManyToOne(targetEntity="Task")
*/
private $task;
}
Task Entity:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Task
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="AppBundle\Entity\TaskRepository")
*/
class Task
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @Assert\NotBlank()
* @ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* @ORM\Column(name="remainingEstimateInSeconds", type="integer", length=255, nullable=true)
*/
private $remainingEstimateInSeconds;
}
Activity Form:
My current implementation is using a non-mapped field. The remaining estimate is set back onto the task in the controller which isn't the best implementation.
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\NotNull;
class ActivityType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('description');
$builder->add('task');
$builder->add('remainingEstimateInSeconds', 'integer', array(
'mapped' => false,
'constraints' => array(
new NotNull()
)
));
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(
array(
'data_class' => 'AppBundle\Entity\Activity',
)
);
}
/**
* @return string
*/
public function getName()
{
return 'activity';
}
}
I also tried using property_path => "task.remainingEstimateInSeconds"
but the duplicate property_path
was throwing some kind of error.