kerin0364

kerin0364

这家伙有点懒,还没写个性签名!

  • 财富值255
  • 威望值0
  • 总积分365

个人信息

  • 2016-02-25 已签到
    连续签到1天,获得了5个金钱
  • 2015-11-23 已签到
    连续签到1天,获得了5个金钱
  • 2015-11-19 已签到
    连续签到1天,获得了5个金钱
  • 贴子很有用,感谢分享!想学习一下GridView,方法里面'delete' => function($url, $model, $key){}函数的三个参数,$url, $model, $key传入顺序和命名是固定的吗?如果想把后台model里定义好的数组传入GridView里的delete方法中使用,如何处理呢?

  • 自己来解答,直接上代码。

    创建链接测试用小程序如下:

    
    namespace app\models;
    
    use Yii;
    use yii\db\ActiveRecord;
    use \yii\db\Connection;
    
    class CmsSyncData extends ActiveRecord
    {
        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'id' => '新闻ID',
            ];
        }
        /**
         * @inheritdoc
         */
        public static function getDb()
        {
            return new Connection([
                'dsn' => 'oci:dbname=//localhost:portno/dbname;charset=utf8',
                'username' => 'XXX',
                'password' => 'XXX',
            ]);
        }
        /**
         * Finds news by $id
         *
         * @param  string $id
         * @return static|null
         */
        public static function findByNewsId($id)
        {
            $connection = self::getDb();
            $command = $connection->createCommand('SELECT * FROM content_news WHERE id='.$id);
            $result = $command->queryOne();
            return $result;
        }
    
    }
    

    需要改YII2.0的代码:
    \vendor\yiisoft\yii2\db\Connection.php
    找到下面的函数,加上配置“, 'oci'”就可以了。

     /**
         * Initializes the DB connection.
         * This method is invoked right after the DB connection is established.
         * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`
         * if [[emulatePrepare]] is true, and sets the database [[charset]] if it is not empty.
         * It then triggers an [[EVENT_AFTER_OPEN]] event.
         */
        protected function initConnection()
        {
            $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            if ($this->emulatePrepare !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {
                $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->emulatePrepare);
            }
            if ($this->charset !== null && in_array($this->getDriverName(), ['pgsql', 'mysql', 'mysqli', 'cubrid', 'oci'])) {
                $this->pdo->exec('SET NAMES ' . $this->pdo->quote($this->charset));
            }
            $this->trigger(self::EVENT_AFTER_OPEN);
        }
    
    
  • 发表了说说
    [表情48]要开始学习YII了,加油加油!~~
见习主管 等级规则
365/500
资料完整度
30/100
用户活跃度
0/100

Ta的关注

6

Ta的粉丝

8

Ta的访客

18