【问题标题】:Expected argument of type "AppBundle\Entity\MyMachine or null", "integer" given给定“AppBundle\Entity\MyMachine 或 null”、“整数”类型的预期参数
【发布时间】:2017-07-07 13:22:01
【问题描述】:

我在将数据添加到我的数据库时遇到问题。

我有错误

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

我的树枝

<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 我尝试清除我的缓存,但我不知道我有问题。帮帮我:)

【问题讨论】:

  • 在数据库中添加数据的代码(例如表单类型)会有帮助
  • 我实现了我的帖子:)
  • public function setUid(\AppBundle\Entity\VoiceAnswerMachine $uid = null) 被一个整数调用。确保这不是您的框架的自动行为。

标签: php symfony doctrine twig


【解决方案1】:
'choices'   => array('Dla dzwoniących' => 1, 'Oddzwaniająca' => 2),

你传递一个整数,但你需要传递一个 VoiceAnswerMachine 类型的实体

【讨论】:

【解决方案2】:

为了补充之前糟糕的答案...

你可以改变

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

在你的表单类中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多