Yii 1.1: Getting nulls from database instead of empty strings


To get null from database instead of empty strings you need to set up your DB connection as follows:

  'connectionString' => 'mysql:host=localhost;dbname=test',
  'username' => 'root',
  'password' => '',
  'charset' => 'utf8',
  // here it is:
  'nullConversion' => PDO::NULL_EMPTY_STRING,

Total 2 comments

#4023 report it
samdark at 2011/05/29 02:32pm

Thanks for correction. Fixed.

#4020 report it
phtamas at 2011/05/29 10:07am
it actually works in the opposite direction

I tried it with PDO_MYSQL driver and it seems that the conversion occurs on read operations and not on save, which can make a significant difference in some circumstances (e.g. nullable temporal columns, queries using IS NULL).

Leave a comment

Please to leave your comment.

Write new article
  • Written by: samdark
  • Category: Tips
  • Yii Version: 1.1
  • Votes: +13
  • Viewed: 10,335 times
  • Created on: May 7, 2011
  • Last updated: May 29, 2011
  • Tags: database, null