谁一开始就是大神?

PHP 第50页

PHP

AR模型没有按照预期工作[2.0]

momoFox阅读(124)

表设计如下:

CREATE TABLE `user` (
   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
   `username` varchar(50) NOT NULL DEFAULT '',
   `password` varchar(50) NOT NULL DEFAULT '',
   `authKey` varchar(50) NOT NULL DEFAULT '',
   `accessToken` varchar(50) NOT NULL DEFAULT '',
   `lock` int(11) NOT NULL DEFAULT '-1',
   PRIMARY KEY (`id`)
 ) ENGINE=InnoDB AUTO_INCREMENT=102 DEFAULT CHARSET=utf8;

有两条记录如下:
100 admin admin test100key 100-token -1
101 demo demo test101key 101-token -1

对应的AR模型类如下:

class User extends ActiveRecord implements \yii\web\IdentityInterface
{
    public $id;
    public $username;
    public $password;
    public $authKey;
    public $accessToken;

    /**
     * @inheritdoc
     */
    public static function findIdentity($id)
    {
        return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;
    }

    /**
     * @inheritdoc
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
        return self::find()->where(['accessToken'=>$token])->one();
    }

    /**
     * Finds user by username
     *
     * @param  string      $username
     * @return static|null
     */
    public static function findByUsername($username)
    {
        return self::find()->where(['username'=>$username])->one();
    }

    /**
     * @inheritdoc
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @inheritdoc
     */
    public function getAuthKey()
    {
        return $this->authKey;
    }

    /**
     * @inheritdoc
     */
    public function validateAuthKey($authKey)
    {
        return $this->authKey === $authKey;
    }

    /**
     * Validates password
     *
     * @param  string  $password password to validate
     * @return boolean if password provided is valid for current user
     */
    public function validatePassword($password)
    {
        return $this->password === $password;
    }
}

具体调用代码如下:

$username = 'demo';
$user = User::findByUsername($username);
print_r($user);

按照AR模型的定义,应该输出如下的结果的:

app\models\User Object
(
    [id] => 100
    [username] => admin
    [password] => admin
    [authKey] => test100key
    [accessToken] => 100-token
    [_attributes:yii\db\BaseActiveRecord:private] => Array
        (
            [id] => 100
            [username] => admin
            [password] => admin
            [authKey] => test100key
            [accessToken] => 100-token
            [lock] => -1
        )

    [_oldAttributes:yii\db\BaseActiveRecord:private] => Array
        (
            [id] => 100
            [username] => admin
            [password] => admin
            [authKey] => test100key
            [accessToken] => 100-token
            [lock] => -1
        )

    [_related:yii\db\BaseActiveRecord:private] => Array
        (
        )

    [_errors:yii\base\Model:private] => 
    [_validators:yii\base\Model:private] => 
    [_scenario:yii\base\Model:private] => default
    [_events:yii\base\Component:private] => Array
        (
        )

    [_behaviors:yii\base\Component:private] => Array
        (
        )

)

但是数据结果却是:

app\models\User Object
(
    [id] => 
    [username] => 
    [password] => 
    [authKey] => 
    [accessToken] => 
    [_attributes:yii\db\BaseActiveRecord:private] => Array
        (
            [id] => 100
            [username] => admin
            [password] => admin
            [authKey] => test100key
            [accessToken] => 100-token
            [lock] => -1
        )

    [_oldAttributes:yii\db\BaseActiveRecord:private] => Array
        (
            [id] => 100
            [username] => admin
            [password] => admin
            [authKey] => test100key
            [accessToken] => 100-token
            [lock] => -1
        )

    [_related:yii\db\BaseActiveRecord:private] => Array
        (
        )

    [_errors:yii\base\Model:private] => 
    [_validators:yii\base\Model:private] => 
    [_scenario:yii\base\Model:private] => default
    [_events:yii\base\Component:private] => Array
        (
        )

    [_behaviors:yii\base\Component:private] => Array
        (
        )

)

这样使用$user->id显然不能访问该记录的字段,所谓AR模型也就没与意义了,此时如果我给$user->username赋新值,然后调用$user->save(),我的本意是修改一条记录的,但是结果却是新增了一条记录。这个问题有没有人察觉过,这究竟是yii2的bug还是有意为之,如果有意为之的话,这样做的道理何在?

YII2 的AR底层已经重写了get()和set()方法 他取值的时候 get()方法是在attributes这个地方获取的 所以请不要在模型中定义数据库字段 如果不懂 用gii生成数据库模型 或者看看框架下载安装好的例子

URL地址访问[2.0]

Kimfrog阅读(125)

QQ图片20160812094452.png
如图 为什么进不去视图页面

你这直接打开目录了。。要访问文件啊 比如index.php
错误类型呢?进不去是怎么个进不去
这样谁看得懂?
首先你的localhost:8000指向哪?其次你的目录结构是什么?
初期阿里云的虚拟主机,域名www.xxxx.com/frontend/web/index.php前台首页如何变成 www.xxxx.com/index.php,也能访问,麻烦说个步骤或者发个解决办法的网址。注意是放到网上域名的,不是在电脑本地。
如果你的安装路径是 /www/web/xxxxcom文件夹
那么,你现在nginx的域名指向的路径是/www/web/xxxxcom,对吧,
因此你的访问路径是:www.xxxx.com/frontend/web/index.php
你需要吧nginx的域名指向的文件路径设置成/www/web/xxxxcom/frontend/web
然后添加默认访问文件index.php即可。

