yii2 数据库字段加一

Continue Read..

YII2中数据库操作DAO中,update操作的时候,手册上好像看不到+1的办法,只有AR模式有+1写法,其实在源代码里有写

看代码

public function update($table, $columns, $condition, &$params)
    {
        if (($tableSchema = $this->db->getTableSchema($table)) !== null) {
            $columnSchemas = $tableSchema->columns;
        } else {
            $columnSchemas = [];
        }

        $lines = [];
        foreach ($columns as $name => $value) {
            if ($value instanceof Expression) {
                $lines[] = $this->db->quoteColumnName($name) . '=' . $value->expression;
                foreach ($value->params as $n => $v) {
                    $params[$n] = $v;
                }
            } else {
                $phName = self::PARAM_PREFIX . count($params);
                $lines[] = $this->db->quoteColumnName($name) . '=' . $phName;
                $params[$phName] = !is_array($value) && isset($columnSchemas[$name]) ? $columnSchemas[$name]->dbTypecast($value) : $value;
            }
        }

        $sql = 'UPDATE ' . $this->db->quoteTableName($table) . ' SET ' . implode(', ', $lines);
        $where = $this->buildWhere($condition, $params);

        return $where === '' ? $sql : $sql . ' ' . $where;
    }

如果值是Expression的实例,会取这个实例的expression属性去拼接sql,

这个类在yii\db\Expression,使用起来很简单

$cardData = [
            'uid' => $uid,
            'cnt' => new Expression('cnt+1')
        ];
$db->createCommand()->update($tableCard, $cardData, "id=5")->execute();
同理,使用mysql原生函数等操作都可以用这个方式来完成。

声明:此文系舞林cuznwww.wulinlw.org)原创稿件,转载请保留版权

js 头像图片过滤器

Continue Read..
html:
<div class="user-icon user-icon-bg">
               <img :src="item.targetAnswerer.userInfo.avatarId | getThumbnail || 'images/mine/default-avatar1.png'">
               <div class="master" v-if="item.answerer.isAnswer == 1"></div>
           </div>

js:
Vue.filter("getThumbnail", function (url) {
            if (!url) {
                return "http://apr.qiniu.toon.mobi/FqnjLn8FzsD52rZatXzYKgwftS6A";
            }
            //静态服务器头像
            if (url.indexOf("http://img.icon.systoon.com") >= 0 || url.indexOf("http://static1.systoon.com") >= 0) {
                return url;
            }
            var w = 80, h = 80, q = 100;
            var path = url.substr(0, url.lastIndexOf("."));
            var mimeType = url.substring(url.lastIndexOf("."), url.length);
            return path + "_" + w + "_" + h + "_" + q + "_1" + mimeType;
        });

声明:此文系舞林cuznwww.wulinlw.org)原创稿件,转载请保留版权