I have created a form that appears to be correct, it has a few text fields and a select box with a list of countries pulled from a table of countries I have. The select box displays correctly using the the correct values for it's 'value' and display text. When I submit the form however I get an exception:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'countryid' cannot be null
If I set the database table (in PHPMyAdmin) to allow a null value for the countryid field it enters the record with no exception but the entry for the countryid is null.
my controller has the following code:
$duck = new \Wfuk\DuckBundle\Entity\Ducks();
$form = $this->createFormBuilder($duck)
->add('city', 'text')
->add('countryid', 'entity', array('class' => 'WfukDuckBundle:Country', 'property' => 'country'))
// cut other fields
->getForm();
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
$errors = $this->get('validator')->validate( $form );
echo $duck->getCountryid();
if ($form->isValid()) {
$em = $this->getDoctrine()->getEntityManager();
$em->persist($duck);
$em->flush();
return $this->redirect($this->generateUrl('upload_duck_success'));
}
the echo in there returns the __toString function of the country object which seems a bit odd - but it is the full country info for the country chosen in the form.
in the Ducks.php class:
/**
* @var string $countryid
*
* @ORM\Column(name="countryid", type="string", length=2, nullable=false)
*/
private $countryid;
/**
* Set countryid
*
* @param string $countryId
*/
public function setCountryid($countryid)
{
$this->countryid = $countryid;
}
/**
* Get countryid
*
* @return string
*/
public function getCountryid()
{
return $this->countryid;
}
This is my first symfony project, but I've been over the docs several times and think I have everything set up ok...
edit:
I have a join set up as follows: Ducks.php
/**
* @ORM\ManyToOne(targetEntity="Country", inversedBy="ducks")
* @ORM\JoinColumn(name="countryid", referencedColumnName="id")
*/
private $country;
/**
* Set country
*
* @param string $country
*/
public function setCountry($country)
{
$this->country = $country;
}
/**
* Get country
*
* @return string
*/
public function getCountry()
{
return $this->country;
}
and on the Country.php side:
/**
* @ORM\OneToMany(targetEntity="Ducks", mappedBy="country")
*/
protected $ducks;
public function __construct()
{
$this->ducks = new ArrayCollection();
}
/**
* Get ducks
*
* @return Doctrine\Common\Collections\Collection
*/
public function getDucks()
{
return $this->ducks;
}
What's happening is that the form is sending an actual Country object to ducks. You can confirm this with:
It sounds like you only want to store a 2 char country code? You don't want an actual relation? If so then this might do the trick:
If you want an actual normal Doctrine managed relation between duck and country then something like:
And adjust your getter/setters accordingly.
It's a bit strange that you seem to have both yml and annotations. From what I understood, you could use one or the other in a given bundle.