Symony 3动态形式和学说

时间:2017-02-07 11:05:53

标签: php doctrine-orm symfony

还在与Symfony3和Doctrine争吵。只是试图从数据库中简单地自动填充表单。我有两张桌子 - 国家和州。

countries.php

   namespace AppBundle\Entity\Forms;

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

/**
 * @ORM\Entity
 * @ORM\Table(name="countries")
 */
class Countries
{

/**
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 * @ORM\Column(type="integer")
 */
private $id;
/**
 * @ORM\Column(type="string")
 */
private $country_code;
/**
 * @ORM\Column(type="string")
 */
private $country_name;


public function __toString()
{
return $this->country_name;
}
/**
 * @var Collection|States[]
 *
 * @ORM\OneToMany(targetEntity="AppBundle\Entity\Forms\States", mappedBy="country_code")
 */
protected $states;

public function __constructor()
{
    $this->states = new ArrayCollection();
}



/**
 * @return mixed
 */
public function getId()
{
    return $this->id;
}

/**
 * @return mixed
 */
public function getCountryCode()
{
    return $this->country_code;
}

/**
 * @param mixed $country_code
 */
public function setCountryCode($country_code)
{
    $this->country_code = $country_code;
}

/**
 * @return mixed
 */
public function getCountryName()
{
    return $this->country_name;
}

/**
 * @param mixed $country_name
 */
public function setCountryName($country_name)
{
    $this->country_name = $country_name;
}

/**
 * @return States[]|Collection
 */
public function getStates()
{
    return $this->states;
}

/**
 * @param States[]|Collection $states
 * @return $this
 */
public function setStates($states)
{
    $this->states = $states;
}

states.php

/**
 * Created by PhpStorm.
 * User: Alvydas Budrys
 * Website: http://alvy.lt
 * Date: 2017-02-07
 * Time: 6:13 AM
 */

namespace AppBundle\Entity\Forms;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="states")
 */
class States
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string")
     */
    private $name;

    /**
     *
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Forms\Countries", inversedBy="states")
     * @ORM\JoinColumn(name="country_code", referencedColumnName="id", onDelete="CASCADE")
     */
    private $country_code;

    public function __toString()
    {
        return $this->name;
    }

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @return mixed
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param mixed $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * @return mixed
     */
    public function getCountryCode()
    {
        return $this->country_code;
    }

    /**
     * @param mixed $country_code
     */
    public function setCountryCode($country_code)
    {
        $this->country_code = $country_code;
    }

还有formfile:

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\FormInterface;
use AppBundle\Entity\Forms\States;



class UserType extends AbstractType
{



    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', TextType::class,
                array('label' => false,
                    'mapped'=>false,
                    'attr' => array(
                        'placeholder' => 'FirstName'
                    )))
            ->add('lastname', TextType::class,
                array('label' => false,
                    'mapped'=>false,
                    'attr' => array(
                        'placeholder' => 'Lastname'
                    )))
            ->add('email', EmailType::class,
                array('label' => false,
                    'mapped'=>false,
                    'attr' => array(
                        'placeholder' => 'Email'
                    )))
            ->add('plainPassword', RepeatedType::class, [
                'type' => PasswordType::class,
                'mapped'=>false,


                'first_options' => ['label' => false,'mapped'=>false,'attr' => array(
                    'placeholder' => 'Password'
                ) ],
                'second_options' => ['label' => false,'mapped'=>false, 'attr' => array(
                    'placeholder' => 'Confirm Password'
                ) ],
            ])
            ->add('accType',ChoiceType::class,
                array(
                    'choices' => array(
                        'Personal' => '1',
                        'Business' => '2',),
                    'choices_as_values' => true,
                    'multiple'=>false,
                    'expanded'=>true,
                    'required' =>true,
                    'data' => true,
                    'mapped'=>false,
                ))
            ->add('city', TextType::class,
                array('label' => false,
                    'mapped'=>false,
                    'attr' => array(
                        'placeholder' => 'City'
                    )))
            ->add('postcode', TextType::class,
                array('label' => false,
                    'mapped'=>false,
                    'attr' => array(
                        'placeholder' => 'Postcode'
                    )))
            ->add('CountryName', EntityType::class, array(
                'class'       => 'AppBundle\Entity\Forms\Countries',
                'label' => false,
                //'mapped'=>false,
                'choice_translation_domain' => true,
            ))
            ->add('companyName', TextType::class,
                array('label' => false,
                    'mapped'=>false,
                    'attr' => array(
                        'placeholder' => 'Company Name'
                    )))
            ->add('companyNumber', TextType::class,
                array('label' => false,
                    'mapped'=>false,
                    'attr' => array(
                        'placeholder' => 'Company Number'
                    )))
            ->add('companyVat', TextType::class,
                array('label' => false,
                    'mapped'=>false,
                    'attr' => array(
                        'placeholder' => 'VAT'
                    )))



        ;

        $formModifier = function (FormInterface $form, States $states = null) {
            $positions = null === $states ? array() : $states->getStates();

            $form->add('states', EntityType::class, array(
                'class'       => 'AppBundle\Entity\Forms\States',
                'placeholder' => '',
                'choices'     => $positions,
            ));
        };

        $builder->addEventListener(
            FormEvents::PRE_SET_DATA,
            function (FormEvent $event) use ($formModifier) {
                // this would be your entity, i.e. SportMeetup
                $data = $event->getData();

                $formModifier($event->getForm(), $data->getCountryName());
            }
        );

        $builder->get('CountryName')->addEventListener(
            FormEvents::POST_SUBMIT,
            function (FormEvent $event) use ($formModifier) {
                // It's important here to fetch $event->getForm()->getData(), as
                // $event->getData() will get you the client data (that is, the ID)
                $countries = $event->getForm()->getData();
                // since we've added the listener to the child, we'll have to pass on
                // the parent to the callback functions!
                $formModifier($event->getForm()->getParent(), $countries);
            }
        );
    }

}

但是仍然有错误:

Type error: Argument 2 passed to AppBundle\Form\UserType::AppBundle\Form\{closure}() must be an instance of AppBundle\Entity\Forms\States, instance of AppBundle\Entity\Forms\Countries given, called in /var/www/hostera.dev/public_html/src/AppBundle/Form/UserType.php on line 139

Uncaught PHP Exception Symfony\Component\Debug\Exception\FatalThrowableError: "Type error: Argument 2 passed to AppBundle\Form\UserType::AppBundle\Form\{closure}() must be an instance of AppBundle\Entity\Forms\States, instance of AppBundle\Entity\Forms\Countries given, called in /var/www/hostera.dev/public_html/src/AppBundle/Form/UserType.php on line 139" at /var/www/hostera.dev/public_html/src/AppBundle/Form/UserType.php line 111

我认为与Doctrine Association有关,但我无法弄清楚是什么。

0 个答案:

没有答案