Yii Framework Forum: Multiple Databases And Find - Yii Framework Forum

Jump to content

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

Multiple Databases And Find Rate Topic: -----

#1 User is offline   RuliTheHat 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 25-May 12

Posted 17 December 2012 - 12:35 PM

Hi!

I'm using activerecord with two databases. I followed instructions given here: http://www.yiiframew...ltiple-domains/ Is it possible to execute queries using Yii's find-methods to the both databases? And how?
0

#2 User is offline   RuliTheHat 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 25-May 12

Posted 19 December 2012 - 04:44 AM

Let me clarify a bit. I would like to use this:

$users = User::model()->findAll();


instead of this:

$users = Yii::app()->db2->createCommand()
    ->select('*')
    ->from('tbl_user')
    ->queryAll();


Is this possible with two databases? I would like to make queries to both of them.
0

#3 User is offline   luc 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 211
  • Joined: 22-June 10
  • Location:france

Posted 19 December 2012 - 06:35 AM

Hi,
the wiki is out of date. Yii feature have envolved since Jul 30, 2010 !

Now the framework has a nice multidb support, with transparent AR calls through different dbs.

you always have to declare your dbs in config/main.php:

'components'=>array(
.........
        'db'=>array(
            'connectionString' => 'mysql:host=localhost;dbname=database1',
            'emulatePrepare' => true,
            'username' => 'root',
            'password' => 'itsasecret',
            'charset' => 'utf8',
        ),
        'db2'=>array(
            'class' => 'CDbConnection',
            'connectionString' => 'mysql:host=localhost;dbname=database2',
            'emulatePrepare' => true,
            'username' => 'root',
            'password' => 'itsasecret',
            'charset' => 'utf8',
        ),
....
),


the in you model/MyModel.php you have to declare the db connection:

/**	 
* @return CDbConnection database connection	 
*/	
public function getDbConnection()	
{		
   return Yii::app()->db; // or return Yii::app()->db2;
}


Notes:
1/ gii (giix also and maybe other generators found in extensions directory), already handle this feature: you can select the db to use for the model generation: Excellent!
2/ db system can be from differnet types (mysql, postgres and so on) and also be hosted on different servers: Brillant !
3/ all relations works transparently: Amazing !

So you can use:

$users = User::model()->with('profile')->findAll();


where User model is from db, and Profile model from db2 and User model has_one Profile declared in its relations array as 'profile'

how simple. Thank you Yii
Hey Ho !
Let's go !
0

#4 User is offline   RuliTheHat 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 25-May 12

Posted 20 December 2012 - 07:57 AM

Thanks a lot! :)
Now save() works perfect, but couldn't manage to get find() to work at all.. Could you clarify that with('profile') thing a bit? What should the 'profile' be inside the brackets? Can I leave it out like this:

$users = User::model()->findAll();

0

#5 User is offline   MadAnd 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 109
  • Joined: 16-June 11
  • Location:Ukraine

Posted 20 December 2012 - 10:33 AM

Hi,

as i know AR cannot handle cross database JOINs, so it's impossible to perform eager loading of the profile with the user at the same time. It seems that
$users = User::model()->with('profile')->findAll();
is misleading in this case, and the lazy loading should be used instead.
0

#6 User is offline   RuliTheHat 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 25-May 12

Posted 21 December 2012 - 04:51 AM

This is what I get when I try to make a query using find:

Fatal error: Call to a member function findAll() on a non-object

Here's the query:

$tempParts = BuildingElement::model()->findAll();


And here's the model:

class BuildingElement extends CActiveRecord{
    
    public $user_id;
    public $name;
    public $lifetime;
    public $talo80_element_id;
    public $talo2000_element_id;
    
    /**
    * Returns the static model of the specified AR class.
    * @return User the static model class
    */
    public static function model($className = __CLASS__) {
        parent::model($className);
    }
    
    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'structural_element';
    }
    
    /**
     * Declares the validation rules.
     * @return array validation rules for model attributes.
     */
    public function rules() {
        return array(
            array('name, lifetime', 'required'),
            array('lifetime', 'match', 'pattern' => '/^[0-9]/'),
        );
    }
    
    public function getDbConnection() {
        return Yii::app()->db2;
    }
}

0

#7 User is offline   luc 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 211
  • Joined: 22-June 10
  • Location:france

Posted 22 December 2012 - 06:35 AM

@MadAnd: I do not have my dev computer here (I am on hollidays) but, it seems to me that I have done something like eager loading ...

@RuliTheHat: strange.
If $user_id;$name;$lifetime ... are in your 'structural_element' table, you do not have to declare them in the Model definition
Hey Ho !
Let's go !
0

#8 User is offline   RuliTheHat 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 25-May 12

Posted 04 January 2013 - 08:30 AM

I'm using PHP version 5.3.16, could that be the reason?

Also, if I create an object from the BuildingElement class, and call the find-method through it, everything works just fine...

Like this:

$element_model = new BuildingElement;
$element_data = $element_model->findAll();

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