将字段设置为 null 时出现有关长度的意外学说验证错误

发布于 2024-09-15 11:51:37 字数 1161 浏览 5 评论 0原文

我有一个定义如下的字段:

class Subcategory extends BaseSubcategory {}

abstract class BaseSubcategory extends Doctrine_Record
{
    public function setTableDefinition()
    {
        // ...
        $this->hasColumn('meta_description', 'string', 255);
        // ...
    }

    // ...
}

这是表格的样子:

mysql> DESCRIBE subcategory;
+----------------------+------------------+------+-----+---------+----------------+
| Field                | Type             | Null | Key | Default | Extra          |
+----------------------+------------------+------+-----+---------+----------------+
| id                   | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
[...]
| meta_description     | varchar(255)     | YES  |     | NULL    |                |
[...]
+----------------------+------------------+------+-----+---------+----------------+
10 rows in set (0.00 sec)

这是我保存记录的代码

$m = new Subcategory;
// ...
$m->meta_description = null;
$m->save();

我收到以下验证错误

* 1 validator failed on meta_description (length)

为什么会发生这种情况?

I have a field that is defined as follows:

class Subcategory extends BaseSubcategory {}

abstract class BaseSubcategory extends Doctrine_Record
{
    public function setTableDefinition()
    {
        // ...
        $this->hasColumn('meta_description', 'string', 255);
        // ...
    }

    // ...
}

Here's what the table looks like:

mysql> DESCRIBE subcategory;
+----------------------+------------------+------+-----+---------+----------------+
| Field                | Type             | Null | Key | Default | Extra          |
+----------------------+------------------+------+-----+---------+----------------+
| id                   | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
[...]
| meta_description     | varchar(255)     | YES  |     | NULL    |                |
[...]
+----------------------+------------------+------+-----+---------+----------------+
10 rows in set (0.00 sec)

Here's my code to save a record

$m = new Subcategory;
// ...
$m->meta_description = null;
$m->save();

I'm getting the following validation error

* 1 validator failed on meta_description (length)

Why is this happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑忘罢 2024-09-22 11:51:37

上面的代码示例并没有说明全部情况。我被早期的保存误导了,其中 meta_description 字段超载了超过 255 个字符。虚惊!

The code samples above do not tell the whole story. I was being misled by an earlier save, in which the meta_description field was being overloaded with over 255 characters. False alarm!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文