1

我有一个描述学校的实体,并且与另一个描述“阶段”的实体相关,该实体与 1 所学校的关联 - > 许多阶段。

学校有以下领域(我将仅列出相关领域/方法):

namespace A4U\DataBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

class StuAnagScuole
{
    .
    .
    .

    private $citta;

    private $provincia;

    private $regione;

    /**
      * @ORM\OneToMany(targetEntity="A4U\FormBundle\Entity\Stage", mappedBy="attendedSchool")
    */
    protected $stages;

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

    .
    .
    .
}

而 Stage 实体是这样的:

namespace A4U\FormBundle\Entity;
class Stage
{
    .
    .
    .
    /**
     * @ORM\ManyToOne(targetEntity="A4U\DataBundle\Entity\StuAnagScuole", inversedBy="stages")
     * @ORM\JoinColumn(name="attendedSchool_id", referencedColumnName="idRecord")
     */
    protected $attendedSchool;
    .
    .
    .
}

我创建了一个表单,允许用户注册一个阶段。在某些时候,用户必须从保存在数据库中的学校列表中输入他就读的学校。

由于有 7000 多所学校,我必须过滤它们:

1)首先用户选择一个区域

2)然后他选择一个区

3)然后是一个城市

4)最后是学校

所以我想为每个“过滤器”添加一个实体字段(第一个是这样的):

->add('attendedSchoolRegion', 'entity', array(
    /*
        usando mapped false si dice a simfony che il campo non deve essere scritto
        nell'entità
    */
    'mapped' => false,
    'label' => 'Regione della scuola*',
    'class' => 'A4UDataBundle:StuAnagScuole',
    'query_builder' => function(EntityRepository $er) {
        return $er->getRegioni();
        },
    'property' => 'regione',
    'attr' => array(
        'class' => 'form-control',
        'placeholder' => 'Campo di studi'
        )
    ))

在实体存储库中:

/**
 * Get Regioni
 *
 * @return array 
 */
public function getRegioni()
{
    $qb = $this->getEntityManager()->createQueryBuilder();

    $qb->select('s0')
    ->from('A4UDataBundle:StuAnagScuole', 's0')
    ->groupBy('s0.regione');

    return $qb;
}

所以此时我想在“attendedSchoolRegion”字段中添加一个 POST_SUBMIT 事件,以像这样动态填充“attendedSchoolDistrict”:

    $addDistrict = function (FormInterface $form, StuAnagScuole $attendedSchoolRegion)
    {
        //$districts = $attendedSchoolRegion->getProvincie($attendedSchoolRegion);

            $form->add('attendedSchoolDistrict', 'entity', array(
            'mapped' => false,
            'label' => 'Provincia della scuola di provenienza*',
            'class' => 'A4UDataBundle:StuAnagScuole',
            'query_builder' => function(EntityRepository $er) {
                return $er->getProvincie($attendedSchoolRegion);
                },
            'property' => 'provincia',
            'attr' => array(
                'class' => 'form-control',
                )
            ));

    };

    $builder->get('attendedSchoolRegion')->addEventListener(
        FormEvents::POST_SUBMIT,
        function (FormEvent $event) use ($addDistrict){
            $attendedSchoolRegion = $event->getForm()->getData();
            $addDistrict($event->getForm()->getParent(), $attendedSchoolRegion);
        }
    );

以此类推,直到选择学校。在该过程结束时,我只希望将所选学校的 id 持久保存到字段参加学校下的数据库中。

但是我似乎无法将所选区域的值传递给“getProvincie”方法。

这是错误:

Catchable Fatal Error: Argument 2 passed to A4U\FormBundle\Form\Type\StageType::A4U\FormBundle\Form\Type\{closure}() must be an instance of A4U\DataBundle\Entity\StuAnagScuole, none given...

我希望我已经足够清楚了,谢谢你的帮助。

4

0 回答 0