zhoupenghui
- zhoupenghui 回复了 zhoupenghui 的回答
你看看这样行不行:
$goods= new Goods(); $attr=['name'=>'西门子','price'=>12,'brand'=>'TOTO']; $goods->setAttributes($attr); $goods->save();
@小程府 那个save()方法就可以判断是更新还是添加:
public function save($runValidation = true, $attributeNames = null) { if ($this->getIsNewRecord()) { return $this->insert($runValidation, $attributeNames); } else { return $this->update($runValidation, $attributeNames) !== false; } }
这是源码里的save方法,你可以去看一下文档
- zhoupenghui 回复了 zhoupenghui 的回答
这样写,你看行不行?
UserComment::deleteAll([ 'and', ['in','id', $id_list] ]);
$id_list
里面是数组那把and去掉吧,毕竟我也没有测试,不知道行不行哈
- zhoupenghui 回答了问题 请问 YII2 模糊查询怎么做?
可以这样:
var $username='张三'; var $email ='qq@qq.com'; var $mobile =18888888888; $result=User::find()->where([ 'status'=>1, ])->andwhere([ 'or', ['like','username',$username], ['like','email',$email], ['like','mobile',$mobile], ])->all();
你看看这样行不行:
$goods= new Goods(); $attr=['name'=>'西门子','price'=>12,'brand'=>'TOTO']; $goods->setAttributes($attr); $goods->save();
在表USER的模型中创建:
/*** relation: get Task. * @return Task */ public function getTask () { return $this->hasMany(Task::className(), ['task.u_id' => 'id']); }
然后:
$data=USER::find()->with([
'task'=>function(\yii\db\ActiveQuery $query){
$query->counn('u_id');
},
])->all();
你试一下,我没有测试可以这样:
先在表ht_user_comm 的模型中增加:public function getHtDiary() { return $this->hasMany(HtDiary::className(), ['id' => 'cid']); // hasMany可能为hasOne }
,然后:
$data = UserComm::find()->with([
'htDiary'=>function(\yii\db\ActiveQuery $query) use $id{$query->where[
'htDiary.id' => $id'
]},
])->orderBy([
'user_comm.count_praise' => SORT_DESC
])->asArray()->all();
我没有测试,你试一下上传时,先用js判断一下图片类型,再在模型中判断类型.模型中增加验证规则:
public function rules(){ $conf = Yii::$app->params['ware.comment']['image']; return array_merge(parent::rules(), [ [ 'pic', 'image', 'extensions' => 'jpg, png', 'mimeTypes' => 'image/jpeg, image/png', 'minSize' => 1000 * $conf['weight.limit'][0], 'maxSize' => 1000 * $conf['weight.limit'][1], 'tooSmall' => '{attribute}不能小于' . $conf['weight.limit'][0] . 'KB', 'tooBig' => '{attribute}不能大于' . $conf['weight.limit'][1] . 'KB', 'minWidth' => $conf['size.limit']['min'][0], 'minHeight' => $conf['size.limit']['min'][1], ], ]); }
这样写,你看行不行?
UserComment::deleteAll([ 'and', ['in','id', $id_list] ]);
$id_list
里面是数组- zhoupenghui 2016-04-29 已签到连续签到1天,获得了5个金钱