{"id":18784740,"url":"https://github.com/carono/yii2-migrate","last_synced_at":"2025-04-13T12:33:24.789Z","repository":{"id":62499870,"uuid":"104178126","full_name":"carono/yii2-migrate","owner":"carono","description":"Трейт для помощи создания баз данных","archived":false,"fork":false,"pushed_at":"2020-12-19T10:53:43.000Z","size":132,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-04-27T20:42:32.243Z","etag":null,"topics":["database","migration","trait","yii2"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/carono.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-09-20T06:57:49.000Z","updated_at":"2020-12-19T10:53:36.000Z","dependencies_parsed_at":"2022-11-02T11:47:34.296Z","dependency_job_id":null,"html_url":"https://github.com/carono/yii2-migrate","commit_stats":null,"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/carono%2Fyii2-migrate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/carono%2Fyii2-migrate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/carono%2Fyii2-migrate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/carono%2Fyii2-migrate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/carono","download_url":"https://codeload.github.com/carono/yii2-migrate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223588693,"owners_count":17169879,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","migration","trait","yii2"],"created_at":"2024-11-07T20:43:56.921Z","updated_at":"2024-11-07T20:43:56.998Z","avatar_url":"https://github.com/carono.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/carono/yii2-migrate/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/carono/yii2-migrate/?branch=master)\n[![Latest Stable Version](https://poser.pugx.org/carono/yii2-migrate/v/stable)](https://packagist.org/packages/carono/yii2-migrate)\n[![Total Downloads](https://poser.pugx.org/carono/yii2-migrate/downloads)](https://packagist.org/packages/carono/yii2-migrate)\n[![License](https://poser.pugx.org/carono/yii2-migrate/license)](https://packagist.org/packages/carono/yii2-migrate)\n[![Build Status](https://travis-ci.org/carono/yii2-migrate.svg?branch=master)](https://travis-ci.org/carono/yii2-migrate)\n[![Code Coverage](https://scrutinizer-ci.com/g/carono/yii2-migrate/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/carono/yii2-migrate/?branch=master)\n\n[[ENG](README.md)] [[RUS](README_RUS.md)]\n\nMigrationTrait   \n=================\nTo expand the migration capabilities, you must add a trait **\\carono\\yii2migrate\\traits\\MigrationTrait** or extend the migration class from **\\carono\\yii2migrate\\Migration**\n\npublic function tableOptions()\n-\nReturn the array with the settings for creating tables, where the key is the name of the db driver.  \nWhen creating tables through createTable(), if no properties are specified, they will be picked up from this function\n```php\n    public function tableOptions()\n    {\n        return [\n            'mysql' =\u003e 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB'\n        ];\n    }\n```    \n\npublic function newTables()\n-\nReturn an array where the key is the name of the table, and the values are columns with types.  \nIf you call the **$this-\u003eupNewTables()** function, all specified tables will be created via createTable()  \nIf you call the function **$this-\u003edownNewTables()**, all specified table will be deleted using dropTable()\n```php\n    public function newTables()\n    {\n        return [\n            '{{%logs}}' =\u003e [\n                'data' =\u003e $this-\u003estring(),\n                '@tableOptions' =\u003e [\n                    'mysql' =\u003e 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=MyISAM'\n                ]\n            ]\n        ];\n    }\n    \n    public function safeUp()\n    {\n        $this-\u003eupNewTables();\n    }\n    \n    public function safeUp()\n    {\n        $this-\u003edownNewTables();\n    }\n```            \n\npubic function newColumns()\n-\nReturn an array where the key is the name of an existing table and the values are columns with types.  \nIf you call the function **$this-\u003eupNewColumns()**, all specified columns will be created using addColumn()  \nIf you call the function **$this-\u003edownNewColumns()**, all specified columns will be deleted after dropColumn()\n```php\n    public function newColumns()\n    {\n        return [\n            '{{%company}}' =\u003e [\n                'address' =\u003e $this-\u003estring(),\n                'is_active' =\u003e $this-\u003eboolean()\n            ]\n        ];\n    }\n    \n    public function safeUp()\n    {\n        $this-\u003eupNewColumns();\n    }\n    \n    public function safeUp()\n    {\n        $this-\u003edownNewColumns();\n    }    \n```\n\npublic function newIndex()\n-\nReturn an array where the key is the name of an existing table and the values are the index parameters via $this-\u003eindex()    \nIf you call the **$this-\u003eupNewIndex()** function, all specified indexes will be created via createIndex()  \nIf you call the function **$this-\u003edownNewIndex()**, all specified columns will be deleted using the dropIndex()\n```php\n    public function newIndex()\n    {\n        return [\n            '{{%company}}' =\u003e [\n                $this-\u003eindex()-\u003ecolumns(['name'])-\u003eunique(true)\n            ],\n        ];\n    }\n    \n    \n    public function safeUp()\n    {\n        $this-\u003eupNewIndex();\n    }\n    \n    public function safeUp()\n    {\n        $this-\u003edownNewIndex();\n    }        \n```    \n    \nWorking with foreign keys\n=========================\n\nCreate a table, specifying a foreign key, by table name only\n```php\n$this-\u003ecreateTable('{{%user}}', [\n    'id' =\u003e $this-\u003eprimaryKey(),\n    'company_id' =\u003e $this-\u003eforeignKey('{{%company}}')\n]);\n```        \n        \nAdding a foreign key column\n```php\n$this-\u003eaddColumn('{{%user}}', 'company_id', $this-\u003eforeignKey('{{%company}}'));\n```        \n\nAdding a foreign key to an existing column\n```php\n$this-\u003ealterColumn('{{%user}}', 'company_id', $this-\u003eforeignKey('{{%company}}'));\n```\n\nAdding foreign key with auto name\n```php\n$this-\u003eaddForeignKey(null, '{{%user}}', 'photo_id', '{{%photo}}', 'id');\n```\n\nDelete foreign key by column name\n```php\n$this-\u003edropForeignKeyByColumn('{{%user}}', 'company_id');\n```\n\nWorking with indexes\n==================\nCreate an index with an automatic name\n```php\n$this-\u003ecreateIndex(null, '{{%user}}', 'name');\n```\n\nDeleting an index by column name\n```php\n$this-\u003edropIndexByColumn('{{%user}}', 'name');\n```\n\n**(!)** It is necessary to pay attention, if there are several columns on the index, then it is necessary to specify them in the necessary sequence.\nIf there are several indexes with such a set and sequence, all of them will be deleted.\n**(!)** Does not work correctly with postgreSQL (https://github.com/yiisoft/yii2/issues/16639)\n```php\n$this-\u003ecreateIndex(null, '{{%user}}', ['name', 'surname']);\n$this-\u003edropIndexByColumn('{{%user}}', ['name', 'surname']);\n```\n\nPivot tables\n===============\nTo implement many-to-many tables, you can use the $this-\u003epivot() function, a table with 2 keys will be created.\nThe names of the keys in the PivotTable are generated automatically, so they can be set via refColumn() and sourceColumn()\n\nCreate a PivotTable by creating a table.\nThe result is the table {{%user}}[id] {{%pv_user_photos}}[user_id, photo_id]\n```php\n$this-\u003ecreateTable('{{%user}}', ['id' =\u003e $this-\u003eprimaryKey(), 'photos' =\u003e $this-\u003epivot('{{%photo}}')]);\n```\n\nCreate a PivotTable by adding a column.\n```php\n$this-\u003eaddColumn('{{%user}}', 'photos', $this-\u003epivot('{{%photo}}'));\n```        \n\nSpecify the name of the PivotTable\n```php\n$this-\u003eaddColumn('{{%user}}', 'photos', $this-\u003epivot('{{%photo}}')-\u003etableName('{{%album}}'));\n```   \n\nPivotTrait\n==========\nTrait to help work with pivot tables.\n\n`$company` - table model Company (requires trait PivotTrait)  \n`$user` - model of table User  \n`PvCompanyDirector` - a pivot table of the two models: company and user  \n`Pivot table` - a table which contains 2 primary key\n\n\nAdded to the table PvCompanyDirector a bunch of the end user company\n```php\n$company-\u003eaddPivot($user, PvCompanyDirector::class, $attributes = []);\n```\n\nGet the PvCompanyDirector model for the company-user bundle\n```php\n$company-\u003egetPivot($model, PvCompanyDirector::class, $condition = []);\n```\n\nRemoved a bunch of the user-company\n```php\n$company-\u003edeletePivot($model, PvCompanyDirector::class);\n```\n\nRemove all users from PvCompanyDirector for this company\n```php\n$company-\u003edeletePivots(PvCompanyDirector::class);\n```\n\nSave to a temporary link variable so that you can use them later\n```php\n$company-\u003estoragePivot($user, PvCompanyDirector::class, ['hire_at' =\u003e '2010-01-01 00:00:00']);\n$users = $company-\u003egetStoragePivots(PvCompanyDirector::class)); // The list of models that have been added earlier\n```\n\nThe preservation of the ties of a temporary variable.  \n$clear - completely clears all links before adding\n```php\n$company-\u003esavePivots($clear); // Save all links added via storagePivot\n```\n\nThe change in behavior of the migration class\n=====================================\npublic function createIndex($name, $table, $columns, $unique = false)  \n-\n* $name you can specify null to generate the index name automatically\n\npublic function createTable($table, $columns, $options = null)\n-\n* $columns supports the $this-\u003eforeignKey() and $this-\u003epivot()\n* if $options is not specified, options are pulled from $this-\u003etableOptions, if there are no options, then from **@tableOptions** to $columns\n\npublic function alterColumn($table, $column, $type)\n-\n* $type supports type $this-\u003eforeignKey()\n\npublic function addColumn($table, $column, $type)\n-\n* $type supports type $this-\u003eforeignKey() and $this-\u003epivot()\n\npublic function addPrimaryKey($name, $table, $columns)\n-\n* $name you can specify null to generate the index name automatically\n\npublic function dropColumn($table, $column)\n-\n* Before deleting the table, foreign keys are cleared\n\nAn example of a complete migration\n==================================\n```php\n\u003c?php\n\nuse yii\\db\\Migration;\nuse \\yii\\db\\Schema;\n/**\n * Class m180712_120503_init\n */\nclass m180712_120503_init extends Migration\n{\n    use \\carono\\yii2migrate\\traits\\MigrationTrait;\n\n    public function tableOptions()\n    {\n        return [\n            'mysql' =\u003e 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB'\n        ];\n    }\n\n    public function newTables()\n    {\n        return [\n            '{{%logs}}' =\u003e [\n                'data' =\u003e $this-\u003estring(),\n                '@tableOptions' =\u003e [\n                    'mysql' =\u003e 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=MyISAM'\n                ]\n            ],\n            '{{%user}}' =\u003e [\n                'id' =\u003e $this-\u003eprimaryKey(),\n                'name' =\u003e $this-\u003estring(),\n                'parents' =\u003e $this-\u003epivot('{{%user}}') // Create a pivot table on itself\n            ],\n            '{{%photo}}' =\u003e [\n                'id' =\u003e $this-\u003eprimaryKey(),\n                'user_id' =\u003e $this-\u003einteger()\n            ],\n            '{{%company}}' =\u003e [\n                'id' =\u003e $this-\u003eprimaryKey(),\n                'name' =\u003e $this-\u003estring(),\n                // Create a pivot table {{%pv_company_directors}}\n                'directors' =\u003e $this-\u003epivot('{{%user}}', 'director_id')-\u003ecolumns(\n                    [\n                        'hire_at' =\u003e $this-\u003edateTime(),\n                        // A foreign key with SET NULL rule is when you remove data from {{%user}}\n                        'hired_id' =\u003e $this-\u003eforeignKey('{{%user}}', null)-\u003eonDeleteNull()-\u003eunsigned()\n                    ]\n                ),\n                '@tableOptions' =\u003e [\n                    'mysql' =\u003e 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB'\n                ]\n            ],\n            '{{%pv_company_user_photo}}' =\u003e [\n                // Create a PivotTable of several keys\n                'company_id' =\u003e $this-\u003eforeignKey('{{%company}}', null, Schema::TYPE_PK),\n                'user_id' =\u003e $this-\u003eforeignKey('{{%user}}', null, Schema::TYPE_PK),\n                'photo_id' =\u003e $this-\u003eforeignKey('{{%photo}}', null, Schema::TYPE_PK),\n            ]\n        ];\n    }\n\n    public function newColumns()\n    {\n        return [\n            '{{%company}}' =\u003e [\n                // Create a FK to user\n                'user_id' =\u003e $this-\u003eforeignKey('{{%user}}'),\n                // Create a pivot table employees\n                'users' =\u003e $this-\u003epivot('{{%user}}')-\u003etableName('{{%employees}}')\n            ]\n        ];\n    }\n\n    public function newIndex()\n    {\n        return [\n            '{{%company}}' =\u003e [\n                $this-\u003eindex()-\u003ecolumns(['name'])-\u003eunique(true)-\u003elength(10)\n            ],\n        ];\n    }\n\n    public function safeUp()\n    {\n        $this-\u003eupNewTables();\n        $this-\u003eupNewColumns();\n        // Add a FK to an existing column\n        $this-\u003ealterColumn('{{%photo}}', 'user_id', $this-\u003eforeignKey('{{%user}}'));\n        $this-\u003eupNewIndex();\n        $this-\u003ecreateIndex(null, '{{%user}}', 'name');\n    }\n\n    public function safeDown()\n    {\n        $this-\u003edropIndexByColumn('{{%user}}', 'name');\n        $this-\u003edownNewIndex();\n        // Remove the FK on the column name\n        $this-\u003edropForeignKeyByColumn('{{%photo}}', 'user_id');\n        $this-\u003edownNewColumns();\n        $this-\u003edownNewTables();\n    }\n}\n```\nThe resulting database schema\n=========================\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\"https://raw.github.com/carono/yii2-migrate/HEAD/schema.png\"\u003e\n\u003c/p\u003e\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcarono%2Fyii2-migrate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcarono%2Fyii2-migrate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcarono%2Fyii2-migrate/lists"}