Save() With Dynamic Tenant

Hi there

I’ve been trying to resolve this problem for like a week now, and still no luck!

I’ve been posting few topics around my problem and noticed I wasn’t really clear about what I wanted. So I’ll make it very short and concise.

Please can someone explain me why:

[color="#FF0000"]the save(false) is not working.[/color]

[color="#00FF00"]the saveCustom() is working perfectly.[/color]

[color="#0000FF"]Usage[/color]




$newLayout = new TENANT_Layout(null, $_GET['tenantID'] );

$newLayout->layoutTitle = 'test';

$newLayout->IDGTBtemplate = 4;

$newLayout->save(false); 

//$newLayout->saveCustom();



[color="#0000FF"]Model[/color]




class TENANT_Layout extends /*TENANT_ActiveRecord*/ CActiveRecord

{         

         public $externalTenant;     

// __construct() and getDbConnection() are pretty simple and straightforward.

         public function __construct( $scenario = 'insert', $tenantID = null )

         {

                if ( isset($tenantID) )

                {

                        $this->externalTenant = GTBtenant::model()->findByPk( $tenantID ); 

                        if ( !isset( $this->externalTenant) )

                            Yii::app()->alert->sendAlert(Alert::ERROR_ALERT, 'the tenant ('.$tenantID.') couldnt be found, when requested by class ['.__CLASS__.']');  

                }

                parent::__construct( $scenario );

         }

         

         

         public function getDbConnection()

         {

                if ( isset($this->externalTenant) )

                {                       

                        $connection = new CDbConnection(    'mysql:host='. $this->externalTenant->IPaddress.

                                                            ';dbname='. $this->externalTenant->DBname,

                                                             $this->externalTenant->username,

                                                             $this->externalTenant->password);                       

                        $connection->setActive(true);                       

                        return $connection;                        

                }  else {

                         return Yii::app()->tenant->db;

                }      

         }


//beforesave is triggered with the right info!

  protected function beforeSave() 

        {

            parent::beforeSave();

            Yii::app()->toolbox->alert('!!SAVE()!!! layoutTitle: '.$this->layoutTitle.',  IGGTBtemplate: '.$this->IDGTBtemplate);             

            

        }

        

//aftersave was never triggered with save()

        protected function afterSave() {

            parent::afterSave();

            Yii::app()->toolbox->alert('!!AFTER SAVE()!!! layoutTitle: '.$this->layoutTitle.',  IGGTBtemplate: '.$this->IDGTBtemplate);  

            

        }


//When I save with this function it works like a charm.

        public function saveCustom()

{           

            $table = $this->tableName();             

            $val = '"'.$this->layoutTitle . '","' . $this->IDGTBtemplate.'"';

            

            $dns = $this->getDbConnection()->connectionString;  

            $connection = new CDbConnection( $dns, 'my_user', 'my_password');

            $connection->active=true;

             

            $sql = "INSERT INTO $table (layoutTitle, IDGTBtemplate) VALUES ($val);";

           

            $command = $connection->createCommand($sql);

            $command->query();

            $connection->active=false;

            return true;

        }


  

[...]



[color="#0000FF"]debug.log[/color]




in /var/www/gtbHosting/protected/extensions/GTBwidgets/LayoutCreator/themes/default/views/editor/index.php (206)

in /var/www/gtbHosting/protected/components/GTBmasterWidget.php (176)

in /var/www/gtbHosting/protected/components/GTBmasterWidget.php (161)

2013/06/18 23:57:41 [trace] [system.db.ar.CActiveRecord] GTBtemplate.findByPk()

in /var/www/gtbHosting/protected/extensions/GTBwidgets/LayoutCreator/themes/default/views/editor/index.php (206)

in /var/www/gtbHosting/protected/components/GTBmasterWidget.php (176)

in /var/www/gtbHosting/protected/components/GTBmasterWidget.php (161)

2013/06/18 23:57:41 [trace] [system.db.CDbCommand] Querying SQL: SELECT * FROM `GTBtemplate` `t` WHERE `t`.`ID`=4 LIMIT 1

in /var/www/gtbHosting/protected/extensions/GTBwidgets/LayoutCreator/themes/default/views/editor/index.php (206)

in /var/www/gtbHosting/protected/components/GTBmasterWidget.php (176)

in /var/www/gtbHosting/protected/components/GTBmasterWidget.php (161)

2013/06/18 23:57:41 [trace] [system.db.CDbCommand] Querying SQL: SHOW COLUMNS FROM `GTBlanguage`

in /var/www/gtbHosting/protected/models/GTBlanguage.php (21)

in /var/www/gtbHosting/protected/views/layouts/admin.php (6)

in /var/www/gtbHosting/protected/controllers/SiteController.php (50)

2013/06/18 23:57:41 [trace] [system.db.CDbCommand] Querying SQL: SHOW CREATE TABLE `GTBlanguage`

in /var/www/gtbHosting/protected/models/GTBlanguage.php (21)

in /var/www/gtbHosting/protected/views/layouts/admin.php (6)

in /var/www/gtbHosting/protected/controllers/SiteController.php (50)

2013/06/18 23:57:41 [trace] [system.db.ar.CActiveRecord] GTBlanguage.findByAttributes()

in /var/www/gtbHosting/protected/views/layouts/admin.php (7)

in /var/www/gtbHosting/protected/controllers/SiteController.php (50)

in /var/www/gtbHosting/protected/components/Controller.php (97)

2013/06/18 23:57:41 [trace] [system.db.CDbCommand] Querying SQL: SELECT * FROM `GTBlanguage` `t` WHERE `t`.`i18n`=:yp0 LIMIT 1

in /var/www/gtbHosting/protected/views/layouts/admin.php (7)

in /var/www/gtbHosting/protected/controllers/SiteController.php (50)

in /var/www/gtbHosting/protected/components/Controller.php (97)



Now, can someone please explain me why saveCustom is working and not save()????

Thank you for your time. :)

Hi,

What error do you get when you call save(false)?

Probably $connection must be a static variable, as you see in the source code of CActiveRecord::getDbConnection().

http://www.yiiframework.com/doc/api/1.1/CActiveRecord#getDbConnection-detail

Thank you for your quick answer.

The result after a save() is: a simple reload of the page with no extra layout actually saved.

I also tried your recommendation:




         public function getDbConnection()

         {

                if ( isset($this->externalTenant) )

                {                       

                        self::$db = new CDbConnection(    'mysql:host='. $this->externalTenant->IPaddress.

                                                            ';dbname='. $this->externalTenant->DBname,

                                                             $this->externalTenant->username,

                                                             $this->externalTenant->password);                       

                        self::$db->setActive(true); 

                        return self::$db;                        

                }  else {

                         return Yii::app()->tenant->db;

                }      

         }



But still no luck it does exactly the same thing(reloading the page with nothing saved). Does anyone have an idea!? I’m sure it’s something stupid. ;)

Did you find nothing in the application log?

I would trace the execution of the program, setting a break point on that line and stepping into save() method to see what’s happening.