Reputation: 6560
I have this error:
Expected argument of type "integer or null", "App\Entity\User" given
which I don't understand, well I understand the error, but I'm not sure why it's arising.
Here's my AddController.php file:
<?php
namespace App\Controller\Tag;
use App\Entity\Tag;
use App\Form\Tag\AddType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\HttpFoundation\Request;
class AddController extends Controller
{
public function add(Request $request)
{
$tag = new Tag();
$form = $this->createForm(AddType::class, $tag);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$pageInfo = $form->getData();
$description = $pageInfo['description'];
$name = $pageInfo['name'];
$guru = $pageInfo['guru_id'];
$createdTs = new \DateTime();
$tag->setApproved(false);
$tag->setDescription($description);
$tag->setName($name);
$tag->setGuruId((is_int($guru) ? $guru : null));
$tag->setCreatedTs($createdTs);
try {
$entityManager->persist($tag);
$entityManager->flush();
$this->addFlash('success', 'Tag Submitted for review! '. $guru);
} catch (Exception $e) {
$this->addFlash('danger', 'Something went skew-if. Please try again.');
}
return $this->redirectToRoute('tag_add');
}
return $this->render('tag/add.html.twig', array('form' => $form->createView()));
}
}
the initial load works, but submitting the form generates the above error. But I'm not sure why. Even if I reorder my lines like this:
$tag = new Tag();
$tag->setGuruId(null);
my Tag entity:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\TagRepository")
*/
class Tag
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=125)
*/
private $name;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $description;
/**
* @ORM\Column(type="boolean")
*/
private $approved;
/**
* @ORM\Column(type="datetime")
*/
private $created_ts;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $last_edit_ts;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $guru_id;
public function getId()
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getApproved(): ?bool
{
return $this->approved;
}
public function setApproved(bool $approved): self
{
$this->approved = $approved;
return $this;
}
public function getCreatedTs(): ?\DateTimeInterface
{
return $this->created_ts;
}
public function setCreatedTs(\DateTimeInterface $created_ts): self
{
$this->created_ts = $created_ts;
return $this;
}
public function getLastEditTs(): ?\DateTimeInterface
{
return $this->last_edit_ts;
}
public function setLastEditTs(\DateTimeInterface $last_edit_ts): self
{
$this->last_edit_ts = $last_edit_ts;
return $this;
}
public function getGuruId(): ?int
{
return $this->guru_id;
}
public function setGuruId(?int $guru_id): self
{
$this->guru_id = $guru_id;
return $this;
}
}
my AddType
use App\Entity\Tag;
use App\Entity\User;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class AddType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', TextType::class)
->add('description', TextareaType::class)
->add('guru_id', EntityType::class, array(
'class' => User::class,
'choice_label' => 'username'
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Tag::class
));
}
}
it still gives the same error so I'm really not sure how it's being generated... how can I persist an integer (got from EntityType dropdown) to database?
Thanks
Upvotes: 0
Views: 1388
Reputation: 9575
Probably you have an EntityType
field within AddType
form to handle the guru_id
property and it is mapping the submitted value (instance of User
) automatically with setGuruId(?int $guru_id)
method. That's why that:
Expected argument of type "integer or null", "App\Entity\User" given
You've some options to fix that, but likely the best one is to change the guru_id
mapping to ManyToOne
relationship with User
:
// Tag entity
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User")
*/
private $guru;
public function getGuru(): ?User
{
return $this->guru;
}
public function setGuru(?User $guru): self
{
$this->guru = $guru;
return $this;
}
Also, I'd remove this code:
$tag->setDescription($description);
$tag->setName($name);
$tag->setGuruId((is_int($guru) ? $guru : null));
as this procedure is already done by Form
component after submit the data.
As a quick workaround you can fix this removing the typehint:
public function setGuruId($guru_id): self
{
$this->guru_id = $guru_id instanceof User ? $guru_id->getId() : $guru_id;
return $this;
}
Upvotes: 1