Ar Problem/newbie Question

Hi all,

I’m yii newbie. I have strange problem:




$results = Task::model()->findAll('title like \'%'.addslashes($term).'%\'');

// $results = Task::model()->findAll('title like \'%:tt%\'', array(':tt'=>$term));



First line returns results good, second does not (empty array). Does anybody know why this may happen?

See here.

Hi

A sort way to do that (without CDbCriteria) is


$results = Task::model()->findAll(array('condition'=>'title like :term', 'params'=>array(':term'=>$term)));

your first aproach is hacking way (strictly avoid it due to sql injection issues)

the second one has not the right syntax in Yii

:)

Thank you for answers.

My mistake was that i put % symbol in query and also quotes around parameter. It was necessary to put it in value, that is inserted instead of parameter.

So my initial code should be fixed like this:


 $results = Task::model()->findAll('title like :tt', array(':tt'=>'%'.$term.'%'));

It is right form of findAll call according with yii documentation and link that Da:Sourcerer provided.

Again thank you for answers :) .

Yes it chould be with this way,

But I suggest you to use the best Yii way for each things :)

Well, you are right. Your way is better, because it gives ability to specify not only condition, but much more. It gives more flexibility. :)

Hi I will post the some example i hope it’s may be someone help in feauture.


<?php

 

$c = new CDbCriteria();

Hoge::model()->findAll($c);

// SELECT * FROM `hoge` `t`

 

 

$c = new CDbCriteria();

$c->addSearchCondition('t.fuga', null); // null, false, ''などは無視されるようです

Hoge::model()->findAll($c);

// SELECT * FROM `hoge` `t`

 

 

$c = new CDbCriteria();

$c->addSearchCondition('t.fuga', 'a');

Hoge::model()->findAll($c);

// SELECT * FROM `hoge` `t` WHERE t.fuga LIKE :ycp0. Bound with :ycp0='%a%'

 

 

$c = new CDbCriteria();

$c->addSearchCondition('t.fuga', 'a%', false);

Hoge::model()->findAll($c);

// SELECT * FROM `hoge` `t` WHERE t.fuga LIKE :ycp0. Bound with :ycp0='a%'

 

 

$c = new CDbCriteria();

$c->addSearchCondition('t.fuga', 'a');

$c->addSearchCondition('t.piyo', 'd');

Hoge::model()->findAll($c);

// SELECT * FROM `hoge` `t` WHERE (t.fuga LIKE :ycp0) AND (t.piyo LIKE :ycp1). Bound with :ycp0='%a%', :ycp1='%d%'

 

 

$c = new CDbCriteria();

$c->addSearchCondition('t.fuga', 'a', true, 'OR');

$c->addSearchCondition('t.piyo', 'd', true, 'OR');

Hoge::model()->findAll($c);

// SELECT * FROM `hoge` `t` WHERE (t.fuga LIKE :ycp0) OR (t.piyo LIKE :ycp1). Bound with :ycp0='%a%', :ycp1='%d%'

 

 

$c = new CDbCriteria();

$c->compare('t.fuga', 'a', true, 'OR');

$c->compare('t.piyo', 'b', true, 'OR');

Hoge::model()->findAll($c);

// SELECT * FROM `hoge` `t` WHERE (t.fuga LIKE :ycp0) OR (t.piyo LIKE :ycp1). Bound with :ycp0='%a%', :ycp1='%b%'

 

 

Hoge::model()->findAll('t.fuga LIKE :fuga', array(':fuga' => '%a%'));

// SELECT * FROM `hoge` `t` WHERE t.fuga LIKE :fuga. Bound with :fuga='%a%'

 

 

Hoge::model()->findAll(array(

'condition' => 't.fuga LIKE :fuga',

'params' => array(':fuga' => '%a%'),

));

// SELECT * FROM `hoge` `t` WHERE t.fuga LIKE :fuga. Bound with :fuga='%a%'

 

 

$c = new CDbCriteria();

$c->condition = 't.fuga LIKE :fuga';

$c->params = array(':fuga' => '%a%');

Hoge::model()->findAll($c);

// SELECT * FROM `hoge` `t` WHERE t.fuga LIKE :fuga. Bound with :fuga='%a%'