PersonneController.php
<?php
namespace biblioBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use biblioBundle\Entity\Personne;
use biblioBundle\Form\PersonneType;
/**
* Personne controller.
*
*/
class PersonneController extends Controller
{
/**
* Lists all Personne entities.
*
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$personnes = $em->getRepository('biblioBundle:Personne')->findAll();
return $this->render('personne/index.html.twig', array(
'personnes' => $personnes,
));
}
/**
* Creates a new Personne entity.
*
*/
public function newAction(Request $request)
{
//$em = $this->getDoctrine()->getManager();
//$prf = $em->getRepository('biblioBundle:Profil')->findAll();
$personne = new Personne();
$form = $this->createForm(PersonneType::class, $personne);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($personne);
$em->flush();
return $this->redirectToRoute('personne_show', array('id' => $personne->getIdpersonne()));
}
return $this->render('personne/new.html.twig', array(
'personne' => $personne,
'form' => $form->createView(),
));
}
PersonneType.php
<?php
namespace biblioBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use biblioBundle\Entity\Profil;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
class PersonneType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('nom')
->add('prenom')
->add('datenaiss', 'date')
->add('adresse')
->add('photo1')
->add('sexe',ChoiceType::class,array(
'choices' => array(
'H' => 'Homme',
'F' => 'Femme')))
->add('idProfil',IntegerType::class);
}
/* public function getName()
{
return 'name';
}*/
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'biblioBundle\Entity\Personne'
));
}
}
Personne.php
<?php
namespace biblioBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Personne
*
* @ORM\Table(name="personne", indexes= {@ORM\Index(name="FK_Appartenir2", columns={"idProfil"})})
* @ORM\Entity
*/
class Personne
{
/**
* @var string
*
* @ORM\Column(name="Nom", type="string", length=20, nullable=false)
*/
private $nom;
/**
* @var string
*
* @ORM\Column(name="Prenom", type="string", length=20, nullable=false)
*/
private $prenom;
/**
* @var \DateTime
*
* @ORM\Column(name="DateNaiss", type="date", nullable=false)
*/
private $datenaiss;
/**
* @var string
*
* @ORM\Column(name="Adresse", type="string", length=100, nullable=true)
*/
private $adresse;
/**
* @var string
*
* @ORM\Column(name="Photo1", type="string", length=255, nullable=true)
*/
private $photo1;
/**
* @var string
*
* @ORM\Column(name="Sexe", type="string", length=1, nullable=false)
*/
private $sexe;
/**
* @var integer
*
* @ORM\Column(name="idPersonne", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $idpersonne;
/**
* @var \biblioBundle\Entity\Profil
*
* @ORM\ManyToOne(targetEntity="biblioBundle\Entity\Profil")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="idProfil", referencedColumnName="idProfil")
* })
*/
private $idprofil;
/**
* Set nom
*
* @param string $nom
* @return Personne
*/
public function setNom($nom)
{
$this->nom = $nom;
return $this;
}
/**
* Get nom
*
* @return string
*/
public function getNom()
{
return $this->nom;
}
/**
* Set prenom
*
* @param string $prenom
* @return Personne
*/
public function setPrenom($prenom)
{
$this->prenom = $prenom;
return $this;
}
/**
* Get prenom
*
* @return string
*/
public function getPrenom()
{
return $this->prenom;
}
/**
* Set datenaiss
*
* @param \DateTime $datenaiss
* @return Personne
*/
public function setDatenaiss($datenaiss)
{
$this->datenaiss = $datenaiss;
return $this;
}
/**
* Get datenaiss
*
* @return \DateTime
*/
public function getDatenaiss()
{
return $this->datenaiss;
}
/**
* Set adresse
*
* @param string $adresse
* @return Personne
*/
public function setAdresse($adresse)
{
$this->adresse = $adresse;
return $this;
}
/**
* Get adresse
*
* @return string
*/
public function getAdresse()
{
return $this->adresse;
}
/**
* Set photo1
*
* @param string $photo1
* @return Personne
*/
public function setPhoto1($photo1)
{
$this->photo1 = $photo1;
return $this;
}
/**
* Get photo1
*
* @return string
*/
public function getPhoto1()
{
return $this->photo1;
}
/**
* Set sexe
*
* @param string $sexe
* @return Personne
*/
public function setSexe($sexe)
{
$this->sexe = $sexe;
return $this;
}
/**
* Get sexe
*
* @return string
*/
public function getSexe()
{
return $this->sexe;
}
/**
* Get idpersonne
*
* @return integer
*/
public function getIdpersonne()
{
return $this->idpersonne;
}
/**
* Set idprofil
*
* @param \biblioBundle\Entity\Profil $idprofil
* @return Personne
*/
public function setIdprofil(\biblioBundle\Entity\Profil $idprofil = null)
{
$this->idprofil = $idprofil;
return $this;
}
/**
* Get idprofil
*
* @return \biblioBundle\Entity\Profil
*/
public function getIdprofil()
{
return $this->idprofil;
}
}
每当我尝试运行代码时,我得到:
{预期参数类型为“biblioBundle \ Entity \ Profil”,“整数”给出}
我需要帮助。
答案 0 :(得分:0)
在PersonneType
课程中,您有一行:
->add('idProfil',IntegerType::class);
在Personne
课程中你有:
public function setIdprofil(\biblioBundle\Entity\Profil $idprofil = null)
{
$this->idprofil = $idprofil;
return $this;
}
您需要Profil
实例而不是整数(在表单中定义)。怎么解决这个问题?
IntegerType
课程改为EntityType
。integer
并转换为Profil
,反之亦然。integer
类型而不是Profil
。