Config application

Hi.

I have to write actionInitConfig. I download data from the form, save in the files: @app/config/db.php and @app/config/mailer.php.
I want to open the database and export the structure and preliminary data. I can’t open the database. I tried several ways. Here’s the last way.

$db = new Yii\db\Connection([
		'dsn' => 'mysql:host=' . $this->hostDB . ';dbname=' . $this->nameDB,
		'username' => $this->userDB,
		'password' => $this->passDB,
		'charset' => 'utf8',
	]);
	$sql = file_get_contents(Yii::getAlias('@app//config/structure.sql'));
	$sql .= "INSERT INTO `user` (`userid`, `login`, `accessid`, `fname`, `lname`, `email`, `password`, `block`, `manyTimes`, `blockTimes`) VALUES
	(2, '" . $this->login . "', 3, '" . $this->fName . "', '" . $this->lName . "', '" . $this->email . "', '', 0, 0, '0000-00-00 00:00:00');";
	$db->createCommand($sql)->queryAll();

This way shows me access denit even though the data is correct.

I have the same problem with sending an email.

		$mailer = new yii\swiftmailer\Mailer([
		'class' => 'yii\swiftmailer\Mailer',
		'transport' => [
			'class' => 'Swift_SmtpTransport',
			'host' => $this->smtp,
			'username' => $this->userMail,
			'password' => $this->passMail,
			'port' => $this->portMail,
			'encryption' => $this->encryptionMail,
		],
	]);
	$mailer->compose()
		->setFrom($this->adminEmail)
		->setTo('email@domain')
		->setSubject(Yii::$app->name . " - New application.")
		->setTextBody('New server: ' .  $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'])
		->send();

Please help.

https://www.yiiframework.com/doc/guide/2.0/en/structure-applications#application-lifecycle

And what’s next?

Sorry for sloppy reading. My bad.

Why do you think db and mail is one issue and not two?

db:
Where in the code do you get “access denied”?
Have you tried some simple sql command?
Do the db user have enough rights?

Why do you think db and mail is one issue and not two?

Two, but come down to errors in creating objects.

Where in the code do you get “access denied”?

$db->createCommand($sql)->queryAll();

Have you tried some simple sql command?

Yes, same.

Do the db user have enough rights?

Yes, the same as I used to write the entire application. Now I want to fill the installation procedure.

You need to use execute() instead of queryAll.
First check if you can make the insert to an existing structure.
If not, check db privileges.

Assuming you add the code to a controller action it should work.
(you can also use a different connection name e.g. “db2”)

You need to use execute() instead of queryAll.

I tried. Access to the database is denied.

I verified the insertion, then I verified structure changes (delete and add column).
Did you do the same step by step?
Again, check (global) privileges to the db.

Is OK

DB open:

$db = new Yii\db\Connection([
	'dsn' => 'mysql:host=' . $this->hostDB . ';dbname=' . $this->nameDB,
	'username' => $this->userDB,
	'password' => $this->passDB,
	'charset' => 'utf8',
]);
$db->open();
$sql = file_get_contents(Yii::getAlias('@app//config/structure.sql'));
$db->createCommand($sql)->execute();

and mail init:

$mail = (new yii\swiftmailer\Message())
	->setFrom($this->smtp)
	->setTo('...)
	->setSubject(Yii::$app->name . " - New application.")
	->setTextBody('New server: ' .  yii\helpers\Url::base(true));

Yii::$app->mailer->setTransport([
	'class' => 'Swift_SmtpTransport',
	'host' => $this->hostSMTP,
	'username' => $this->userSMTP,
	'password' => $this->passwordSMTP,
	'port' => $this->portSMTP,
	'encryption' => $this->encryptionSMTP,
]);
Yii::$app->mailer->send($mail);

Close :grinning: