duanlan4801 2019-02-10 19:48
浏览 36
已采纳

无法使用表单symfony在我的数据库中注册实体

I'm new with symphony and I try to register an entity in my base with form. Here is the form:

<?php
namespace App\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
use Symfony\Component\Form\Extension\Core\Type\TelType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;

class OfferType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('title', TextType::class, array('label' => '*Title: ' , 'attr' => array('class' => 'input2')))
        ->add('nameofgame', TextType::class, array('label' => 'Name of the Game:','attr' => array('class' => 'create button1')))
        ->add('description', TextType::class, array('label' => 'Description: ' , 'attr' => array('class' => 'input2')))
        ->add('price', MoneyType::class, array('label' => '*Price: ' , 'attr' => array('class' => 'input2')))
        ->add('numbertelephone', TelType::class, array('label' => 'Telephone Number:','attr' => array('class' => 'create button1')))
        ->add('save', SubmitType::class, array('label' => 'Post Offer','attr' => array('class' => 'create button1')))
    ;
}
}

Here is my controller:

/**
 * @Route("/BoardFind/Trade/RegisterOffer", name="RegisterOffer")
 * @param Request $request
 * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
 */
public function register(Request $request)
{
    $Offer = new TradeOffer();
    $form = $this->createForm(OfferType::class, $Offer);
    $form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {
        $Offer = $form->getData();
        echo maleee;
        $entityManager = $this->getDoctrine()->getManager();

        $session = $this->get('session');
        $userId = $session->get("id");
        $user = $this->getDoctrine()
            ->getRepository(User::class)->find($userId);
        $Offer->setUser($user);
        $Offer->setTraderName($user->getName());
        $Offer->setTraderLastName($user->getLastName());
        $Offer->setUsername($user->getUsername());
        $Offer->setTraderEmail($user->getEmail());

        $entityManager->persist($Offer);
        $entityManager->flush();

        $flashbag = $this->get('session')->getFlashBag();
        $flashbag->add("SuccessfullRegister", "You successfully registered your offer!");
        return $this->redirectToRoute('Trade');
    }

    return $this->render('home/RegisterOffer.html.twig', array(
        'form' => $form->createView(),
    ));

}

When I try to register an offer it says that "Expected argument of type "App\Entity\double", "double" given." but the thing is that I think the property of the entity is right. Here is the property:

/**
 * @Assert\Type("double")
 * @Assert\NotBlank()
 */
private $price;

If you want image of the error here it is. So where is the problem?

  • 写回答

1条回答 默认 最新

  • drq22639 2019-02-10 21:33
    关注

    In PHP double is an alias for float. When using scalar type hints or return types this alias is not supported and the actual type must be used instead. This behavior is comparable with bool being supported, but not boolean.

    In other words in your getters & setters you have to refer to to float instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