Got this job here:

Implement the following functionality on yii or yii2: 3) Write the class of interaction with the database (preferably postgreSQL), implement the methods for the class:

  • Create - creates a database, input parameters, table name, and an array consisting of the field name, field data type, ext. Field data (not null, primary key for example).

Now I can’t understand with my mind how to do it from under yii2. After all, in the settings of yii2 in the config, the database used is already indicated and how to create a new database is not clear to me, because for this you need root rights to the DBMS.

The question is: do I not understand the task or is the task itself not feasible?

  • Well, judging by the входные параметры, название таблицы, и массив состоящий из названия поля, тип данных поля, доп can still create a table. And the text should have been written "creates in the database." I think it is better to clarify with those who gave the task. - Alexey Shimansky
  • and I also reason as you. just before the delivery of the task remains ~ 30 hours. and they ask questions by email, lose time and stay with the nose. - perfect
  • one
    Decide @perfect based on the fact that you have the right to create a database. The task can actually be useful, for example, when you automatically deploy an application in a container or in a test environment. If the production rights will not - your task simply will not work. - cheops
  • one
    You can see how the migration to yii2 for postgres works, creating a database is not there, but it can give you an idea. - StalkAlex
  • 2
    I will assume that the task was set incorrectly. Since the very concept of creating a database does not apply to the framework but to the admin part. A table can be easily created. That most likely was required with the help of QueryBuilder or with the help of Command - Ninazu

1 answer 1

I understand that I was late with the answer, but I will answer. The fact that in the config when connecting, we specify the database name in DSN, it just says that this is the base for our application by default and does not prohibit us to work with other databases up to and including those.

Let's see an example of DSN

 $db = [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=localhost;dbname=db1', 'username' => 'root', 'password' => '', 'charset' => 'utf8', ]; 

We indicate to the application that we will connect to db1 by default. But what prevents us from making such a request from the application?

 SELECT db1.users.id, db2.firms.id FROM db1.users JOIN db2.firms ON db1.users.firm_id = db2.firms.id 

A table from another database will be connected and the query will succeed. The main thing is to have enough rights.

We can also create, for example, the following migration

 <?php use yii\db\Migration; class m170219_083615__ extends Migration { public function up() { $this->execute("CREATE DATABASE test2"); } } 

And this migration will also be successful. The main thing that our current user had the right to create databases.

  • one
    if you have access to the console, it's easier to do all this using PHP without yii using the shell_exec function - perfect