Relation Returns Empty Arrays

My relation seems to be selecting the correct records but they are empty.

Model:





public function relations() {

	return array(

		'subscribersResources' => array(self::HAS_MANY, 'SubscribersResources', array('id_subscriber'=>'id')),

        	'resources' => array(self::HAS_MANY, 'Resources', array('id_resource'=>'id'), 'through' => 'subscribersResources'),

	);

	}




Controller:





  /**

     * @param string

     * @return string

     * @soap

  */  

  public function getResources($userId) {


	$subscriber = Subscribers::model()->findByPk($userId);

	$response = $subscriber->resources;

	return json_encode($response);

			


  }




Returns





Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) [3] => Array ( ) [4] => Array ( ) ) 




Seems that your subscribersResources are not being populated. check results of the that relation

That array is also empty. Why would this be though? It seems to be correct, what can I do to check?





public function relations(){

	return array(

		'subscribersResources' => array(self::HAS_MANY, 'SubscribersResources', 'id_subscriber'),

        	'resources' => array(self::HAS_MANY, 'Resources', array('id_resource'=>'id'), 'through' => 'subscribersResources'),

	);

}




Can anyone help?? It sounds so straight forward, it should work. PLEASE HELP!!

try this:




return CJSON::encode($response);



instead of




return json_encode($response);



Your relations config is correct. I have tried this.

For me it returns array of object. Try to check the


var_dump($subscriber->resources)

it should

return array of object.

One more thing.

Your function should return json encoded string as output, but the output you have written is not json encoded string.

I will try var_dump I’m using print_r, I don’t think it will make a difference may shed some light perhaps?

That’s because I decoded in the view file. Sorry should have explained that.

One of the tables was MyISAM type, the rest are InnoDB. I’ve corrected now so they are all InnoDB. I thought this might have been the cause but still have the same problem.

I read in another post that a good check is to run just plain SQL query to rule out the DB as the cause.