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.


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>