Yii Framework Forum: Display Non-Related Db Content - Yii Framework Forum

Jump to content

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

Display Non-Related Db Content Rate Topic: -----

#1 User is offline   enfield 

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

Posted 11 January 2013 - 01:12 PM

I have a model without a db table used to display information that is stored in a session.

I am able to display the content in the session which was loaded from a given table using

  $ids = Yii::app()->storedData->getIds();
    foreach($ids as $id) {
    echo 'ID '.$id .'<br />'; 
    } 


Instead of displaying the Ids I would rather display information from a related table of the table from which this info was loaded into the session.

To help explain what I am after, given:

Posted Image

I have ModelC to display session data that is loaded from TableA. Currently with the code above the returned values are TableAId. I would rather display TableB:Description instead. How can I go about doing this without just loading that Description value into the session data too?

In the above code I can retrieve a variable in the session in ModelC by using
$ids = Yii::app()->storedData->getIds();
say this gives me 48 which is TableAId. Instead of getting 48 I want to return the related description which is stored in TableB.
0

#2 User is offline   waitforit 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 382
  • Joined: 09-February 11

Posted 11 January 2013 - 02:20 PM

Do a findByPk() for each ID and echo the description.

Pseudocode:
foreach $ids as $id
{
  $model=Model::findByPk($id);
  echo $model->description;
}

1

#3 User is offline   enfield 

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

Posted 11 January 2013 - 04:44 PM

View Postwaitforit, on 11 January 2013 - 02:20 PM, said:

Do a findByPk() for each ID and echo the description.

Pseudocode:
foreach $ids as $id
{
  $model=Model::findByPk($id);
  echo $model->description;
}



When I try to use your code example I get "ModelCController does not have a method named "getTableAlias"."

I am not sure what is causing this or how to clear it up. Any ideas?
0

#4 User is offline   alirz23 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 817
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 11 January 2013 - 10:41 PM

you got it wrong, it suppose to be

foreach $ids as $id
{
  $model=Model::model()->findByPk($id);
  echo $model->description;
}


findByPk is not a static method
1

#5 User is offline   waitforit 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 382
  • Joined: 09-February 11

Posted 12 January 2013 - 12:11 AM

Thats why I said pseudocode :)

Thanks for the correction.
0

#6 User is offline   enfield 

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

Posted 12 January 2013 - 02:46 PM

Ok by changing from findByPk to findByAttributes in the above code (as was pointed out to me on SO) I have got what I need. Thanks you two for the help.
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