Hi,
I believe I might have found a bug in the
CDbCriteria->addSearchCondition()
method.
I have the following data:
ID (int) | NAME (varchar) | ACTIVE (int)
-----------------------------------------
1 | One | 1
1 | Two | 0
1 | Three | 1
1 | Four | 0
1 | Five | 1
Test 1
$model = Bugtest::model()->findAll();
Query Generated:
SELECT * FROM `bugtest` `t`
Gives the correct results. The whole table.
Test 2
$criteria=new CDbCriteria;
$criteria->select='id, name, active';
$criteria->addSearchCondition('active', 1);
$searchone = Bugtest::model()->findAll($criteria);
Query Generated:
SELECT id, name, active FROM `bugtest` `t` WHERE active LIKE :ycp0
Where
:ycp0 = 1
Give the correct results. Only values where active is 1.
Test 3
$criteria=new CDbCriteria;
$criteria->select='id, name, active';
$criteria->addSearchCondition('active', 0);
$searchtwo = Bugtest::model()->findAll($criteria);
Query Generated:
SELECT * FROM `bugtest` `t`
Gives incorrect results. Returns the whole table like
findAll()
.
From what I can see,
$criteria->addSearchCondition('active', 0);
is just being ignored and dropped.
I am aware of other ways to do this search, but this behaviour was strange to me.
Any comments? Is it a bug or is it by design?
Info
OS: Windows 7
Server: Apache 2.2.17
PHP: 5.3.4
Yii: 1.1.8
MySQL: 5.5.8
Full sample project can be found here
dev.donovansolms.com/bugs/cdbcriteria-bugtest.zip
It includes the SQL for the table. All you need to do is set the database password in main config.