0

I have a form with this field:

class OrdersType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // Others form fields

        if ($options['curr_action'] !== NULL)
        {
            $builder
                    ->add('status', 'entity', array(
                        'class' => 'CommonBundle:OrderStatus',
                        'property' => 'name',
                        'required' => TRUE,
                        'label' => FALSE,
                        'mapped' => FALSE
            ));
        }
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setRequired(array(
            'register_type'
        ));

        $resolver->setOptional(array(
            'curr_action'
        ));

        $resolver->setDefaults(array(
            'data_class' => 'Tanane\FrontendBundle\Entity\Orders',
            'render_fieldset' => FALSE,
            'show_legend' => FALSE,
            'intention' => 'orders_form'
        ));
    }

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

}

And have this entity:

class OrderHasStatus {
    use IdentifiedAutogeneratedEntityTrait;

    /**
     * Hook timestampable behavior
     * updates createdAt, updatedAt fields
     */
    use TimestampableEntity;

    /**
     * @ORM\ManyToOne(targetEntity="\Tanane\FrontendBundle\Entity\Orders")
     * @ORM\JoinColumn(name="general_orders_id", referencedColumnName="id")
     */
    protected $order;

    /**
     * @ORM\ManyToOne(targetEntity="\Tanane\CommonBundle\Entity\OrderStatus")
     * @ORM\JoinColumn(name="status_id", referencedColumnName="id")
     */
    protected $status;

    public function setOrder(\Tanane\FrontendBundle\Entity\Orders $order)
    {
        $this->order = $order;
    }

    public function getOrder()
    {
        return $this->order;
    }

    public function setStatus(\Tanane\CommonBundle\Entity\OrderStatus $status)
    {
        $this->status = $status;
    }

    public function getStatus()
    {
        return $this->status;
    }

}

Where I store the latest status asigned to the order. I need to set that status on the form when I'm editing or showing the order but don't know how to achieve this, can any give me some help?

ReynierPM
  • 17,594
  • 53
  • 193
  • 363

1 Answers1

1

So, this looks related to your other question concerning n:m relationships. Your existing solution might work if you had a Many-to-Many relationship, but won't with three Entities.

I think the solution is similar:

OrdersType

$builder->add('orderHasStatus', 'collection', array(
    'type' => new OrderHasStatusType(),
    'allow_add' => true,
    'allow_delete' => true
));

OrderHasStatusType

$builder->add('status', 'collection', array(
    'type' => new StatusType(),
    'allow_add' => true,
    'allow_delete' => true
));

StatusType

$builder->add('status', 'entity', array(
    'class' => 'CommonBundle:Status'
));

As your schema stands^, your OrdersType needs to contain a Collection of OrderHasStatus types, which themselves have StatusTypes containing the statuses themselves.

^One thing I would query, and maybe should have in my other answer, is whether your schema is correct - do you need each Order to have multiple Statuses? If not, then what you really want is a Many-to-One between Order and Status, which would remove the intermediate OrderHasStatus layer. If you want the extra data about OrderHasStatus (the timestamp?) then you could put that info on the Order table. But if you need multiple Statuses, or want to keep the extra data separate from the Orders table (in which case Orders and OrderHasStatus are actually one-to-one), fine.

Community
  • 1
  • 1
frumious
  • 1,567
  • 15
  • 25