如何在 FUEL / ActiveRecord 中插入 NULL 值

发布于 2024-10-27 01:08:40 字数 2687 浏览 8 评论 0原文

简而言之:我试图在 FUEL 论坛上询问这个问题,但每次我尝试注册时,他们的论坛都会显示“发送激活电子邮件失败”,并且我无法登录或重置我的帐户。所以希望这里的人们能够检查一下。我之前在这个网站上见过一些 FUEL 的开发者。

这是一个 mysql 表示例:

CREATE TABLE `test` (
  `user_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  `username` VARCHAR( 128 ) NULL ,
  `last_login` DATETIME NULL
) ENGINE = InnoDB 

显然,与很多人不同,我个人喜欢利用数据库的 NULL 值。通俗地说,NULL 意味着它从未被初始化过。在这种情况下,如果我们的用户从未登录过,我希望通过将 last_login 值设置为 NULL 来反映在他的记录中。因此,NULL =“从未登录过”。

如果我要通过命令行或 phpMyAdmin 等方式向数据库添加新用户,我会输入以下查询。

INSERT INTO `test` (
    `user_id` ,
    `username` ,
    `last_login`
)
VALUES (
    NULL , 'test_person_1', NULL
);

这是该查询的结果。

+---------+---------------+--------------+
| user_id |    username   |  last_login  |
+---------+---------------+--------------+
|    1    | test_person_1 |     NULL     |
+---------+---------------+--------------+

现在,让我们使用 FUEL 的 ActiveRecord

这是我的模型(足够简单):

<?php

class Model_Test extends ActiveRecord\Model { 

    public $primary_key = 'user_id';
    public $table_name = 'test';  // Why does fuel want to pluralize table names?  Grr.

}

这是一个将记录插入表中的控制器方法的超级基本示例。我知道我永远不会想要一次又一次重复插入相同数据的操作。这只是一个测试。凉爽的?

public function action_save_example1()
{
    $o_user = new Model_Test(array(
        'username' => 'test_person_2',
    ));
}

这就是我运行该方法后得到的结果:

mysql> select * from test;
+---------+---------------+---------------------+
| user_id | username      | last_login          |
+---------+---------------+---------------------+
|       1 | test_person_1 | NULL                |
|       2 | test_person_2 | 0000-00-00 00:00:00 |
+---------+---------------+---------------------+
2 rows in set (0.00 sec)

请注意,test_person_2 的 DATETIME 字段是“0000-00-00 00:00:00”,这不是 NULL。即使我特别声明last_login为null,FUEL的ActiveRecord类也使得in不为null。例子。

public function action_save_example1()
{
    $o_user = new Model_Test(array(
        'username' => 'test_person_2',
        'last_login' => NULL
    ));
}

我有一种感觉,这是 ActiveRecord 正在运行的查询。

INSERT INTO `portal_links`.`test` (
    `user_id` ,
    `username` ,
    `last_login`
)
VALUES (
    NULL , 'test_person_2', ''
);

在插入或更新之前需要某种逻辑来测试值是否 === NULL,如果是 NULL,则应该使用关键字 NULL 而不是 ''。 CodeIgniter 的 ActiveRecord 类似乎理解 NULL 和 '' 之间的区别。

<?php
// CodeIgniter Example
$o_query = $this->db->insert('test', array(
   'user_id' => NULL,
   'username' => 'code_igniter_user',
   'last_login' => NULL
));

Brief forward: I tried to ask this on the FUEL forums, but every time I try and register, their forum says "Failed sending activation email" and I can't log in or reset my account. So hopefully folks here will check it out. I saw some of the developers of FUEL on this site before.

Here is an example mysql table:

CREATE TABLE `test` (
  `user_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  `username` VARCHAR( 128 ) NULL ,
  `last_login` DATETIME NULL
) ENGINE = InnoDB 

Evidently, unlike a lot of folks, I personally like to take advantage of the NULL value of databases. In layman's terms, NULL means it's never been initialized with a value. In this case if our user has never logged in, I want that reflected in his record by having the last_login value equal NULL. So, NULL = "never logged in."

If I was to add a new user to my database via the command line or through something like phpMyAdmin, I'd type in the following query.

INSERT INTO `test` (
    `user_id` ,
    `username` ,
    `last_login`
)
VALUES (
    NULL , 'test_person_1', NULL
);

This is the result of that query.

+---------+---------------+--------------+
| user_id |    username   |  last_login  |
+---------+---------------+--------------+
|    1    | test_person_1 |     NULL     |
+---------+---------------+--------------+

Now, let's use FUEL's ActiveRecord

Here is my model (simple enough):

<?php

class Model_Test extends ActiveRecord\Model { 

    public $primary_key = 'user_id';
    public $table_name = 'test';  // Why does fuel want to pluralize table names?  Grr.

}

And here is a super-basic example of a controller method that inserts a record into the table. I know I would never want an action that repeatedly inserts the same data over and over again. This is just a test. Cool?

public function action_save_example1()
{
    $o_user = new Model_Test(array(
        'username' => 'test_person_2',
    ));
}

And this is what I get after running that method:

mysql> select * from test;
+---------+---------------+---------------------+
| user_id | username      | last_login          |
+---------+---------------+---------------------+
|       1 | test_person_1 | NULL                |
|       2 | test_person_2 | 0000-00-00 00:00:00 |
+---------+---------------+---------------------+
2 rows in set (0.00 sec)

Notice that test_person_2's DATETIME field is "0000-00-00 00:00:00" That's not NULL. Even if I specifically state that last_login is null, FUEL's ActiveRecord class makes in not null. Example.

public function action_save_example1()
{
    $o_user = new Model_Test(array(
        'username' => 'test_person_2',
        'last_login' => NULL
    ));
}

I have a feeling that this is the query that ActiveRecord is running.

INSERT INTO `portal_links`.`test` (
    `user_id` ,
    `username` ,
    `last_login`
)
VALUES (
    NULL , 'test_person_2', ''
);

There needs to be some sort of logic to test if a value === NULL before inserting or updating and if it is NULL, it should use the keyword NULL and not ''. CodeIgniter's ActiveRecord class seems to understand the difference between NULL and ''.

<?php
// CodeIgniter Example
$o_query = $this->db->insert('test', array(
   'user_id' => NULL,
   'username' => 'code_igniter_user',
   'last_login' => NULL
));

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

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

发布评论

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

评论(1

吻泪 2024-11-03 01:08:41

这并不是你问题的答案。但 AR 已经或刚刚失去了支持。查看新的 orm 包。文档正在制作中。

祝你好运。

Not that this is the answer to your question. But AR is or just has lost support. Look into the new orm package. Docs are in the making.

Good luck.

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