类型" AppBundle \ Entity \ MyMachine的预期参数或null","整数"特定

时间:2017-07-07 13:22:02

标签: php symfony doctrine twig

我有问题,我的数据库添加数据。

我有错误

Expected argument of type "AppBundle\Entity\VoiceAnswerMachine or null", "integer" given

我的实体一

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

/**
 * @ORM\ManyToOne(targetEntity="VoiceAnsweringMachine", inversedBy="uid")
 */
protected $uid;

/**
 * @ORM\Column(type="integer", nullable=true)
 */
private $buttonNumber;

/**
 * @ORM\Column(type="text", nullable=false)
 */
private $linkToMusic;

..... getters, setters


/**
 * Set uid
 *
 * @param \AppBundle\Entity\VoiceAnswerMachine $uid
 *
 * @return AnsweringMachine
 */
public function setUid(\AppBundle\Entity\VoiceAnswerMachine $uid = null)
{
    $this->uid = $uid;

    return $this;
}

/**
 * Get uid
 *
 * @return \AppBundle\Entity\VoiceAnswerMachine
 */
public function getUid()
{
    return $this->uid;
}

我的实体二

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

/**
 * @ORM\OneToMany(targetEntity="AnsweringMachine", mappedBy="uid")
 */
private $uid;

/**
 * @ORM\Column(type="integer")
 */
private $numberMachine;

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

/**
 * @ORM\Column(type="text")
 */
private $linkToVoice;

/**
 * Constructor
 */
public function __construct()
{
    $this->uid = new \Doctrine\Common\Collections\ArrayCollection();
}

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

/**
 * Set numberMachine
 *
 * @param integer $numberMachine
 *
 * @return VoiceAnsweringMachine
 */
public function setNumberMachine($numberMachine)
{
    $this->numberMachine = $numberMachine;

    return $this;
}

/**
 * Get numberMachine
 *
 * @return integer
 */
public function getNumberMachine()
{
    return $this->numberMachine;
}

/**
 * Set nameMachine
 *
 * @param string $nameMachine
 *
 * @return VoiceAnsweringMachine
 */
public function setNameMachine($nameMachine)
{
    $this->nameMachine = $nameMachine;

    return $this;
}

/**
 * Get nameMachine
 *
 * @return string
 */
public function getNameMachine()
{
    return $this->nameMachine;
}

/**
 * Set linkToVoice
 *
 * @param string $linkToVoice
 *
 * @return VoiceAnsweringMachine
 */
public function setLinkToVoice($linkToVoice)
{
    $this->linkToVoice = $linkToVoice;

    return $this;
}

/**
 * Get linkToVoice
 *
 * @return string
 */
public function getLinkToVoice()
{
    return $this->linkToVoice;
}

/**
 * Add uid
 *
 * @param \AppBundle\Entity\AnsweringMachine $uid
 *
 * @return VoiceAnsweringMachine
 */
public function addUid(\AppBundle\Entity\AnsweringMachine $uid)
{
    $this->uid[] = $uid;

    return $this;
}

/**
 * Remove uid
 *
 * @param \AppBundle\Entity\AnsweringMachine $uid
 */
public function removeUid(\AppBundle\Entity\AnsweringMachine $uid)
{
    $this->uid->removeElement($uid);
}

/**
 * Get uid
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getUid()
{
    return $this->uid;
}

My Twig

<div class="row">
                            <div class="col-sm-12">
                                {{ form_start(answeringForm, {'attr': {'autocomplete': 'off'}}) }}
                                <div class="form-group">
                                    <div class="col-sm-12">
                                        {{ form_label(answeringForm.uid, 'Wybierz maszynę') }}
                                    </div>
                                    <div class="col-sm-12" style="margin-bottom: 15px;">
                                        {{ form_errors(answeringForm.uid) }}
                                        {{ form_widget(answeringForm.uid) }}
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-sm-12">
                                        {{ form_label(answeringForm.buttonNumber, 'Numer przycisku do przekierowania') }}
                                    </div>
                                    <div class="col-sm-12" style="margin-bottom: 15px;">
                                        {{ form_errors(answeringForm.buttonNumber) }}
                                        {{ form_widget(answeringForm.buttonNumber) }}
                                    </div>
                                </div>
etc .....

我的构建表单

public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder->add('uid', ChoiceType::class, array(
                    'choices'   => array('Dla dzwoniących' => 1, 'Oddzwaniająca' => 2),
                    'required'  => false,
                    'label' => 'Wybierz maszynę',
            ))
            ->add('buttonNumber', ChoiceType::class, array(
                    'choices'   => array('1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9),
                    'required'  => false,
                    'label' => 'Numer przycisku'
            ))
            ->add('linkToMusic', TextType::class, [
                'label' => 'Adres URL do muzyki',
                'required' => true
            ])
            ->add('telephoneNumber', PhoneNumberType::class, [
                'label' => 'Numer Telefonu',
                'required'  => false,
                'widget' => PhoneNumberType::WIDGET_COUNTRY_CHOICE,
                'preferred_country_choices' => ['PL']
            ])
            ->add('acceptRegulations', ChoiceType::class, array(
                    'choices'   => array('1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9),
                    'required'  => false,
                    'label' => 'Numer przycisku do akceptacji regulaminu'
            ))
            ->add('submit', \Symfony\Component\Form\Extension\Core\Type\SubmitType::class, array(
                'label' => 'Zapisz'
            ));
}

我添加了$buttoNumber$linkToMusic等,但我没有添加$uid 我尝试清除缓存,但我不知道自己有问题。帮帮我:)

2 个答案:

答案 0 :(得分:3)

'choices'   => array('Dla dzwoniących' => 1, 'Oddzwaniająca' => 2),

传递一个整数,但需要传递VoiceAnswerMachine

类型的实体

答案 1 :(得分:0)

为了补充先前的错误答案...

您可以更改

->add('buttonNumber', ChoiceType::class, array(
                    'choices'   => array('1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9),
                    'required'  => false,
                    'label' => 'Numer przycisku'
            ))

作者

->add('buttonNumber', EntityType::class, array(
        'class' => 'AppBundle:ButtonNumber',
        'label' => 'Button number',
    ))

在您的实体中设置__toString()方法

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

记住要实现:

use Symfony\Bridge\Doctrine\Form\Type\EntityType;

在表单类中。