Reputation: 245
I'm getting an exception when trying to save an Office object to the database using Symfony/Doctrine as follows:
An exception occurred while executing 'UPDATE offices SET county_postcode = ? WHERE id = ?' with params [{}, 1]:
SQLSTATE[HY000]: General error: 1366 Incorrect integer value: 'Co. Longford' for column 'county_postcode' at row 1
My Office class contains a mapping to the County class as follows:
/**
* @ORM\Entity
* @ORM\Table(name="offices")
*/
class Office
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @param mixed $id
/**
* @ORM\Column(type="integer", length=11 )
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\County")
*/
private $countyPostcode;
My OfficeType uses the EntityType
class OfficeType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('officeName')->add('addressLine1')->add('addressLine2')- >add('addressLine3')
->add('eirCode')->add('landlineTelephone')->add('mobileTelephone')->add('isActive')
->add('countyPostcode', EntityType::class, array(
// query choices from this entity
'class' => 'AppBundle:County',
'choice_label' => function ($county) {
return $county->getCountyName();
}));
}
This works fine and renders the countyNames as strings in the select control on the form. Checking the form data in my browser shows that the form contains the correct integer values as does inspecting the object using var_dump($office) but as per the error at the start of the question it throws
SQLSTATE[HY000]: General error: 1366 Incorrect integer value: 'Co. Kilkenny' for column 'county_postcode' at row 1
['Co. Kilkenny' is the string value, not the related integer type]
I've tried the solution noted here and added __toString() method to my County class but this didn't resolve the issue
public function __toString() {
return $this->countyName; }
Anyone any suggestions?
Thanks
John
Edit- eventually figured it out, thanks in part to Malcolm's comment
Apparently you can’t have both an
@ORM\Column
and
@ORM\JoinColumn
annotations together on the same field so the annotation needs to become
/**
* @ORM\ManyToOne(targetEntity="County")
* @ORM\JoinColumn(name="county_postcode", referencedColumnName="id")
*/\
Upvotes: 1
Views: 1921
Reputation: 245
As noted in the edits above, it's wasn't sufficient to remove the @ORM\Column annotation, this simply broke the views.
The issue is that you can't have both
@ORM\Column
and
@ORM\JoinColumn
annotations together on the same field
So the correct annotation needs to become
/**
* @ORM\ManyToOne(targetEntity="County")
* @ORM\JoinColumn(name="county_postcode", referencedColumnName="id")
*/\
One other step that's also essential is to ensure that the referenced entity, in this case County, defines a _toString() method which returns the String name of referenced entity
public function __toString() {
return $this->countyName; }
Thanks to both Malcolm and Renan for their help in resolving this
Upvotes: 1
Reputation: 572
The association mapping between "Office" and "County" is wrongly defined, see http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#many-to-one-unidirectional.
Remove this line * @ORM\Column(type="integer", length=11 )
and update your schema.
Upvotes: 1