javalzbin

javalzbin

多看,多想,多动手。

  • 财富值6926
  • 威望值150
  • 总积分8796

个人信息

  • 2016-03-23 已签到
    连续签到30天,获得了20个金钱
  • 2016-03-22 已签到
    连续签到29天,获得了20个金钱
  • 那可以这样:

    return [
        // ...
        'components' => [
            // ...
            'db' => [
                'class' => 'yii\db\Connection',
                'dsn' => 'mysql:host=localhost;dbname=mydatabase', 
                'username' => 'root',
                'password' => '',
                'charset' => 'utf8',
            ],
    //可以配置多个
            'db2' => [
                'class' => 'yii\db\Connection',
                'dsn' => 'mysql:host=localhost;dbname=mydatabase2', 
                'username' => 'root',
                'password' => '',
                'charset' => 'utf8',
            ],
        ],
        // ...
    ];
    

    然后

    $connection = \Yii::$app->db;
    $connection2 = \Yii::$app->db2;
    

    然后

    //一旦有了连接实例就可以通过yii\db\Command执行 SQL 查询。你拿你需要的连接实例去查就好了
    $command = $connection->createCommand('SELECT count(*) FROM post');
    $post = $command->queryOne();
    
  • 仅供参考:

    <span ref="Yii::app()->createUrl('控制器/方法', array('参数1'=>'值', '参数2'=>'值'))"  class="autoload"></span>
    

    方法里用echo输出:

    public function actionReadData()
    {
        //业务处理...
         
        echo "";
    }
    

    其次,用js也可以实现,用页面加载完成后,用js取回数据,填充到界面上

  • 2016-03-21 已签到
    连续签到28天,获得了20个金钱
  • 2016-03-20 已签到
    连续签到27天,获得了20个金钱
  • 2016-03-19 已签到
    连续签到26天,获得了20个金钱
  • 2016-03-18 已签到
    连续签到25天,获得了20个金钱
  • 看了下源码,最终会调用: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 "备注"'
    

    这样应该也是可行的。具体需要你去试试。

  • 2.0.7不支持之前版本的实现了?是不能那样用了吗?

副总裁 等级规则
8796/10000
资料完整度
40/100
用户活跃度
0/100

Ta的关注

0

Ta的粉丝

6

Ta的访客

20