yii2动态生成的datepicker没有用[2.0]

TinaCherry阅读(125)

yii2-dynamicform用了这个动态生成的控件,但是动态生成的datepicker没有用,有人知道怎么回事吗

楼主检查参数,有一个参数是限制动态新增的个数的,仔细检查一下,或者读一下官网文档。

如果将这条sql语句转换成YII的查询[2.0]

Noblecrab阅读(124)

SELECT * FROM `workbasicinfo` WHERE worktitle like '%11%' and worktag like '%22%'and (worktitle like '%33%' or worktag like '%33%')  

我现在写的是这样的:

 $query = Worksinfo::find()
			->andFilterWhere(['like', 'workid', $this->workid])
			->andFilterWhere(['like', 'makerid', $this->makerid])
			->andFilterWhere(['like', 'worktitle', $this->worktitle])
			->andFilterWhere(['like', 'worktag', $this->worktag])
			->andFilterWhere(['like', 'makername', $this->makername])
			->asArray();

yii-admin权限管理语言设置[2.0]

Noblehope阅读(138)

rbac安装好了,但是是英文界面,怎么设置成中文?

_JM$L)JCFS7II))M6MMM@6J.png

<project>/common/config/main-local.php配置文件中指定语言'language' => 'zh-CN',配置即可。
这个插件默认和站点是统一的语言配置。
看看吧! 改整體配置語言
https://github.com/mdmsoft/yii2-admin/issues/171
但建議你看一下此目錄下面 @vendor/mdmsoft/yii2-admin/messages/
有支援幾種語言!
如果用的是advanced版本,那么这个配置要在前后台都写还是写个公共的

如何防止在没有登录的情况下,随意跳转到后台的页面[1.0]

Bobhappy阅读(136)

请问如何防止在没有登录的情况下,随意跳转到后台的页面,,,代码应该如何写。。。。。请大神给一下代码

看到《全站搜索》那几个字了吗,去搜一下
所有需要登陆的控制器继承一个新的controller,然后在这个controller的init里判断如果没登陆就跳到登陆页
二楼正解,继承+权限判断

删除文件、图片的接口[2.0]

Newfans阅读(127)

yii有没有删除文件和图片的接口啊?文档上面好像没找到啊

直接unlink不可以么,
unlink()就可以了啊
php函数unlink($filepath);
yii中没有封装相应的函数,因为没必要。
你直接,if(){unlink(图片路径)};就可以了

restful上传图片问题[2.0]

JasonCherry阅读(121)

上传图片

$url = "http://localhost/v1/accident/upload-image";

$file = realpath('_mars.jpg'); //要上传的文件


$data = array(
    'uid'=>'123',
    'appcode'=>'bhwhc',
    'file' => '@'.$file,
    'flg' => 2
);

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url );
curl_setopt ($ch, CURLOPT_POST, true );
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );

$res = curl_exec($ch);
curl_close($ch);
$json_obj = json_decode($res,true);
var_dump($json_obj);exit;

restful 接口

     public function actionUploadImage()
     {
        
         if(Yii::$app->request->isGet)
         {
           $request = Yii::$app->request->get();
         }elseif (Yii::$app->request->isPost)
         {
          $request = Yii::$app->request->post();
         }
         $file = UploadedFile::getInstance($model,'file');
         //return $request;
         $uid = $request['uid'];
         $appcode = $request['appcode'];
         if(empty($uid) || empty($appcode))
         {
             return  ['code'=>'1001','message'=>'参数缺失'];
         }
         
         $userinfo = KckpUser::findOne(['app_user_id'=>$uid,'app_code'=>$appcode]);
         if(empty($userinfo))
         {
             $model = new KckpUser();
             $model->app_user_id = $uid;
             $model->app_code = $appcode;
             $model->create_time = date('Y-m-d H:i:s');
             $model->save();
         }
       
         if($_FILES['file'])
         {
             //$filename = $_FILES['file']['name'];
             return  ['code'=>'200','message'=>'有数据上传'];
         }else{
             return ['code'=>'400','message'=>'无文件上传'];
         }
         $modelClass = $this->modelClass;
         $query = $modelClass::find();
         return new ActiveDataProvider([
             'query' => $query
         ]);
     }

不知道那里有问题,一直都是没有文件上传

查看php版本,5.5前是用’@’+文件全路径,5.5后用CURLFile类。
你先一步步打印,看程序走到哪里了?
能打印出来上传的文件资源吗

dropDownList的option选项怎样添加disabled属性?[2.0]

Beedolphin阅读(122)

dropDownList 的option选项怎样添加 disabled属性

$form->field($model, 'name')->dropDownList(['value'=>'title']);

需要生成这样的:
<option disabled value="1">请选择</option>
求助大神们

你要的是这个效果吧?
楼上正解:
不过楼主可能更需要:
感 常感就阿斯蒂芬
楼主解决了吗,可以直接禁用这个select吗

关于自义验证[2.0]

JimLion阅读(140)

哪位大神懂yii2自定义验证规则的:
比如数据表字段中判断最大值不能小于最小值,

     public function validateMultipleax_credits($attribute, $params) 
{
    if([$this->max_credits] >= [$this->min_credits]) {
        return $this->max_credits;
    }  
    return $this->min_credits;
}

请问我是哪里错了?

看看这里是否能解决你的问题,http://blog.csdn.net/cjqh_hao/article/details/52028957

登录

找回密码

注册