Yii Framework Forum: display name instead of id in view - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

display name instead of id in view Rate Topic: -----

#1 User is offline   smug 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 73
  • Joined: 29-April 11

Posted 28 July 2011 - 05:49 AM

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
0

#2 User is offline   enfield 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 288
  • Joined: 12-January 11

Posted 28 July 2011 - 06:13 AM

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);
}

0

#3 User is offline   smug 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 73
  • Joined: 29-April 11

Posted 28 July 2011 - 06:21 AM

$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.
0

#4 User is offline   enfield 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 288
  • Joined: 12-January 11

Posted 28 July 2011 - 06:27 AM

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'

0

#5 User is offline   smug 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 73
  • Joined: 29-April 11

Posted 28 July 2011 - 06:34 AM

View Postenfield, on 28 July 2011 - 06:27 AM, said:

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'


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
0

#6 User is offline   enfield 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 288
  • Joined: 12-January 11

Posted 28 July 2011 - 06:42 AM

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?
0

#7 User is offline   MadAnd 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 109
  • Joined: 16-June 11
  • Location:Ukraine

Posted 28 July 2011 - 06:45 AM

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.
0

#8 User is offline   smug 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 73
  • Joined: 29-April 11

Posted 28 July 2011 - 06:47 AM

View PostMadAnd, on 28 July 2011 - 06:45 AM, said:

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.

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

#9 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,359
  • Joined: 12-October 09
  • Location:Croatia

Posted 28 July 2011 - 07:02 AM

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...
Find more about me.... btw. Do you know your WAN IP?
0

#10 User is offline   smug 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 73
  • Joined: 29-April 11

Posted 28 July 2011 - 07:10 AM

View Postmdomba, on 28 July 2011 - 07:02 AM, said:

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...

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?
0

#11 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,359
  • Joined: 12-October 09
  • Location:Croatia

Posted 28 July 2011 - 07:23 AM

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

Quote

Is it possible?


I already responded you above... yes... with a new method that would select the name from the related table...
Find more about me.... btw. Do you know your WAN IP?
0

#12 User is offline   vijayakumar 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 08-May 13

Posted 09 May 2013 - 06:58 AM

i think so...
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users