wangqy010
- wangqy010 赞了回答
为什么不用yii\web\UploadedFile?
- wangqy010 赞了回答
看着多了个空格。加上引号,或者把空格去掉,应该就可以了。
- wangqy010 2016-12-30 已签到连续签到1天,获得了5个金钱
- wangqy010 赞了评论
<?php //控制器中
namespace app\controllers;
header('content-type:text/html;charset=utf8');
use Yii;
use app\models\Position;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
class PositionController extends Controller{... ...其他代码... ...
public function actionView($p_id){ //跳往修改页面
//$id = Yii::$app->request->get('p_id');和括号里接值效果是一样的(因为前页是get传值) $arr=Position::find()->where(['p_id'=>$p_id])->with('company')->asArray()->one(); return $this->render('view',$arr);//因为数据表Position字段和视图view.php文本框、单选框等等的标签名字完全一致,所以这里直接用了$arr,否则要用[’key’=>’value’]的形式。 } public function actionUpdate(){ //执行修改 $item=Yii::$app->request->post(); //接修改后的值 $query=new Position(); $sign=$query->rewrite($item); //封装好的方法 if($sign==1){ $this->redirect(['position/index']); }else{ throw new NotFoundHttpException(); } }
}
<?php //模型中
namespace app\models;
use yii\db\ActiveRecord;
use Yii;
use yii\data\Pagination;
class Position extends ActiveRecord{
... ...其他代码... ...
public function Rewrite($data){ //即使新值和旧值完全相同,依然可以提交并执行修改$res=Position::find()->where(['p_id'=>$data['p_id']])->one(); $res->p_name=$data['p_name']; $res->p_time=time(); $res->p_request=$data['p_request'][0];//数组需要处理成字符串 $sign=$res->save(); return $sign;
}
} - wangqy010 评论了教程 Yii 修改数据的两种方式
<?php //控制器中
namespace app\controllers;
header('content-type:text/html;charset=utf8');
use Yii;
use app\models\Position;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
class PositionController extends Controller{... ...其他代码... ...
public function actionView($p_id){ //跳往修改页面
//$id = Yii::$app->request->get('p_id');和括号里接值效果是一样的(因为前页是get传值) $arr=Position::find()->where(['p_id'=>$p_id])->with('company')->asArray()->one(); return $this->render('view',$arr);//因为数据表Position字段和视图view.php文本框、单选框等等的标签名字完全一致,所以这里直接用了$arr,否则要用[’key’=>’value’]的形式。 } public function actionUpdate(){ //执行修改 $item=Yii::$app->request->post(); //接修改后的值 $query=new Position(); $sign=$query->rewrite($item); //封装好的方法 if($sign==1){ $this->redirect(['position/index']); }else{ throw new NotFoundHttpException(); } }
}
<?php //模型中
namespace app\models;
use yii\db\ActiveRecord;
use Yii;
use yii\data\Pagination;
class Position extends ActiveRecord{
... ...其他代码... ...
public function Rewrite($data){ //即使新值和旧值完全相同,依然可以提交并执行修改$res=Position::find()->where(['p_id'=>$data['p_id']])->one(); $res->p_name=$data['p_name']; $res->p_time=time(); $res->p_request=$data['p_request'][0];//数组需要处理成字符串 $sign=$res->save(); return $sign;
}
} - wangqy010 赞了回答
1、把showAll方法增加static声明,然后可以用Cate::showAll()调用
2、或者,先new,再用,既:$cate=new Cate(); $cate->showAll();