xutongle
- xutongle 赞了说说右眼跳,我要去看一声!!
- xutongle 赞了说说react & yii
- xutongle 赞了说说昨天去一家餐馆吃饭,门口挂着的横幅令我到现在念念不忘:你的肚子,是我搞大的。。。
- xutongle 赞了说说er
- xutongle 2016-03-23 已签到连续签到1天,获得了5个金钱
- xutongle 赞了回答
看了下源码,最终会调用:yii\db\QueryBuilder的createTable方法
public function createTable($table, $columns, $options = null) { $cols = []; foreach ($columns as $name => $type) { if (is_string($name)) { $cols[] = "\t" . $this->db->quoteColumnName($name) . ' ' . $this->getColumnType($type); } else { $cols[] = "\t" . $type; } } $sql = "CREATE TABLE " . $this->db->quoteTableName($table) . " (\n" . implode(",\n", $cols) . "\n)"; return $options === null ? $sql : $sql . ' ' . $options; }
他举了个例子:
$sql = $queryBuilder->createTable('user', [ 'id' => 'pk', 'name' => 'string', 'age' => 'integer', ]);
也就是数组中的key会作为字段,value作为类型,然后得看这个方法getColumnType
public function getColumnType($type) { if ($type instanceof ColumnSchemaBuilder) { $type = $type->__toString(); } if (isset($this->typeMap[$type])) { return $this->typeMap[$type]; } elseif (preg_match('/^(\w+)\((.+?)\)(.*)$/', $type, $matches)) { if (isset($this->typeMap[$matches[1]])) { return preg_replace('/\(.+\)/', '(' . $matches[2] . ')', $this->typeMap[$matches[1]]) . $matches[3]; } } elseif (preg_match('/^(\w+)\s+/', $type, $matches)) { if (isset($this->typeMap[$matches[1]])) { return preg_replace('/^\w+/', $this->typeMap[$matches[1]], $type); } } return $type; }
他首先判断传过来的类型是不是ColumnSchemaBuilder这个对象,如果不是,做一些正则判断,最终返回一个字符串,那反正最终就是构成一个合法的字段声明, 比如:
`phone` int(11) NULL DEFAULT NULL COMMENT '备注' ,
那你可以试试,在构建的时候,直接这样做:
'phone' => 'int(11) NULL DEFAULT NULL COMMENT "备注"'
这样应该也是可行的。具体需要你去试试。
- xutongle 赞了说说头像是狗头的都来点个赞!
- xutongle 赞了回答
我用的是sphinx搜索引擎,其实是三张表,索引到三个索引中,搜索的时候是三个索引一起被搜索,结果就是你看到的情况。
- xutongle 2016-03-19 已签到连续签到2天,获得了10个金钱