DB Query Help

Hi dears,

New to Yii.

Please anyone can give a detailed example of making DB Queries in Yii.

Thanks

As you are new… you can start by reading the Definitive guide to Yii and the Yii Blog Tutorial to get the besics (and more) of Yii…

Here is simplest one:




	$criteria=new CDbCriteria;

        $criteria->select='id,hotel,stars'; //wich fields you want to select

        $criteria->condition='stars>3' // selects hotels with stars more than 3



I have studied the guide but I am bit confused about using the Active Record.

Will there be any help in understanding the operation and working example in detail?

Thanks

The guide is very good about this. Do you have a webapp created yet? If so, you can enable gii in your /protected/config/main.php file, then visit

http://myapp/index.php?r=gii

This will allow you to create a model based on your table. Once you have this, you can do queries like this

<?php

$hotels = hotel::model()->findAll();

foreach($hotels as $hotel) {

echo &#036;hotel-&gt;name;


echo &#036;hotel-&gt;stars;

}

?>

A bit off-topic but since reading some late posts like this one I’m finally thinking that this should be said!

I would follow mdomba’s suggestion and I would be a little bit harsh this time. IMHO it can’t be explained easier than it was done in Yii Blog Guide. This should be a major, first read upon first approach to Yii by new user. If one reads it thoroughly (really read it from beginning till end) and still claims it is to hard for him to understand it, he should not start programming in Yii, because it is a waste of time for him (that is that harsh part)! I say it again - it can’t be explained easier than it was done there.

Yes - official guide can be a bit misleading at first sight (as I can prove it basing on my own experience) it is far longer than Blog guide and not so example-focused. But if someone spend let’s say half a day (2-3 hours if he is a quick reader) on reading Yii Blog Guide and maybe a few articles from Cookbook and will do this really honest - there is no option that Yii will turn out to be too hard to understand! No, way…

Dear,

Thanks for your replies.

As I want to develop my existing web application on Yii framework, so I was very fast in getting the desired results. I just rush through the guide and understand basic concepts of working with the Yii.

After understanding the Relations in Yii, which are basically related to the different tables of the database and strongly based on the PK.

I get frustrated when I want to build the relation between two tables using the field of the tables rather then PKs or FKs.

I want to ask that Is there any method to build the relations based on table fields?

@Tregder

Dear,

Thanks for your comments and suggestions. I would like to fellow your suggestions.

Thanks

It’s a good idea to search the forum… befor asking…

Check this thread - http://www.yiiframework.com/forum/index.php?/topic/4148-ar-relation-foreign-key