Basic sql question

Sorry if this is duplicating an earlier question but I am quite new to the Yii framework and this is a real basic question.

All I want to do is query a table get all the values in a certain column and then display all of them in a view. Here is what some of the code from the view looks like, <? echo CHtml::link($content, array(‘controller/action’)); ?> I haven’t made the pages this will redirect to.

And here is what the controller looks like

$message = <model name>::model()->findByPk(1);

$this->message=$message-> <column name in table>

$this->render(‘index’, array(‘content’=>$this->message));

The sql for this is very basic, ‘select <column name> from <table_name>’. I know findByPk(<parameter>) only returns one value, I’ve tried doing $message = <model name>::model()->findAllBySql(‘select <column_name> from <table_name>’); but to no avail… any hints on how to do this most basic of tasks?

Thanks in advance

macmac, the simplest way is probably to use Gii: Automatic Code Generation (it does scaffolding / basic CRUD generation). You can generate a model and views for your table. If you examine the code, you’ll be able to understand what is going on and modify the code to display only a single column.

SQL can be used directly when the functionality provided by ActiveRecord is not needed:

$messages = Yii::app()->db->createCommand('SELECT column FROM table')


$this->render('index', array('messages' => $messages));

right. Using CDbConnection (as above) is useful when the query is out of context for the ActiveRecord, meaning involves more than one tables and unrelated to relationship between tables (that can be handled by AR). That is - AFAIK… B)