0

I want to convert this SQL query to CDbCriteria format in Yii framework so I can use it in Yii:

SELECT title FROM project WHERE ((title like 'to %') or (title like ' % to %') or (title like '% to'));

Yiiframework CDbCriteria Description Link is as below:

http://www.yiiframework.com/doc/api/1.1/CDbCriteria

1
  • Just a heads-up: the above query will ignore records with only 'to' as the title. You might want to include a condition for this. Commented Jan 27, 2013 at 22:50

2 Answers 2

4

also you can try this

$criteria = new CDbCriteria;
$criteria->select = "title";
$criteria->compare('title',' to ',true);

$model = new Project;
$models = $model->findAll($criteria);
Sign up to request clarification or add additional context in comments.

2 Comments

This works only if you add a 3th param to compare with value 'true'
CDbCritera should be CDbCriteria
2

Here you go:

$criteria = new CDbCriteria;
$criteria->select = "title";
$criteria->condition = "((title like ':to%') or (title like ' % :to %') or (title like '% :to'))";
$criteria->params = array(':to' => 'to');

$model = new Project;
$models = $model->findAll($criteria);

1 Comment

CDbCritera should be CDbCriteria

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.