Yii Framework Forum: CDbCommand - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

CDbCommand createCommand Rate Topic: -----

#1 User is offline   tuga 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 68
  • Joined: 02-January 10

Posted 21 January 2010 - 01:57 PM

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
0

#2 User is offline   jonah 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 733
  • Joined: 27-November 08
  • Location:California (US)

Posted 21 January 2010 - 03:32 PM

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
http://php-thoughts.cubedwater.com - my bloggings about Yii
0

#3 User is offline   tuga 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 68
  • Joined: 02-January 10

Posted 21 January 2010 - 04:09 PM

View Postjonah, on 21 January 2010 - 03:32 PM, said:

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.
0

#4 User is offline   Alex Muir 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 96
  • Joined: 25-April 09

Posted 21 January 2010 - 04:35 PM

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.
My sites: The Big Eat and The Big Space
0

#5 User is offline   jonah 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 733
  • Joined: 27-November 08
  • Location:California (US)

Posted 21 January 2010 - 04:58 PM

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
http://php-thoughts.cubedwater.com - my bloggings about Yii
0

#6 User is offline   tuga 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 68
  • Joined: 02-January 10

Posted 21 January 2010 - 05:37 PM

View Postjonah, on 21 January 2010 - 04:58 PM, said:

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
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users