[Yii 2] Creare a runtime una tabella

Questo codice non funziona




 public function actionUpTo0001()

    {	

    	$command = $query->createCommand();


        $out = "migration Up";


        $command->createTable('{{%user}}', [

            'id' 					=> Schema::TYPE_PK,

            'username' 				=> Schema::TYPE_STRING . ' NOT NULL',

            'auth_key' 				=> Schema::TYPE_STRING . '(32) NOT NULL',

            'password_hash' 		=> Schema::TYPE_STRING . ' NOT NULL',

            'password_reset_token' 	=> Schema::TYPE_STRING,

            'email' 				=> Schema::TYPE_STRING . ' NOT NULL',


            'status' 				=> Schema::TYPE_SMALLINT . ' NOT NULL DEFAULT 10',

            'created_at' 			=> Schema::TYPE_INTEGER . ' NOT NULL',

            'updated_at' 			=> Schema::TYPE_INTEGER . ' NOT NULL',

        ], 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB');


        $out .= "<BR> Executed";

        return $out;

    }



Motivo:

Questo succede perchè $command->createTable invoca dalla classe Controller il metodo omonimo che cerca di fare questo




public function createTable($table, $columns, $options = null)

    {

        $sql = $this->db->getQueryBuilder()->createTable($table, $columns, $options);

 

        return $this->setSql($sql);

    }



Benissimo, mi sono detto… sostituisco tutto con la chiamata a $this->db-> etc …

Solo che a questo punto … come imposto $this->db? Qual’è l’equivalente di Yii::app()->db in Yii 2?

Esiste un sistema migliore?

Facile !




Yii::$app->db->createCommand()->createTable('{{%user}}', [

	            'id' 					=> Schema::TYPE_PK,

	            'username' 				=> Schema::TYPE_STRING . ' NOT NULL',

	            'auth_key' 				=> Schema::TYPE_STRING . '(32) NOT NULL',

	            'password_hash' 		=> Schema::TYPE_STRING . ' NOT NULL',

	            'password_reset_token' 	=> Schema::TYPE_STRING,

	            'email' 				=> Schema::TYPE_STRING . ' NOT NULL',


	            'status' 				=> Schema::TYPE_SMALLINT . ' NOT NULL DEFAULT 10',

	            'created_at' 			=> Schema::TYPE_INTEGER . ' NOT NULL',

	            'updated_at' 			=> Schema::TYPE_INTEGER . ' NOT NULL',

	        ], 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB')->execute();



Ottimo, però perché non usi le funzioni di migrazione già disponibili in Yii2? Sono molto comode e ben fatte.

ciaociao

Shared host

… ovvero "se potessi lo farei"

Maledetti! >:(

Comunque penso sia possibile sfruttarle anche da UI con runAction(), ma forse già te lo avevo detto in un altro post (?)

Non ci sono riuscito, mi sono del tutto arreso.

Tra

  • far andare gli .htaccess a dovere per poter avere sia frontend che backend sulla stessa macchina

  • non poter usare i console command

  • dover impazzire persino per i cookie per il ben noto problema del backend/frontend sulla stessa root

  • etc…

… questo sito lo faccio a mano … spiacente.

mi arrendo

La mia opinione è che non puoi fare un framework che incasini tanto l’installazione su shared host. Punto

che poi su vps o host dedicato vada tutto da Dio, ok, ma su shared host il template avanzato è pressochè un panico totale da affrontare, per cui, a questo giro, niente yii 2

Per definizione uno shared host è per un host, Yii advanced ne prevede 2, di sicuro non è adatta per funzionare “al volo”, per uno shared host credo sia corretto fare un template custom per la gestione di backend/frontend, ma ovviamente dipende anche dal budget che uno ha! ;)