Answer the question
In order to leave comments, you need to log in
How to properly and safely use ORM in Yii2?
Hello.
yii migrate
- и будет тебе счастье. <?php
use yii\db\Migration;
class Action extends Migration
{
public function up($table, $params = [])
{
$this->createTable($table, [
'id' => $this->primaryKey(),
$params // передаём массив того, какие столбцы надо добавить
]);
}
public function down($table)
{
$this->dropTable($table); // передаём, какую таблицу будем удалять
}
public function clear($table)
{
$this->truncateTable($table); // передаём, какую таблицу будем очищать
}
public function getColumn($table, $column, $type = 'string')
{
$this->addColumn($table, $column, $type); // добавление столбца в таблицу
}
// и так далее ...
}
?>
<?php
...
class SiteController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'clear' => ['post'], // post ?? - чтобы выполнить ?
],
],
];
}
...
public function actionClear()
{
Action::clear('post');
return $this->redirect(['index']);
}
...
}
?>
...
<?=Html::a('Truncate table', ['site/clear'], ['title' => 'очистить таблицу?', 'class' => 'btn', 'data-method' => 'post', 'data-pjax' => '',]);?>
...
Answer the question
In order to leave comments, you need to log in
Even if you or someone else “mounted” the doctrine to the Yii2 project (this is possible in principle ), this does not mean that you do not need to understand the meaning of migrations. As Maxim Fedorov already said , migrations are needed for database versioning . Using versioning (migrations) of the database as a command is a stupid idea .
Also, migrations don't have to operate on data , they only version the schema. On a working project, you work with data: through scripts, SQL queries, etc.
For example. You have downloaded the project from the repository. Created an empty database. Connected to the project. You apply migrations, and they give an error related to data migrationwhich you don't have. This is clear?
So how do you organize the work of the customer not through the console, but through the web interface? There are two ways to go:
class AdminController extends \yii\web\Controller
{
public function actionIndex()
{
return $this->render('index');
}
public function actionComposerUpdate()
{
return $this->commandExec('composer update');
}
public function actionComposerInstall()
{
return $this->commandExec('composer install');
}
public function actionComposerSelfUpdate()
{
return $this->commandExec('composer self-update');
}
private function commandExec($command)
{
$projectPath = \Yii::getAlias('@app');
$exec = "cd {$projectPath} && {$command}";
exec($exec, $resultArray);
if ($resultArray){
\Yii::$app->getSession()->setFlash('info', implode("<br>", $resultArray));
}
return $this->redirect('index');
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question