Active Record doesn't handle 'NULL' columns? [SOLVED] - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Active Record doesn't handle 'NULL' columns? [SOLVED] (/showthread.php?tid=43316) |
Active Record doesn't handle 'NULL' columns? [SOLVED] - El Forum - 07-07-2011 [eluser]Jaketoolson[/eluser] I'm running a UNION on 2 SELECT queries. One of the select creates a fictitious column, setting the value to NULL (i.e. NULL AS sub_cat). This is done because when the 2 queries are combined, the other select query has values for this column. When running this across multiple platforms using various methods (MS Access, MySQL Query Browser, phpMyAdmin, I have no problems except when I use the active record method in CI. Code: $sql = "sub.model_id, sub.sub_cat I receive the following error: Quote:A Database Error Occurred When I switch CI query methods: Code: //from this The correct results are produced: Code: model_id sub_cat Active Record doesn't handle 'NULL' columns? [SOLVED] - El Forum - 07-07-2011 [eluser]Phil_B[/eluser] Hi Jake, You have all your SQL in the select portion of the ActiveRecord query. This may or may not work but try this instead: Code: $from = '( Active Record doesn't handle 'NULL' columns? [SOLVED] - El Forum - 07-07-2011 [eluser]jmadsen[/eluser] why not just skip ActiveRecord, since you aren't actually using it? Code: $query = $this->sess_db->query($sql); Active Record is just a query building library. You don't have to use it. http://ellislab.com/codeigniter/user-guide/database/examples.html Active Record doesn't handle 'NULL' columns? [SOLVED] - El Forum - 07-07-2011 [eluser]Jaketoolson[/eluser] Quote:why not just skip ActiveRecord, since you aren’t actually using it? I was just being consistent with the other functions in the class I'm writing and put no thought into whether or not I need to use ActiveRecord. Also, for debugging purposes, I begin with a "full query" that I know works and chain it down. Breaking my query apart into a very simple SELECT using ActiveRecord, while still forcing a NULL, still produces the same error: Code: $this->sess_db->select("models.model_id, NULL AS sub_cat"); Quote:A Database Error Occurred I believe this to be a bug. Active Record doesn't handle 'NULL' columns? [SOLVED] - El Forum - 07-07-2011 [eluser]jmadsen[/eluser] If you print out your query, you will see that NULL is being "pipped" as `NULL` Add "false" as a second argument to your ->select() and you will be fine. i.e., Code: $this->sess_db->select("models.model_id, NULL AS sub_cat", false); Active Record doesn't handle 'NULL' columns? [SOLVED] - El Forum - 07-07-2011 [eluser]Jaketoolson[/eluser] Thanks! Setting the 2nd parameter to false caused CI to not protect my fields with `backticks` which resolved the issue. |