原csdn地址https://blog.cs|

蜗牛使劲冲

园龄:7年6个月粉丝:3关注:10

📂yii2
🔖yii2-0
2017-10-25 10:43阅读: 5评论: 0推荐: 0

yii2.0(笔记)

路由介绍
http://b.zmnjz.com/index.php?r=site/view
走的是SiteController.php里面的actionView()

->where(['status' => Customer::STATUS_ACTIVE])

提示:在上面的代码中,Customer::STATUS_ACTIVE 是一个在 Customer 类里定义的常量。(译注:这种常量的值一般都是tinyint)相较于直接在代码中写死字符串或数字,使用一个更有意义的常量名称是一种更好的编程习惯。

// 更新现有客户记录

$customer = Customer::findOne($id); 
$customer->email = 'james@example.com'; 
$customer->save(); 

// 删除已有客户记录

$customer = Customer::findOne($id); 
$customer->delete(); 

// 删除多个年龄大于20,性别为男(Male)的客户记录
Customer::deleteAll('age > :age AND gender = :gender', [':age' => 20, ':gender' => 'M']);

//联表查询
return $this->hasOne(ArticleType::className(), [‘id’ => ‘type_id’]);
两种关联查询的例子

class Customer extends \yii\db\ActiveRecord
{
    public function getOrders()
    {
        // 客户和订单通过 Order.customer_id -> id 关联建立一对多关系
        return $this->hasMany(Order::className(), ['customer_id' => 'id']);
    }
}


class Order extends \yii\db\ActiveRecord
{
    // 订单和客户通过 Customer.id -> customer_id 关联建立一对一关系
    public function getCustomer()
    {
        return $this->hasOne(Customer::className(), ['id' => 'customer_id']);
    }
}
// 取得客户的订单(既然联表查询了就更好的获得数据了)
$customer = Customer::findOne(1);
$orders = $customer->orders; // $orders 是 Order 对象数组

reules[..]写在model里面,而且写在大model和search里的model不一样


联表得到相对应的数据在列表中(两种):

$majorName = Major::find()->select('name')->where(['id' => $this->major_id])->scalar();

return $majorName;
$major = Major::find()->where(['id' => $this->major_id])->one();

  return $major->name;

在调用外表的name ‘attribute’ => ‘user.real_name’,搜索能直接用,见zmnjz\/super-store/person/index


可以直接这样’attribute’ => ‘majorType.name’,(联表.字段)。弊端:不能排序,不能搜索
也可以我那种方法’value’=>…$model->getMajorType()


删除必须得用post传输
在删除按钮那加入这个属性’data-method’ => ‘post’,然后再在控制器的behaviors动作里加限制属性’delete’ => [‘post’],。

报错:(关于图片的)
{“name”:”Invalid Configuration”,
“message”:”The fileinfo PHP extension is not installed.”,
“code”:0,
意思是拓展没有安装,到php.ini里面开启扩展.
问题1
列表那边显示不出来图片了,数据库存储图片格式是 ,XXX,,XXX, 我需要先用afterFind()方法先把取出来的字符串转化为数组,然后通过展现出来
问题2
编辑界面图片一直上传报错需传一个字符串给他, 这个报错首先是rules那边我之前设定了pic字段必须得是string>255的,然后还要用beforeSave()这个监听时间把数组转化为字符串保存起来才行。
在组件中放的图片URL是数组格式哦

**上传先add变绿色再push, 不要忘了新加的文件要add
function的括号是上下对齐的,if的括号是跟着的。**

?????????????????????????
新yii2坤阳
1.科目分类-修改编辑前端页面,的radio选项用的static,要用function啊!!怎么用?
2.列表那用all()就不能出来为什么
3.在create_by用admin还是user
++++++++++++++++++++++++++++++
1.直接用static
2.all()直接就出来结果了,做分页要sql语句,所以我的dataProvider里面放的是sql语句
3.admin是后台,user是用户
4.那个控制器里面的findModel()方法(主要用于更新)放在最下面,这样能避免save什么的操作不变红,注释什么要写返回的东西加上用的model。每个具体的操作可以直接这样// 删除

???????????????
1.科目的前台的调用状态,怎么可以直接调用MajorType里面写的数组?
2.major表为什么字段major_type_id只能选择?
3.return $this->actionPreviousRedirect();这个设置的在“设为正常”那有问题

1.不能,$this->status都有它自己的对应,所以不行

2.自己百度表的设计

3.$this->remeberUrl()没有写在列表方法里面

+++++++++++++++
下面这个是查询数据,查出来的是数组,然后如果 $map为true的话转为为里面映射的关系就是那种下拉框需要的数组格式

public static function ( $map = false)
    {
        $model = self::find()->select(['id', 'name'])->where(['province_id' => $provinceId])->asArray()->all();
        if ($map) {
            return ArrayHelper::map($model, 'id', 'name');
        }
        return $model;
    }

关于面向对象和过程的一点认识从新增操作和更新操作可以看出
新增:$model = new Person();

更新: model= m o d e l = <script type="math/tex" id="MathJax-Element-1">model = </script>this->findModel($id);

protected function findModel($id)
    {
        $model = Person::findOne($id);
        if (!$model) {
            throw new NotFoundHttpException();
        }
        return $model;
    }

本文作者:蜗牛使劲冲

本文链接:https://www.cnblogs.com/warrenwt/p/18074693

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   蜗牛使劲冲  阅读(5)  评论(0编辑  收藏  举报  
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起