[Solved] Relations across databases

Alright so I’ve tried this in the Live Chat and fried the brains of a few people in there lol… hoping someone here might be able to help me. My problem is:

I have a page with a search which goes through a table finding serial keys, then partialRenders the results box with the new information:

Controller:


public function actionUpdateAjax() {


        $searchParam = $_POST['searchInput'];

        $criteria = new CDbCriteria;

        $criteria->condition = 'serial=:serial';

        $criteria->params = array(':serial' => $searchParam);


        $this->renderPartial('partials/inStock/acc01', array(

            'Modems' => Modems::model()->findAll($criteria)));

}

This works fine when I search for values.

My problem is that when I use the view code:


foreach($Modems AS $Modem)

    {

    echo "<a class='asset_frame' href='" .CHtml::ajax( array('id'=>'searchInput', 'method'=>'POST')). "'>";

    echo "Modem ID: ".

        $Modem->modem_id.

            "<br>".

        $Modem->asset_id.

            "<br>".$Modem->asset->displayName;

    echo "</a>";

    }

… the partial render won’t render. However, if I comment out the $Modem->asset->displayName I get the partialRender working.

The relation for the Modem model is:


public function relations()

	{

		return array(

            'asset' => array(self::BELONGS_TO, 'Assets', 'asset_id'),

        );

Can anyone help? This is really holding me up :(

P.S as a side note, I have two other relations set up in two other models and can access them just fine when used in the same manner. If it matters, I override getCDbConnection in all three models as the Modems model uses a seperate DB and relates into the same one as the first two.

Can you please pastebin the var_dump($Modem->asset); ???


foreach($Modems AS $Modem)

{

     var_dump($Modem->asset);die;

}

No luck, var_dump results in the same issue as when I have $Modem->asset->displayName uncommented

UPDATE:

Renamed all references to Assets and asset to eliminate the possibility of it being a reserved word conflict and was hit with the same issue anyway… Just in case anyone was wondering lol

Alright, development #2 here:

Just discovered that when I moved the modems table into the same DB as the others, i can use the relation $Modem->asset->displayName.

This means that my code is all working as it should but the cross-db relations aren’t working.

hmmm…strange…

UPDATE #2:

Turns out that this won’t work since the relations use a JOIN which can’t work across db’s.

I’m stuck then lol

Ok finally, solution.

Many thanks to Horizons, who helped me solve this one.

For anyone trying to do the same, in each of your models, as well as specifying the database in the getCDbConnection you must also change the tableName function to allow for the seperate dbs.


	

public function getDbConnection()

    {

        return Yii::app()->db; /*this relates to the connection in config/main.php */

    }




public function tableName()

	{

		return 'dbname.tableName'; //auto-generated as 'tableName' but must be 

                                           //changed to allow for the different database

	}

And don’t forget to set the connections in your config/main.php.

Damn searched all night for that… thx brah ;D