Ich bin ziemlich neu in Symfony, aber nicht in PHP Web-Entwicklung im Allgemeinen. Allerdings habe ich große Schwierigkeiten, ein "dynamisches" Formular zu erhalten, um mich erfolgreich einzureichen.Symfony 3 spl_object_hash erwartet, dass Parameter 1 Objekt, Integer beim Übermitteln des Formulars ist
Ich habe die folgenden Informationen verfolgt: http://symfony.com/doc/current/form/dynamic_form_modification.html#form-events-submitted-data.
Ich habe eine Profile
Einheit, die eine ManyToOne Beziehung zu den Country
und Area
Einheiten hat. Area
hat eine ManyToOne-Beziehung zu Country
.
Auf dem Formular wird das Entitätsauswahlelement Area
je nach dem Wert Country
dynamisch ausgewählt. Das funktioniert OK. Wie auch immer, wenn ich das Formular abschicke, erhalte ich den folgenden Fehler:
Warning: spl_object_hash() expects parameter 1 to be object, integer given 500 Internal Server Error - ContextErrorException
.
Betrachtet man die Stack-Trace, scheint dies von Methoden zur Verflachung meiner Area
Entity Choice-Array stammen.
Alle und alle Hilfe ist willkommen - ich habe 2 Tage damit verbracht, dies zu betrachten und ich habe nicht viel weiter!
Im Folgenden finden Sie einige Codeinformationen. Wenn es weitere Informationen gibt, die ich zur Verfügung stellen kann, fragen Sie bitte!
Dank
t2t
Meine ProfileType
Klasse AbstractType
und enthält meine buildForm
Routine, die die Form baut erstreckt.
Innerhalb ProfileType
buildForm
Ich habe den folgenden Code:
// Location Country & Area
// Start: Dynamic form stuff
// http://symfony.com/doc/current/form/dynamic_form_modification.html#form-events-submitted-data
$builder
->add('locationCountry', EntityType::class, [
'class' => 'AppBundle:Country',
'placeholder' => '',
]);
$formModifier = function (FormInterface $form, Country $country = null) {
$arChoices = array();
if (!is_null($country)) {
$arChoices = $this->em->getRepository('AppBundle:Area')->findByCountry($country);
}
$areas = null === $country ? array() : $arChoices;
$form->add('locationArea', EntityType::class, [
'class' => 'AppBundle:Area',
'placeholder' => '',
'choices' => $areas
]);
};
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) use ($formModifier) {
// this would be your entity, i.e. Profile
$data = $event->getData();
$formModifier($event->getForm(), $data->getLocationCountry());
}
);
$builder->get('locationCountry')->addEventListener(
FormEvents::POST_SUBMIT,
function (FormEvent $event) use ($formModifier) {
// It's important here to fetch $event->getForm()->getData(), as
// $event->getData() will get you the client data (that is, the ID)
$country = $event->getForm()->getData();
// since we've added the listener to the child, we'll have to pass on
// the parent to the callback functions!
$formModifier($event->getForm()->getParent(), $country);
}
);
// End: Dynamic form stuff
Country
ist definiert als:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Country
*
* @ORM\Table(name="Country")
* @ORM\Entity(repositoryClass="AppBundle\Repository\CountryRepository")
*/
class Country
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=100, unique=true)
*/
private $name;
/**
* @var bool
*
* @ORM\Column(name="visible", type="boolean")
*/
private $visible;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*
* @return Country
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set visible
*
* @param boolean $visible
*
* @return Country
*/
public function setVisible($visible)
{
$this->visible = $visible;
return $this;
}
/**
* Get visible
*
* @return bool
*/
public function getVisible()
{
return $this->visible;
}
public function __toString() {
return $this->getName();
}
}
Area
ist definiert als:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Area
*
* @ORM\Table(name="Area")
* @ORM\Entity(repositoryClass="AppBundle\Repository\AreaRepository")
*/
class Area
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Country")
*/
private $country;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*
* @return Area
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
public function __toString() {
return $this->name;
}
/**
* Set country
*
* @param \AppBundle\Entity\Country $country
*
* @return Area
*/
public function setCountry(\AppBundle\Entity\Country $country = null)
{
$this->country = $country;
return $this;
}
/**
* Get country
*
* @return \AppBundle\Entity\Country
*/
public function getCountry()
{
return $this->country;
}
}
und dem entsprechenden Teil des Profile
ist definieren d als:
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Country")
*/
private $locationCountry;
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Area")
*/
private $locationArea;
/**
* Set locationArea
*
* @param \AppBundle\Entity\Area $locationArea
*
* @return Profile
*/
public function setLocationArea(\AppBundle\Entity\Area $locationArea = null)
{
$this->locationArea = $locationArea;
return $this;
}
/**
* Get locationArea
*
* @return \AppBundle\Entity\Area
*/
public function getLocationArea()
{
return $this->locationArea;
}
/**
* Set locationCountry
*
* @param \AppBundle\Entity\Country $locationCountry
*
* @return Profile
*/
public function setLocationCountry(\AppBundle\Entity\Country $locationCountry = null)
{
$this->locationCountry = $locationCountry;
return $this;
}
/**
* Get locationCountry
*
* @return \AppBundle\Entity\Country
*/
public function getLocationCountry()
{
return $this->locationCountry;
}
schließlich in meinem AreaRepository
ich habe folgendes:
namespace AppBundle\Repository;
use AppBundle\Entity\Area;
/**
* AreaRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class AreaRepository extends \Doctrine\ORM\EntityRepository
{
/**
* @param $oCountry
* @return array
*/
public function findByCountry($oCountry) {
if (is_null($oCountry)) {
return array();
}
$oRepo = $this->getEntityManager()->getRepository('AppBundle:Area');
$oQB = $oRepo->createQueryBuilder('a');
$oQuery = $oQB->where('a.country = :countryId')
->setParameter('countryId', $oCountry)
->getQuery();
$arResult = $oQuery->getArrayResult();
return $arResult;
}
}
Doh !! Danke das scheint dieses Problem behoben zu haben! Haben Sie Ihre Antwort mit Dankbarkeit angenommen! t2t – tip2tail
Sie sind herzlich willkommen.Glück, dass es geholfen hat. – Mawcel