Two relations via the same 'through' table

Mine goal is to have possibility to search Documents via the Users Names and Surnames and also via Recrutation Year and Semester.

Documents are related only to Declarations in such a way that Document are connected to exatly one Declaration and Declaration can be connected to exatly One or none Documents.

Declarations are related to OutgoingStudent and Recrutation.

So when quering Documents I want to query also OutgoingStudent and Recrutations via the Declaration table.

My code for relations in Documents:




return array(

			'declaration' => array(self::BELONGS_TO, 'Declaration', 'DeclarationID'),

            'outgoingStudentUserIdUser' => array(self::HAS_ONE, 'OutgoingStudent', 'OutgoingStudent_User_idUser','through'=>'declaration',),

      		'Recrutation' => array(self::HAS_ONE, 'Recrutation', 'Recrutation_RecrutationID','through'=>'declaration'),

		);



And now when in search() function I want to make a query ->with ‘declaration’,‘outgoingStudentUserIdUser’ and ‘Recrutation’:


		$criteria->with = array('declaration','Recrutation','outgoingStudentUserIdUser');



Im getting error:




CDbCommand nie zdołał wykonać instrukcji SQL: SQLSTATE[42000] [1066] Not unique table/alias: 'declaration'. The SQL statement executed was: SELECT COUNT(DISTINCT `t`.`DeclarationID`) FROM `Documents` `t` LEFT OUTER JOIN `Declarations` `declaration` ON (`t`.`DeclarationID`=`declaration`.`idDeclarations`) LEFT OUTER JOIN `Recrutation` `Recrutation` ON (`declaration`.`Recrutation_RecrutationID`=`Recrutation`.`RecrutationID`) LEFT OUTER JOIN `Declarations` `declaration` ON (`t`.`DeclarationID`=`declaration`.`idDeclarations`) LEFT OUTER JOIN `OutgoingStudent` `outgoingStudentUserIdUser` ON (`declaration`.`OutgoingStudent_User_idUser`=`outgoingStudentUserIdUser`.`User_idUser`) 

when using only $criteria->with = array(‘declaration’,‘Recrutation’) or $criteria->with = array(‘declaration’,‘outgoingStudentUserIdUser’) there is no error only when using both.

So probably it should be done in some other way, but how?