0

我有一个包含此字段的表格:

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';
    }

}

并拥有这个实体:

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;
    }

}

我存储分配给订单的最新状态的位置。我在编辑或显示订单时需要在表单上设置该状态,但不知道如何实现这一点,可以给我一些帮助吗?

4

1 回答 1

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.

于 2014-09-22T08:09:10.383 回答