English 中文(简体)
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 ActiveRecordModel { 

    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
));
问题回答

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.





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签