Get Plugin variable data in Controller

AHK

Active Member
Hello @twisted1919,

As i am modifying my plugin with more options.
I want to know 2 things
1. how to get plugin stored data in controller, as i was able to store data in plugin like in example plugin "dummy_setting" variable.
I am able to save this data in plugin settings page, how can i now get this data in controller page?

2. also in which table of database this data is saved, if i want to access this data outside plugin controller like i want to make one controller who will run with cron job.

Thanks
 
@AHK - From anywhere, you can get the extension instance with:
PHP:
$extension = Yii::app()->extensionsManager->getExtensionInstance('your-extension-name');
Then you can call any of the the extension methods, including getOption/setOption like:
PHP:
$extension->getOption('whatever');
 
@AHK - Please look in the way Yii framework is handling the cron jobs.
Basically you have to create a console command in your extension and hook it into the list if available commands from mailwizz.
 
Hello @twisted1919,
Thanks for help and i am able to work with cron jobs. but to make my cron job i need to update main.php in
'commandMap' => array(
to put my command to work.
is there any other way to do that in order to be safe on update?
Thanks
 
Last edited:
@AHK - Yes there is, simply create a main-custom.php file near main.php and place your custom commandMap overrides there ;)
 
Hello @twisted1919,
I have Successfully created cronjob and its working perfect.
i am running foreach loop for campaigns and updating data in DB commands like this
foreach($email as $email)
{
///my code
$sql = 'INSERT INTO `dbname` (campaign_uid) VALUES (:cid1)';
Yii::app()->getDb()->createCommand($sql)->execute(array(
':cid1' => $key));
}
this work fine, as you can see its LOOP and if loop become big i got this error on terminal
CDbCommand failed to execute the SQL statement
General error: 2006 MySQL server has gone away

After searching on internet and your code i got this commands

Yii::app()->getDb()->setActive(false);
If i put this code in loop, this increase lot of load on DB
Yii::app()->getDb()->setActive(true);
and if i put this code this also give same error.
and if i put code like this
Yii::app()->getDb()->setActive(false);
Yii::app()->getDb()->setActive(true);
This also increase lot of load on DB.


AS you may got same issue before, Please help me how to fix this.
Thanks
 
Hello @twisted1919,
Thanks for update but i think in my SQL query batch insert will not work as i am using query as bellow
INSERT INTO table (id,a,b,c,d,e,f,g)
VALUES (1,2,3,4,5,6,7,8)
ON DUPLICATE KEY
UPDATE a=a, b=b, c=c, d=d, e=e, f=f, g=g;
 
i tried all options but all is error, how can i write onduplicate in bellow command ?

Yii::app()->db->schema->commandBuilder;
$command=$builder->createMultipleInsertCommand('tbl_post', array(
array('title' => 'record 1', 'text' => 'text1'),
array('title' => 'record 2', 'text' => 'text2'),
));
$command->execute();

I know its Yii not MW but as you are working with Yii so you may know best way.

what i need is, insert record if there is no record and if there is record then add (1) to old value of record.

or is there any way to add bath SQL statements ?

Thanks
 
Hello @twisted1919,

after week of working with my DB server gone away issue i find out what was causing issue.
in my consol command my function start with
$extension = Yii::app()->extensionsManager->getExtensionInstance('resnd');
now as i have extension and i get data and perform few tasks, some time these tasks takes around hours and when task complete i need to update DB and update extension value too like which time task complete.
for DB i did bellow command and its working

if(!empty($data)){
$db = Yii::app()->getDb();
$inserts = array_chunk($data, 100);
foreach ($inserts as $insert) {
$connection = $db->getSchema()->getCommandBuilder();
$command = $connection->createMultipleInsertCommand('errors', $insert);
$command->execute();
}
$connection->active=false;
$db->active=false;
}


if you see above after one task i close the DB and connection too, and in next task i do this again and its working.

but after all tasks, when i want to set extension data as bellow
$extension->setOption('timecomplete',$ctime);
here i got error that MYSQL server gone away and i think that extension can't stay alive so long for hours and its closed.
so what i try to do is, again create a new extension variable set values as bellow, at end of function
$extension1 = Yii::app()->extensionsManager->getExtensionInstance('resnd');
$extension1->setOption('timecomplete',$ctime);

it should work but its giving same error :(

is there any way to close the extension connection after getting values and then reopen it at end so that it save DB connection.

thanks
 
is there any way to close the extension connection after getting values
When you close the database connection, you close it for everything.

The right way to close the connection is Yii::app()->getDb()->setActive(false); and the right way to open it is Yii::app()->getDb()->setActive(true);

but after all tasks, when i want to set extension data as bellow
$extension->setOption('timecomplete',$ctime);
here i got error that MYSQL server gone away and i think that extension can't stay alive so long for hours and its closed.
Maybe you can try to open the connection again and then try to set the option.
 
Back
Top