Reputation: 1510
In my controller
$agent = University::model()->findByPK($university_id);
I hope it will return value of a row of value.
I want a single attribute(field3) value say university_name
, (with out using findByPK), how to get it
SELECT field3 FROM table [WHERE Clause]
Upvotes: 4
Views: 27989
Reputation: 2706
Try this:
$university_name = University::model()->findByPK($university_id, array('select'=>'univeersity_name'))->university_name;
#Query: SElECT university_name FROM table_name where id=x;
Instead of
$university_name = University::model()->findByPK($university_id)->university_name;
#Query: SElECT * FROM table_name where id=x;
Second query returns all the fields. So better to avoid those fields are not necessary.
Upvotes: 2
Reputation: 1721
It can be done like this:
$agent = University::model()->findAllByAttributes(array('field3'),"WHERE `id` = :id", array(':id' => $university_id));
First argument of findByAttributes is an array of attributes you wish it to return. If left empty it returns all (*).
Upvotes: 0
Reputation: 1871
Try this
$usercriteria = new CDbCriteria();
$usercriteria->select = "university_name";
$usercriteria->condition = "university_id=$university_id";
$university = University::model()->findAll($usercriteria);
echo $university->university_name;
Or simply do like u did first
$agent = University::model()->findByPK($university_id);
echo $agent-> university_name;
Upvotes: 11
Reputation: 49
It should be like this:
$agent = University::model()->findByPK($university_id)->university_name;
Upvotes: 3
Reputation: 15
in case if you need to view on the yii _views, i have implemented this on my project i put this inside my '_view.php' at /protected/views/myTable/
$agent = University::model()->findByPK($data->id_university/*this is the PK field name*/);
echo $agent->university_name /*university field name*/;
sorry again for my bad english :o
Upvotes: 0
Reputation: 10177
$agent = University::model()->findByPK($university_id);
echo $agent->university_name;
Upvotes: 5