【问题标题】:Symfony Entity Type reference contains String not integer when savingSymfony 实体类型引用在保存时包含字符串而不是整数
【发布时间】:2017-03-19 23:39:43
【问题描述】:

尝试使用 Symfony/Doctrine 将 Office 对象保存到数据库时出现异常,如下所示:

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

我的 Office 类包含到 County 类的映射,如下所示:

/**
* @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;

我的 OfficeType 使用 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();
            }));




}

这可以正常工作并将 CountyNames 呈现为表单上的选择控件中的字符串。在我的浏览器中检查表单数据显示表单包含正确的整数值,就像使用 var_dump($office) 检查对象一样,但根据它抛出的问题开始时的错误

SQLSTATE[HY000]:一般错误:1366 不正确的整数值:'Co.第 1 行的“county_postcode”列的 Kilkenny'

['Co. Kilkenny'是字符串值,不是相关的整数类型]

我已经尝试了here 的解决方案并将 __toString() 方法添加到我的 County 类,但这并没有解决问题

public function __toString() {
    return $this->countyName;    }

大家有什么建议吗?

谢谢

约翰

编辑- 最终想通了,部分感谢马尔科姆的评论

显然你不能同时拥有一个

@ORM\Column 
and  
@ORM\JoinColumn

注释放在同一个字段上,所以注释需要变成

    /**
     * @ORM\ManyToOne(targetEntity="County")
    * @ORM\JoinColumn(name="county_postcode",    referencedColumnName="id")
       */\

【问题讨论】:

  • 删除@ORM\Column(type="integer", length=11 )
  • 感谢您的建议,不幸的是,这样做会破坏所有视图索引、显示、编辑等。还会抛出 SQLSTATE[42S22]: Column not found: 1054 Unknown column 't0.county_postcode_id' in '字段列表'

标签: php symfony doctrine-orm


【解决方案1】:

“办公室”和“县”的关联映射定义错误,见http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#many-to-one-unidirectional

删除此行 * @ORM\Column(type="integer", length=11 ) 并更新您的架构。

【讨论】:

  • 谢谢 Renan,是的,我几分钟前刚刚用解决方案编辑了问题
【解决方案2】:

如上面的编辑所述,删除 @ORM\Column 注释是不够的,这只会破坏视图。

问题是你不能两者兼得 @ORM\列 和
@ORM\JoinColumn

注释放在同一个字段上

所以正确的注解需要变成

/**
     * @ORM\ManyToOne(targetEntity="County")
    * @ORM\JoinColumn(name="county_postcode",    referencedColumnName="id")
       */\

另一个同样重要的步骤是确保被引用实体(在本例中为 County)定义一个 _toString() 方法,该方法返回被引用实体的字符串名称

 public function __toString() {
        return $this->countyName;    }

感谢 Malcolm 和 Renan 帮助解决此问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-17
    • 2019-08-12
    • 1970-01-01
    • 1970-01-01
    • 2015-02-20
    • 2013-09-04
    相关资源
    最近更新 更多