Quantcast
Channel: Yii Framework Forum
Viewing all articles
Browse latest Browse all 18717

Save() With Dynamic Tenant

$
0
0
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:

the save(false) is not working.
the saveCustom() is working perfectly.

Usage
$newLayout = new TENANT_Layout(null, $_GET['tenantID'] );
$newLayout->layoutTitle = 'test';
$newLayout->IDGTBtemplate = 4;
$newLayout->save(false); 
//$newLayout->saveCustom();


Model
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;
        }

  
[...]


debug.log
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. :)

Viewing all articles
Browse latest Browse all 18717

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>