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.

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

报告相同问题?