dongniaocheng3773 2015-08-31 08:53
浏览 24
已采纳

如何在doctrine2实体中禁用字符串最大长度的隐式字符串修剪?

Using dontrine2, I have an entity having property with following annotations:

/**
 * @var string
 * @Column(type="string", length=4, nullable=false)
 */
protected $someProperty;

Now when the property is set to foobar, instead of getting an exception the string is trimmed to its first four characters, i.e. foob which is then stored into the database.

I know that I could implement a length check in the setter. Yet then I would configure the length of the string in two places which I rather avoid.

I also could use a validation, yet this also would double the configuration.

I find it confusing that doctrine2 shortens the string implicitly, yet when setting the value to null when nullable is false, I get an exception. This seems inconsistent.

Can I disable the trimming and force an exception? Or do I have to validate the entity myself?

  • 写回答

1条回答 默认 最新

  • duanjie6912 2015-08-31 14:27
    关注

    Doctrine does not do any validation neither any string truncation. All exceptions you get comes from database layer.

    When you try to insert null value into a column which is not allowed to be null database query fails.

    When you try to insert a string value longer than length defined in a column definition MySql automatically truncate it. You can change this behaviour configuring MySql to use STRICT mode.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思