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.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度