Article^ Parent
Yii2 笔记
Date: | 2018-07-24 00:00:00 |
Description: | Yii2 使用笔记。 |
Keywords: | Yii2, 使用, PHP |
Category: | engineering_technology/computer/software/web/yii2 |
Tag: | yii2, php |
Link: | https://www.diewuxi.com/blog/article/102.html |
Changelog
* 2018-08-23
* Add: Share host directory setting.
* 2018-08-21
* Add: File mode problem.
* Add: Add deploy.
* 2018-08-16
* Add: drop_xxx_from_yyy not work, drop_xxx_column_from_yyy_table work.
* 2018-08-06
* Add: add_xxx_to_yyy not work.
* 2018-07-24
* Done
Introduction
Version: yii2 basic 2.0.15 site.
Install
- Use offical site file, 2.0.15
- uncompress
- apache setting
- setting files permission
In root directory of the program, chmod for apache.
chmod runtime 0777
chmod web/assets 0777
chmod yii 0755
May chmod other directories, when gii create code. Such as controllers, models, views. And other directory you program may write to, such as files upload.
- Use composer
Run
Open yii server(php server):
php yii serve
Open browser
Migration
Create migration file
yii migrate/create <name> <options>
<name>:
a description of the migration, only number, alphabet and underline are allowed
create_<table_name> can generate extra code for create a table named <table_name>
drop_<table_name> can generate extra code for drop a table named <table_name>
add_<column_name>_to_<table_name> can generate extra code for add column
drop_<column_name>_from_<table_name> can generate extra code for add column
<options>
--fields=<fields_setting> <fields_setting> example: "author_id:integer:notNull:foreignKey(user),category_id:integer:defaultValue(1):foreignKey,title:string,body:text"
--migrationPath=<path> string (默认值为 @app/migrations),指定存放所有迁移类文件的目录。该选项可以是一个目录的路径, 也可以是 路径别名。需要注意的是指定的目录必选存在, 否则将会触发一个错误。
edit migration file
fileds map:
----------------------------------------------------------------------------------------------------------
SCHEMA_CACHE_VERSION 1 Schema cache version, to detect incompatibilities yii\db\Schema
in cached values when the data format of the
cache changes.
TYPE_BIGINT 'bigint' yii\db\Schema
TYPE_BIGPK 'bigpk' yii\db\Schema
TYPE_BINARY 'binary' yii\db\Schema
TYPE_BOOLEAN 'boolean' yii\db\Schema
TYPE_CHAR 'char' yii\db\Schema
TYPE_DATE 'date' yii\db\Schema
TYPE_DATETIME 'datetime' yii\db\Schema
TYPE_DECIMAL 'decimal' yii\db\Schema
TYPE_DOUBLE 'double' yii\db\Schema
TYPE_FLOAT 'float' yii\db\Schema
TYPE_INTEGER 'integer' yii\db\Schema
TYPE_JSON 'json' yii\db\Schema
TYPE_MONEY 'money' yii\db\Schema
TYPE_PK 'pk' yii\db\Schema
TYPE_SMALLINT 'smallint' yii\db\Schema
TYPE_STRING 'string' yii\db\Schema
TYPE_TEXT 'text' yii\db\Schema
TYPE_TIME 'time' yii\db\Schema
TYPE_TIMESTAMP 'timestamp' yii\db\Schema
TYPE_TINYINT 'tinyint' yii\db\Schema
TYPE_UBIGPK 'ubigpk' yii\db\Schema
TYPE_UPK 'upk' yii\db\Schema
----------------------------------------------------------------------------------------------------------
下是所有 migration 类提供的数据库访问方法的列表:
execute(): 执行一条 SQL 语句
insert(): 插入单行数据
batchInsert(): 插入多行数据
update(): 更新数据
delete(): 删除数据
createTable(): 创建表
renameTable(): 重命名表名
dropTable(): 删除一张表
truncateTable(): 清空表中的所有数据
addColumn(): 加一个字段
renameColumn(): 重命名字段名称
dropColumn(): 删除一个字段
alterColumn(): 修改字段
addPrimaryKey(): 添加一个主键
dropPrimaryKey(): 删除一个主键
addForeignKey(): 添加一个外键
dropForeignKey(): 删除一个外键
createIndex(): 创建一个索引
dropIndex(): 删除一个索引
addCommentOnColumn(): adding comment to column
dropCommentFromColumn(): dropping comment from column
addCommentOnTable(): adding comment to table
dropCommentFromTable(): dropping comment from table
示例:
public function up()
{
$this->createTable('news', [
'id' => $this->primaryKey(),
'title' => $this->string()->notNull(),
'content' => $this->text(),
]);
}
public function down()
{
$this->dropTable('news');
}
note:
I suspect $this->primarykey()
, primaryKey can written as pk, according above table.
I find that create_xxx
do not work, instead create_xxx_table
work.
add_xxx_to_yyy
not work
drop_xxx_from_yyy
not work
drop_xxx_column_from_yyy_table
work
Excute migration command
yii migrate
migrate may create migrations table in the database.
你可以使用如下命令列出那些提交了的或者是还未提交的迁移:
yii migrate/history # 显示最近10次提交的迁移
yii migrate/history 5 # 显示最近5次提交的迁移
yii migrate/history all # 显示所有已经提交过的迁移
yii migrate/new # 显示前10个还未提交的迁移
yii migrate/new 5 # 显示前5个还未提交的迁移
yii migrate/new all # 显示所有还未提交的迁移
修改历史
yii migrate/mark 180806_115901
如果发现已提交的迁移不满意,并且这些迁移不能还原,可以删除掉这些迁移文件,这时数据库中仍有记录,还要手动还原更改到这些迁移之前,再使用上述命令使迁移位置记录到参数对应的位置处,这时数据库中的记录也删除了,就好像没有进行过这些迁移一样。 然后可以重新写迁移文件,做迁移。
File mode problem
Set dirMode
, fileMode
if invoved file access problem.
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
'dirMode' => 0777,
'fileMode' => 0777,
'enabled' => false,
],
],
],
'assetManager' => [
'appendTimestamp' => true,
'dirMode' => 0777,
'fileMode' => 0777,
'forceCopy' => true,
],
$config['modules']['debug'] = [
'class' => 'yii\debug\Module',
// uncomment the following to add your IP if you are not connecting from localhost.
//'allowedIPs' => ['127.0.0.1', '::1'],
'dirMode' => 0777,
'fileMode' => 0777,
],
Deploy
Clear tempary file
rm -r runtime/*
rm -r web/asset/*
Share hosts, document root top directory
app->request->baseUrl default to rtrim(dirname(scriptUrl), '\\/')
app->request->scriptUrl default to related to scriptFile, scriptFile default to $SERVER['SCRIPT_FILENAME']
app->homeUrl default to baseUrl . '/' or scriptUrl
app->urlManager->baseUrl default to baseUrl
app->urlManager->scriptUrl default to scriptUrl
app->assetManager->basePath default to @webroot/assets, @webroot default to scriptFile, scriptFile default to $SERVER['SCRIPT_FILENAME']
app->assetManager->baseUrl default to @web/assets, @web default to baseUrl
Last modified: 2018-08-23
Comments [0]
There is no comments now.