Symfony 3& Doctrine - 具有OnetoMany和ManyToMany关系的复杂形式

时间:2017-11-20 09:06:10

标签: symfony-forms

我希望以多种货币管理产品的费率,并保持货币汇率的历史性。

所以onetomany关系: 利率可以有很多CurrencyRate。

多种关系: 许多currencyRate有很多货币。

费率:

+-----------------+----------+------+-----+---------+----------------+
| Field           | Type     | Null | Key | Default | Extra          |
+-----------------+----------+------+-----+---------+----------------+
| id              | int(11)  | NO   | PRI | NULL    | auto_increment |
| rate            | double   | YES  |     | NULL    |                |
| timeStamp       | datetime | NO   |     | NULL    |                |
| currencyRate_id | int(11)  | YES  | MUL | NULL    |                |
+-----------------+----------+------+-----+---------+----------------+

货币汇率

+-----------+----------+------+-----+---------+----------------+
| Field     | Type     | Null | Key | Default | Extra          |
+-----------+----------+------+-----+---------+----------------+
| id        | int(11)  | NO   | PRI | NULL    | auto_increment |
| rate      | double   | NO   |     | NULL    |                |
| timeStamp | datetime | NO   |     | NULL    |                |
+-----------+----------+------+-----+---------+----------------+

currencyrateshascurrencies(manytomany join table)

+-----------------+---------+------+-----+---------+-------+
| Field           | Type    | Null | Key | Default | Extra |
+-----------------+---------+------+-----+---------+-------+
| currency_id     | int(11) | NO   | PRI | NULL    |       |
| currencyRate_id | int(11) | NO   | PRI | NULL    |       |
+-----------------+---------+------+-----+---------+-------+

货币

+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| name        | varchar(255) | NO   | UNI | NULL    |                |
| abreviation | varchar(5)   | NO   | UNI | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+

我想从所有这些中生成一个表单。 html表单将获得具有文本字段的所有可用货币以指示CurrencyRate。 例如:

USD <input type="text">
EUR <input type="text">
CNY <input type="text">
...

我在Symfony上看到了关于manytomany形式的文档。但是我的更复杂,有一个额外的onetomany关系和文本字段。我完全迷失了。

谢谢,如果你能把我放在正确的方向上。

致以最诚挚的问候,

皮尔

1 个答案:

答案 0 :(得分:0)

我找到了回复:

首先,我需要更正不需要ManytoMany关系的Schema:

费率:

+-----------+----------+------+-----+---------+----------------+
| Field     | Type     | Null | Key | Default | Extra          |
+-----------+----------+------+-----+---------+----------------+
| id        | int(11)  | NO   | PRI | NULL    | auto_increment |
| rate      | double   | YES  |     | NULL    |                |
| timeStamp | datetime | NO   |     | NULL    |                |
+-----------+----------+------+-----+---------+----------------+

货币汇率

+-------------+---------+------+-----+---------+----------------+
| Field       | Type    | Null | Key | Default | Extra          |
+-------------+---------+------+-----+---------+----------------+
| id          | int(11) | NO   | PRI | NULL    | auto_increment |
| ratio       | double  | NO   |     | NULL    |                |
| currency_id | int(11) | YES  | MUL | NULL    |                |
| Rate_id     | int(11) | YES  | MUL | NULL    |                |
+-------------+---------+------+-----+---------+----------------+

货币

+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| name        | varchar(255) | NO   | UNI | NULL    |                |
| abreviation | varchar(5)   | NO   | UNI | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+

然后是一种表格类型

class CurrencyRateType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('currency',EntityType::class, array(
                                'class'=>'AppBundle:Currencies',
                                'choice_label' => 'abreviation',
                                'disabled' => true,
                 ))
                ->add('ratio', TextType::class);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => CurrencyRates::class,
        ));
    }
}

嵌入在最终表单类型中:

class RateType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder    ->add('rate')
                    ->add('CurrencyRate', CollectionType::class, array(
            'entry_type' => CurrencyRateType::class,
            'entry_options' => array('label' => false))
            )
                    ->add('save', SubmitType::class, array('label' => 'create'));
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => Rates::class,
        ));
    }
}