林小陌
- 林小陌 赞了回答
文件上传 多文件
`控制器 yii\web\UploadedFile; $model->file = UploadedFile::getInstances($model, 'file'); $file_arr = array(); foreach($model -> file as $file){ $file_name = 'uploads/' . time() . rand(111111,999999).'.'. $file->extension; $file -> saveAs($file_name); $file_arr[] = $file_name; } 视图: <?php use yii\widgets\ActiveForm; use yii\helpers\Html; ?> <?php $form=ActiveForm::begin(['options'=>['enctype'=>'multipart/form-data'],'action'=> ['news/upfile'],'method'=>'post',])?> <?=$form->field($model,'file[]')->fileInput(['multiple'=>true])?> <?php echo Html::submitButton('提交',['class' => 'btn btn-primary'])?> <?php ActiveForm::end();?>
`
- 林小陌 赞了回答
dao模式分页,我看了好多教程,他们用的那个类,在框架里面根本找不到。
<?php
namespace frontend\controllers;
use yii\web\controller;
use yii;
class DataController extends Controller{public function actionIndex(){ $db = new \yii\db\Connection([ 'dsn' => 'mysql:host=localhost;dbname=aaa', 'username' => 'root', 'password' => '', ]); $db->open(); //查询 $command = $db->createCommand('SELECT * FROM zhuce')->queryAll(); //修改 //$command = $db->createCommand("UPDATE zhuce SET uname='qweqdqdq' WHERE id=2")->execute(); //删除 //$command = $db->createCommand("delete from zhuce where id =2")->execute(); //添加 //$command = $db->createCommand("insert into zhuce (uname,pwd)values('ok','123')")->execute(); //添加多条 //$command = $db->createCommand()->batchInsert('zhuce', ['uname', 'pwd'], [['Tom', 30],['Jane', 20],['Linda', 25]])->execute(); $session = Yii::$app->session; }
}
?>
@wxy1996 DAO模式没有很会用,感谢。
不知道你会不会DAO模式的分页呢?- 林小陌 关注了15210999641
文件上传 多文件
`控制器 yii\web\UploadedFile; $model->file = UploadedFile::getInstances($model, 'file'); $file_arr = array(); foreach($model -> file as $file){ $file_name = 'uploads/' . time() . rand(111111,999999).'.'. $file->extension; $file -> saveAs($file_name); $file_arr[] = $file_name; } 视图: <?php use yii\widgets\ActiveForm; use yii\helpers\Html; ?> <?php $form=ActiveForm::begin(['options'=>['enctype'=>'multipart/form-data'],'action'=> ['news/upfile'],'method'=>'post',])?> <?=$form->field($model,'file[]')->fileInput(['multiple'=>true])?> <?php echo Html::submitButton('提交',['class' => 'btn btn-primary'])?> <?php ActiveForm::end();?>
`
但是假如我要循环入库,是不是要重新创建对象
``` 首先引入 yii\data\Pagination; 还有各种模型 控制器: new一个AR对象出来 $search = Yii::$app->request->get('search_key'); $where = '1 = 1'; if ($search) { $where .= ' and JName like "%'.$search.'%"'; } $pageArr = $model -> find()->where($where); $pagination = new Pagination(['totalCount' => $pageArr -> count(),'pageSize'=>'3']); $result = $pageArr ->join('INNER JOIN', 'recruit_enterprise','recruit_enterprise.CId = recruit_job.CId') ->select('*') ->offset($pagination ->offset) ->limit($pagination ->limit) ->where($where) ->asArray() ->all(); return $this -> render('job_list', ['result' => $result, 'pagination' => $pagination, ] );
以上是本人个人代码 仅供参考
你发的这个很实用,谢谢你
- 林小陌 回答了问题 yii2 AR搜索分页
yii2 框架中的一些常用操作,望各位大神指导。