Yii Framework Forum: [SOLVED] 'with' in the Model - Yii Framework Forum

Jump to content

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

[SOLVED] 'with' in the Model How to access data? Rate Topic: -----

#1 User is offline   drech 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 100
  • Joined: 06-April 09

Posted 28 October 2010 - 02:36 PM

Rookie question:

I have a model with the following relation:

'articles'=>array(self::BELONGS_TO, 'Article', 'proficiencyId','with'=>'articleReviews'),


I'm using it like this:

$articles = Comments::model()->with('articles')->together()->findAll();


How do I access the 'articleReviews' array?

I've tried $articles->articles->articleReviews

and

$articles->articles['articleReviews']

but I get the error 'trying to access property of non-object'.

I can print_r the resultant array and all the data is there in a format that should be accessible with the '$articles->articles->articleReviews' syntax.

Thanks!
0

#2 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,450
  • Joined: 04-October 10

Posted 28 October 2010 - 03:22 PM

I think you got it wrong, you should:

Article -> relatedTo -> ArticleReview by their correspondent foreign key

in Article:

public function relations(){
    return array('reviews'=>array(self::HAS_MANY,'ArticleReview', 'FOREIGNKEYTHATCONNECTSBOTHSTABLES'),);
}


in ArticleReview

public function relations(){
    return array('article'=>array(self::BELONGS_TO, 'Article', 'FOREIGNKEYTHATCONNECTSBOTHSTABLES'),);
}


Then you can call

$articles = Article::model()->with('reviews')->findAll();

// or

$article = Article::model()->findbyPk('thekey');

foreach( $article->reviews as $review ) ...



Or that, or I didn't understood the question... hope I helped you anyway
┬┐How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
1

#3 User is offline   drech 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 100
  • Joined: 06-April 09

Posted 28 October 2010 - 04:38 PM

I tried your way first but it would not work because the ArticleReview table does not share the same FK as the other two (normaiized DB). The only way to get to the data I need is to use 'with' in the model which cascades, for lack of a better word, the relations through the the middle table (Article).

The data does in fact get returned; it just doesn't seem accessible.

I'm sure I need to use multiple foreach statements to break the arrays up but I just can't seem to get the right depth.
0

#4 User is offline   got 2 doodle 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 249
  • Joined: 12-December 08
  • Location:Beautiful Nova Scotia, Canada

Posted 28 October 2010 - 05:32 PM

Quote

I'm using it like this:

$articles = Comments::model()->with('articles')->together()->findAll();


try
<?php
foreach($articles as $article)
{
 echo $article->reviews ; 
// you might have to do a foreach within this loop because I think the reviews is an array of review objects 
}


Totally untested

doodle
Check out myYii powered website
1

#5 User is offline   drech 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 100
  • Joined: 06-April 09

Posted 29 October 2010 - 11:34 AM

<?php
foreach($articles as $article)
{
 echo $article->reviews ; 
// you might have to do a foreach within this loop because I think the reviews is an array of review objects 
}


Thanks guys, that got me on the right track.

With two foreach it works:

foreach($articles as $article)
  foreach($article->articles->articleReviews as $review)


I was making it more complex than it needed to be.
0

#6 User is offline   thinkt4nk 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 19-September 10

Posted 29 October 2010 - 02:15 PM

Your table naming choices make your job of conceptualizing the data an unhappy one.
0

#7 User is offline   drech 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 100
  • Joined: 06-April 09

Posted 29 October 2010 - 04:55 PM

Those aren't the actual table names.

/shock

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