Yii Framework Forum: Having Trouble Printing Record Weight from a Join Table - Yii Framework Forum

Jump to content

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

Having Trouble Printing Record Weight from a Join Table Rate Topic: -----

#1 User is offline   ptoly 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 03-August 09

Posted 08 March 2010 - 05:13 PM

I'm building a photo gallery. I have a many to many relationship between the art and the galleries tables. Each gallery can have the art in them sorted (weighted) in their own way. I may be committing some kind of sin but it seems logical to store the weight of each art record in the join table. So now I have this in my model (does the art sorting within the gallery):

'artWeight'=>array(self::HAS_MANY, 'galXart', 'galID'),
            'art'=>array(self::MANY_MANY, 'art', 'galXart(galID, artID)',
                                'with'=>'artWeight',
                                'order'=>'artWeight.weight ASC'),


This works fine for sorting the art in each gallery. However I'm having a hard time getting the actual value of the join table's weight field printed in my page.

I gather the art from the galleries relationship this way:

$art         = $model->art;
$this->renderPartial('_thumbs',array(
    'gallery'    => $model,
    'art'        => $art,
)); 


This may seem foolish to you pros, but I can't figure out how to access the weight field buried in the $art object.

Suggestions? Thanks!
0

#2 User is offline   Theill11 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 51
  • Joined: 03-March 10
  • Location:@ localhost

Posted 08 March 2010 - 09:49 PM

The $art variable is not an object but an array.

So you need to do a foreach-loop on the variable to get the 'artWeight'-relation which again is an array which you should do a foreach-loop on.

Something like this:

foreach ($art as $a)
{
  foreach ($a->artWeight as $weight)
  {
    echo $weight->weight;
  }
}

0

#3 User is offline   ptoly 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 03-August 09

Posted 08 March 2010 - 11:06 PM

Ah recursive stuff. I was kind of hoping I could avoid it. However it does work! Thank you Komodo!
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