Yii Framework Forum: CActiveDataProvider Join - Unexpected Results - Yii Framework Forum

Jump to content

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

CActiveDataProvider Join - Unexpected Results Rate Topic: -----

#1 User is offline   waterloomatt 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 545
  • Joined: 09-April 10

Posted 23 April 2010 - 12:41 PM

Hi,

I am trying to find all Posts that have pending (Status = 1) Bids. I have a HAS_MANY relationship from Post -> Bid and BELONGS_TO relationship from Bid -> Post.

The conditions are fairly straightforward. The Post status and Bid status must be Open and the Author of the Post should be the logged in user.

Unexpected results: I am using a CListView to display the results. It appears as if the $model object is "merged" with other records. Ex. The Title is correct but the author & bidCount properties are wrong. To me, this indicates something wrong with my SQL.

$dataProvider=new CActiveDataProvider('Post', 
	array(
		'criteria' => array(
                 'join' => 'INNER JOIN {{bid}} ON t.id = {{bid}}.post_id',
			'condition'=>'{{bid}}.status=:bidStatus AND t.status=:postStatus AND t.created_by=:author',
			'params'=>array(
				':postStatus'=>Post::STATUS_OPEN, 
				':bidStatus'=>Bid::STATUS_OPEN,
				':author'=>Yii::app()->user->id,
                 ),
			'order'=>'t.id DESC',
          	'pagination'=>array(
            	'pageSize'=>Yii::app()->params['postsPerPage']
                 ),
	  	)
	)
);


Any ideas? Please let me know if you need additional info. I have attached a screenshot showing the fudged data and SQL profile output.

Cheers.

Attached File(s)


0

#2 User is offline   waterloomatt 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 545
  • Joined: 09-April 10

Posted 23 April 2010 - 09:37 PM

I thought this was resolved but I wrong. Does't seem to be using my With clause at all. No matter what I change the Bid status to, it still return all Open Posts.

To summarize:
  • I am trying to find all Posts with at least 1 Open Bid.
  • Post HAS_MANY Bids
  • Bid BELONGS_TO Post


$dataProvider=new CActiveDataProvider('Post', 
			array(
				'criteria' => array(
					'condition'=>'t.status=:postStatus AND t.created_by=:author',
					'params'=>array(
						':postStatus'=>$postStatus, 
						':author'=>Yii::app()->user->id,),
					'with'=>array(
						'bids'=>array(
	                 		'condition'=>'bids.status=:bidStatus',
		                 	'params'=>array(
		                 		':bidStatus'=>$bidStatus,
							),
						),
					),
					'order'=>'t.created_on DESC',
		          	'pagination'=>array(
		            	'pageSize'=>Yii::app()->params['postsPerPage']),
			  	)
			)
		);


Cheers
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