display name instead of id in view


(Ramakrishnanalajala) #1

hi all,

i have to display block name in view where i am presently displaying id. block name is a dropdown which i am getting from a model called block and i am using rev_block_name in another model for storing this value.

i have tried the following code in my index.php


 $blockids=array();

      $blockids = Yii::app()->db->createCommand()

			               ->select('rev_block_name')

			               ->from('tbl_revenue')

			               ->queryColumn();

			              // echo $blockids[0];

       $blockname =array();

 for($i=0;$i<count($blockids);$i++)

    {

             echo "***\n".$blockids[$i];


	  $blockname= Yii::app()->db->createCommand()

			               ->select('block_name')

			               ->from('tbl_block')

			               ->where('block_id=:block_id', array(':block_id'=>19))

			               ->queryColumn();

 echo "----------\n".$blockname[0];



i have given 19 here as a value of blockid which is static.but i dont know how to get it dynamically.

in my cdetail view what should i do the array.

thanks


(Swisscheese2122) #2

Is there a reason why you are not just using relationships to accomplish this? A lot less coding and easy to work with.

ex. where categories = relationship and Name is what you want. Of course adjusted to fit your needs.


public function getNames ()

{

  $out=CHtml::listData($this->categories,'CategoryId','Name');

  return $out=implode('<br />', $out);

}


(Ramakrishnanalajala) #3

$this->widget('zii.widgets.grid.CGridView', array(

    'dataProvider'=>$dataProvider,

    'columns'=>array(

   // display the 'content' attribute as purified HTML

   		array(            // display 'create_time' using an expression

            'name'=>'rev_block_id',

            'value'=>'$data->rev_block_id',

           

        ),

        array(            // display 'create_time' using an expression

            'name'=>'rev_flat_number',

            'value'=>'$data->rev_flat_number',

        ),

        array(            // display 'create_time' using an expression

            'name'=>'rev_instrument_number',

            'value'=>'$data->rev_instrument_number',

        ), ),

));      ?>

i know rev_block_id but i need to get block_name here which is in another model:block.

please help me.


(Swisscheese2122) #4

You just have to use…for a HAS_ONE relation


'value'=>'$data->relation->block_name'

…if a HAS_MANY use what I posted above to define array and call with


'value'=>'$data->name'


(Ramakrishnanalajala) #5

do i have to establish a relation in database or simply define a relation in model.i dont want to define any relation in database.

please help. thanks for reply


(Swisscheese2122) #6

All my dbs have relationships in them. I have never done so without so I can’t say for sure about just defining in the model. If there are no associated attributes I don’t see how it would work. Maybe someone else can answer?


(Dev Madand) #7

Hi,

you only need to proper declare relations() method in Revenue model. But it will be good also define foreign key in DB to ensure data consistency if your DBMS support that.


(Ramakrishnanalajala) #8

i dont want to do it with relations.is there any way to display other than defining relations


(Maurizio Domba Cerin) #9

Is there any reason on why not to use a relation ?

The relation makes things easier for you… as it makes the related database call transparently…

The other way would be to call a model method and pass the related ID to that method… then in that method perform the select to the related table…

But again… all this is automatically done if you define the relation…


(Ramakrishnanalajala) #10

as my application is almost done so i dont want to change my database schema.so i am trying to display name in my view without using relation.Is it possible?


(Maurizio Domba Cerin) #11

You dont need to change the database… you just define the relation in the model…

I already responded you above… yes… with a new method that would select the name from the related table…


(Vijayakumar4you) #12

i think so…