开发者

How do I insert a NULL value in FUEL / ActiveRecord

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 开发者_如何学Pythonwant 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
));


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