artisan 队列在 class 中使用变量值工作
artisan queue to work in class with variable values
我想对以下 pdo 语句进行排队以创建这样的数据库:
Queue::push(function($job, $dbname){
$dbh = new PDO("mysql:host=".getenv('DB_HOST'), getenv('DB_USERNAME'), getenv('DB_PASSWORD'));
$dbh->exec("CREATE DATABASE `$dbname` CHARACTER SET=utf8 COLLATE=utf8_unicode_ci;
GRANT ALL ON `$dbname`.* TO 'xxx'@'%';
FLUSH PRIVILEGES;");
unset($dbh);
Log::useFiles(storage_path().'/logs/databasecreated');
Log::info('database created: '.$dbname);
$job->delete();
});
但是它抛出
'Jeremeamia\SuperClosure\SerializableClosure::{closure}()
缺少参数 2
尝试 运行 php artisan queue:work
$dbname
设置为
$dbname = $this->getDbName();
并且整个代码驻留在 class' 函数中。
我也尝试过直接在闭包中使用 $this->getDbName()
和 $this->dbName
,但这自然没有用。
难道不能在队列中 运行 这样的命令吗?
尝试使用 use
语句传递变量:
Queue::push(function($job) use ($dbname){
$dbh = new PDO("mysql:host=".getenv('DB_HOST'), getenv('DB_USERNAME'), getenv('DB_PASSWORD'));
$dbh->exec("CREATE DATABASE `$dbname` CHARACTER SET=utf8 COLLATE=utf8_unicode_ci;
GRANT ALL ON `$dbname`.* TO 'xxx'@'%';
FLUSH PRIVILEGES;");
unset($dbh);
Log::useFiles(storage_path().'/logs/databasecreated');
Log::info('database created: '.$dbname);
$job->delete();
});
我想对以下 pdo 语句进行排队以创建这样的数据库:
Queue::push(function($job, $dbname){
$dbh = new PDO("mysql:host=".getenv('DB_HOST'), getenv('DB_USERNAME'), getenv('DB_PASSWORD'));
$dbh->exec("CREATE DATABASE `$dbname` CHARACTER SET=utf8 COLLATE=utf8_unicode_ci;
GRANT ALL ON `$dbname`.* TO 'xxx'@'%';
FLUSH PRIVILEGES;");
unset($dbh);
Log::useFiles(storage_path().'/logs/databasecreated');
Log::info('database created: '.$dbname);
$job->delete();
});
但是它抛出
'Jeremeamia\SuperClosure\SerializableClosure::{closure}()
缺少参数 2尝试 运行 php artisan queue:work
$dbname
设置为
$dbname = $this->getDbName();
并且整个代码驻留在 class' 函数中。
我也尝试过直接在闭包中使用 $this->getDbName()
和 $this->dbName
,但这自然没有用。
难道不能在队列中 运行 这样的命令吗?
尝试使用 use
语句传递变量:
Queue::push(function($job) use ($dbname){
$dbh = new PDO("mysql:host=".getenv('DB_HOST'), getenv('DB_USERNAME'), getenv('DB_PASSWORD'));
$dbh->exec("CREATE DATABASE `$dbname` CHARACTER SET=utf8 COLLATE=utf8_unicode_ci;
GRANT ALL ON `$dbname`.* TO 'xxx'@'%';
FLUSH PRIVILEGES;");
unset($dbh);
Log::useFiles(storage_path().'/logs/databasecreated');
Log::info('database created: '.$dbname);
$job->delete();
});