谁一开始就是大神?

PHP 第43页

PHP

$model->load(Yii::$app->request->post()的作用是什么?[2.0]

Gracequeen阅读(104)

放在if里面判断,成功代表的是有值,还是只是代表有post值,还是代表有post值,并通过了验证

成功代表的是有值。而你的值是request->post()获取的,所以代表的是有post值。
load()的作用是批量填充模型。因为你会在load前面发现 $model = new Model。此时的 $model 是 new 出来的,是新的,只是个骨头架子,纸老虎,所以它迫切需要一些值来填充自己,否则它除了能为前端 ActiveForm 提供一些JS验证外,屁用没有。而此时,load() 应运而生。
而解决温饱飞黄腾达之后,Yii 会审视 $model。这小子做的这些事符不符合我给他定的规矩。于是Yii 根据 $model 调用了你接下来看到的 validate(),然后不合规矩就干掉他,即使他是苍蝇或者老虎。所以验证部分 load() 不会去做,而是交给了 validate()。就像贪官能发现自己是贪官吗?
post提交的数据,request发送。
问题能否再详细一些?
获取post提交的值 并调用setAttributes()给model里的属性赋值
$model->load(Yii::$app->request->post())成功表示验证通过
用Yii::$app->request->post()数据填充$model

【加急】github克隆下来的项目怎么处理?[2.0]

NeoKeo阅读(101)

问题1:在github上克隆下来的项目,在composer update之前,是必须要先安装 composer global require "fxp/composer-asset-plugin:1.2.0" 这个插件吗?

问题2: 是每次克隆下来一个项目就要安装一次 composer global require "fxp/composer-asset-plugin:1.2.0" 这个插件吗?

问题3: 克隆下来的项目 是要用 composer update 还是 composer install 有什么区别?

composer install命令(主要)用于生产环境,composer.lock文件记录项目当前版本信息,当执行install命令时,会检测lock文件的各扩展
版本与最新版本差别,如果有则更新到最新版。而composer update命令也会执行上述所讲,但是如果在composer.json文件添加库到require字段时,
就必须用composer update命令了。但这时会更新其他库的内容,此时如果只是添加某个库而不更新其他库(例如生产环境),就要使用composer
看不出你这有啥急的。
这个插件只需要一次安装,具体的版本跟你的composer版本有关。
问题一:不一定
问题二:不是
问题三:update是安装最新的包,install是安装指定版本的包。
想知道为啥吗?
search the fucking packagist.org and read the fucking manual.
第一个问题,全局安装或者不全局都可以,
第二个问题,不需要
第三个问题,上边几位说的很明显了。update是安装最新的包,install是安装指定版本的包。
用sourcetree git简单化

php获取表单内容,获取后对其处理,处理后放入该表单[2.0]

Beeseagull阅读(115)

1.PNG
在这个表单中输入城市名称,例如“北京”,现有一个函数将“北京”进行处理后得到“beijing”和“bj”,处理后得到的“beijing”和“bj”分别自动补充进下面的栏目中。这个功能在_form.php中怎么实现。(处理函数已经有了)。

2.PNG

js对value进行赋值,目测可行。
简称和拼音都不要用户输入,那你整个输入框干啥。直接后台处理就行了呀。

DatePicker日期控件转换格式问题[2.0]

fationhope阅读(112)

QQ截图20161120183127.png
使用DatePicker控件,新增一条记录的格式调好了,数据库中是以时间戳形式存储的时间,在修改一条记录的时候,如上图所示,哪位朋友知道在哪里可以转化格式,成为yyyy-mm-dd的形式,谢谢。

model里写的getDate,setDate方法,方法里做转换,前台model使用date
选择时间框,绑定的是dateTime
http://www.bootcss.com/p/bootstrap-datetimepicker/
http://www.runoob.com/jqueryui/example-datepicker.html
做程序,文档都不去看。。。。。。。。。。。。。。。
其实就是text框内容,后台php、前端js都可以做到

php函数array_diff()为什么对比的结果不对?[2.0]

momopony阅读(107)

我现在做修改文章操作,用到 array_diff() 函数,对比提交的新数据和数据库里面的旧数据不一样的地方,结果有一个字段不一样却没对比出来,望求解,比如以下两个数组

$new=["abc"=>"abc","c_isrecommend"=>"1", "c_singer"=>"1"];
$old=["abc"=>"abc","c_isrecommend"=>"0", "c_singer"=>"1"];
print_r(array_diff($new,$old));

有差别,但对比的结果是空数组

Array ( )

不知道怎么回事,有知道为什么这样的大神么?求解啊,如果这个函数不准确,有没有别的方法能对比两个数组的差别并筛选出来的方法呢?望解答

array_diff 是返回一个数组,该数组包括了所有在 array1 中但是不在任何其它参数数组中的值。
你$new的c_isrecommend=>1 的值在$old的已经存在了 和$old的c_singer=>1相等
你把$new改成 $new = array(‘abc’=>’abc’,’c_isrecommend’=>’2′,’c_singer’=>’1′);就行了
楼主何不试试这个呢:array_diff_assoc
函数只检查了 数组中的一维

Yii项目不需要build就可以部署,如何通过加号进行部署?[1.0]

SlyJames阅读(117)

用Yii1 进行的项目开发

每次部署都会因为缓存问题访问到的不是已更改后的page
每次部署完都需要**手动清除缓存**来访问更改后的页面

综上,相同给文件加版本号来解决问题

所以,如添加版本号呢?

1、可以设置一个全局变量保存版本信息,在相应的链接地址后面加上 &v=1.0.1
2、可以写个脚本在部署时自动清除缓存
docker大法好,每次更新重启容器后runtime和asset自动清理。

yii2.0basic图片上传不了问题[2.0]

lovelybubble阅读(108)

我的视图:

/view/ext/_form.php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $model app\models\Ext */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="ext-form">

    <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?> 

    <?= $form->field($model, 'image1')->fileInput() ?>

    <?= $form->field($model, 'image2')->fileInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'info')->textInput(['maxlength' => true]) ?>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? '创建' : '更新', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>
    <?php ActiveForm::end(); ?>
