CDbCommand

Hi all!

I think I’m not understanding quite well some basic concepts, I have this query I have to run, it searches some records and if doesn’t find any, changes some conditions. I there another way of doing this?




$session = Yii::app()->session;

$command = $connection->createCommand('

					SELECT 

 	 					currency.id AS "currency.id",

  						country.id AS "country.id",

  						country.language

					FROM

  						country

  						INNER JOIN currency ON (country.currency_id = currency.id)

					WHERE country.language="'.Yii::app()->request->preferredLanguage.'"');

$data = $command->queryRow();


if(!$data){

$command = $connection->createCommand('

					SELECT 

 	 					currency.id AS "currency.id",

  						country.id AS "country.id",

  						country.language

					FROM

  						country

  						INNER JOIN currency ON (country.currency_id = currency.id)

					WHERE country.language="en_us"');

$data = $command->queryRow();

}

Of course I could separate the sql string in two and then just change the where part. But I’m sure Yii must have some way of dealing with this. I just don’t know a way to get there.

Thanks for your help

BTW: I’m loving it

Is there a reason your not using AR?


Country::model()->with('currency')->find('language=:lang', array('lang'=>Yii::app()->request->preferredLanguage));

Of course you will first need to create the Country model class and set up the relations properly

That is one of my big doubts, is AR slower than a simple query? Should I use AR only when want to do CRUD operations and "createCommands" if I want to read only from the table, or should I use AR for everything?

Thanks for your help.

As a general rule you should use AR for everything - until you come across something that you can’t do easily with AR - then you can drop into SQL. But even then, you’re generally better using ModelName::model()->findAllBySql() than going into createCommand.

Your example should definitely use AR.

If your concern is over speed, you can later add in database caching, or view caching. But first, focus on getting your app up and running - then you can go in and start optimizing for speed.

I use AR for everything except when I need to return a large result sets of for example 1000 rows (once I needed to do this to generate statistical graphs). In those cases, if you use AR you can run out of memory. AR is way easier to work with though, as well as very powerful, so I use it whenever I can, which is like 95% if the time for me.

I have not run into a case yet that I wasn’t able to use AR because it did not have a ability I needed

I see… in fact I started using AR then changed for the createCommand, I will change back to AR, as I’m a noobie I will follow your suggestion.

Thanks a lot.

I’ve struggling for 3 months with another popular framework, the results were NULL, I must say this one rocks… The learning curve in this one is very good, and the documentation exceptional, even for a noob like me.

Thanks