Find data from database using findByPk() method in yiiframework

Hey Guys,

Suppose your model name is “CompanyUnit”. Now you want to select data from there using PrimaryKey(PK). Then you can use “findByPk()” method in yiiframe. It is very easy and simple to use.


  1. <?php 
  2. $comUnit = CompanyUnit::model()->findByPk(2); 
  3. echo $comUnit->name;
  4. ?>
Here “2″ is a PK and “CompanyUnit” is a name of model.
when “echo $comUnit->name” is executed then display name base of pk.
I think you like that.