</div>

这是model
models/ext.php

<?php
namespace app\models;
use Yii;
use yii\web\UploadedFile;
/**
 * This is the model class for table "ext".
 *
 * @property integer $id
 * @property string $image1
 * @property string $image2
 * @property string $info
 */
class Ext extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'ext';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['image1', 'image2'], 'string', 'max' => 60],
            [['image1', 'image2'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg'],
            [['info'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'image1' => 'logo图片',
            'image2' => '微信二维码图片',
            'info' => '站点简介',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function upload()
    {        
        if ($this->validate()) 
        {
            $randName=date("Y").date("m").date("d").date("H").date("i").date("s").rand(100, 999).'-'.md5(microtime());   //生成随机文件名
            $uploadPath= 'uploads/' . $randName . '.' . $this->image1->extension;  //设置保存路径, 为 backend\web\uploads
            $this->image1->saveAs($uploadPath);  //保存文件
            return Url::to('app/web/'.$uploadPath,true); //返回文件的 url 路径,使用 Url 帮助类。
        } 
        else 
        {            
            return false;
        }
    }
}

控制器:
controllers/Extcontroller.php

<?php
namespace app\controllers;
use Yii;
use app\models\Ext;
use app\models\ExtSearch;
use yii\web\Controller;
use yii\web\UploadedFile;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

class ExtController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all Ext models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new ExtSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Ext model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        ......
    }

    /**

    public function actionCreate()
    {
        $model = new Ext();
        //$model->wb_logo = UploadedFile::getInstance($model, 'image1');
        if ($model->load(Yii::$app->request->post()) ) {
            //var_dump(Yii::$app->request->post());die;
            $model->save();
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    protected function findModel($id)
    {
        if (($model = Ext::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    
    public function actionUpload()
    {
        $model = new Ext();

        if (Yii::$app->request->isPost) {
            $model->image1 = UploadedFile::getInstance($model, 'image1');
            if ($model->upload()) {
                // 文件上传成功
                return;
            }
        }
        return $this->render('upload', ['model' => $model]);
    }
}

现在就是一直成功不了

[[‘image1’, ‘image2’], ‘string’, ‘max’ => 60],
[[‘image1’, ‘image2’], ‘string’, ‘max’ => 60],

新手提问:怎么统计各个页面访问人数[2.0]

Bellaslim阅读(116)

你自己都说出来答案了。在新闻表的加个字段。读一次新闻该字段自增1
@zhuxinghui 问题乱码了,重新编辑下
每次访问页面都在数据库字段加1 吧,我是新手
加个visit_count

表单错误提示信息不现实[2.0]

BraveStone阅读(99)

表单验证会提示错误信息 我新创一个表单做添加用户的 当用户名输入框获取焦点没输入内容再获取其他输入框的焦点时,应该用户名的框会提示错误信息,我这没提示
QQ图片20160928154913.png

QQ图片20160928154928.png

QQ图片20160928154933.png

QQ图片20160928154939.png

是的,是因为你把布局文件禁止了。布局文件里的beginBody那些方法以及AppAsset里引入的YII.js都和 ActiveForm 的前端验证有关。
给你的class = "form-group" 再添加一个"require"类咯,
当你使用<?= $form->field($model, 'password')->passwordInput() ?>之后,就会生成
所以你不必要自己再写那么一大堆,

登录

找回密码

注册