sjg20010414

sjg20010414

这家伙有点懒,还没写个性签名!

  • 财富值80
  • 威望值10
  • 总积分200

个人信息

  • 赞了教程
    yii2 支付宝支付教程
  • 回复了 的回答
    $arr1 = Array(
        0 => Array
        (
            'created_at' => '2016 - 08',
            'a' => 31900.00
        ),
        1 => Array
        (
            'created_at' => '2016 - 09',
            'a' => 45400.00
        ),
        2 => Array
        (
            'created_at' => '2016 - 10',
            'a' => 69489.00
        ));
    
    $arr2 = Array(
        0 => Array
        (
            'created_at' => '2016 - 08',
            'b' => 21900.00
        ),
        1 => Array
        (
            'created_at' => '2016 - 09',
            'b' => 25400.00
        ));
    $arr3 = [];
    foreach ($arr1 as $key => $val) {
        $arr3[$key]['created_at'] = $val['created_at'];
        $arr3[$key]['a'] = $val['a'];
        foreach ($arr2 as $v) {
            if ($arr3[$key]['created_at'] == $v['created_at']) {
                $arr3[$key]['b'] = $v['b'];
            } else {
                $arr3[$key]['b'] = '';
            }
        }
    }
    var_dump($arr3);
    

    结果 :

    array (size=3)
      0 => 
        array (size=3)
          'created_at' => string '2016 - 08' (length=9)
          'a' => float 31900
          'b' => string '' (length=0)
      1 => 
        array (size=3)
          'created_at' => string '2016 - 09' (length=9)
          'a' => float 45400
          'b' => float 25400
      2 => 
        array (size=3)
          'created_at' => string '2016 - 10' (length=9)
          'a' => float 69489
          'b' => string '' (length=0)
          
    
    array (size=3)
      'created_at' => string '2016 - 08' (length=9)
      'a' => float 31900
      'b' => string '' (length=0)
    

    ‘b' 不应该空的!应该修改一下,判断键是否已经设置,如下(我的变量与此不同):内层循环修改一下

    if (!array_key_exists('b', $d[$k1]))
       $d[$k1]['b'] = $d[$k1]['created_at'] == $v2['created_at'] ? $v2['b'] : '';
    
  • 以下代码可以满足你的要求,尽管这个要求有点奇怪

    $a = [
        ['created_at' => '2016-8', 'a' => 31900.00],
        ['created_at' => '2016-6', 'a' => 11900.00],
        ['created_at' => '2016-9', 'a' => 45400.00],
        ['created_at' => '2016-10', 'a' => 69489.00],
    ];
    
    $b = [
        ['created_at' => '2016-8', 'b' => 21900.00],
        ['created_at' => '2016-9', 'b' => 25400.00],
    ];
    $aa = [];
    $bb = [];
    foreach ($a as $item) {
        $aa[$item['created_at']] = $item;
    }
    foreach ($b as $item) {
        $bb[$item['created_at']] = $item;
    }
    $cc = array_merge_recursive($aa, $bb);
    $c = array_map($f = function ($v) {
        $v['created_at'] = $v['created_at'][0];
        return $v;
    }, $cc);
    var_dump(array_values($c));
    

    从常规索引数组到关联数组,没有想到好办法,只能循环了。
    很难判断算法在大数据量时的优缺点

  • 还是自己来了结此事,解决方案是,在用 $this->room 这个“关系属性”之前 unset($this->room),从而迫使用到 $this->room 的时候重新查询,具体情况看权威指南 http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#relational-data 中 Accessing Relational Data 一节:
    AR类定义了 getXyz() 关系,那么 $model->xyz 返回的是模型实例或者模型实例构成的数组,$model->getXyz() 返回的是 AQ 类的实例,后者代表着一个查询,前者是查询后的实例结果,如果$model->xyz连续用了两次,那么,实际执行一次查询,后一次肯定是用了“缓存”的结果。在我的例子中,算不上连续用了两次,但属于“很接近”,具体不清楚是如何“脏读”的(试验中快速连续时,反而后续结果都是正确的,隔2分钟左右才会出现问题)。

  • 很多问题,还是要靠自己一步步来诊断

    你先你的代码 $model1 = TblTemp::find()->one(); 一句下面加
    var_dump($model1->attributes);
    看看模型实例的属性类型是否符合预期,再来考虑是你的版本AR类被修改了还是ArrayHelper助手类被修改了(我的版本是 Yii2 version 2.0.8)

  • 采用事务或者自己设计锁(beforeSave lock,afterSave unlock)也没用,经过试验,发现似乎关联查询存在“迟滞”造成脏读:

    beforeSave内

    if (parent::beforeSave($insert)) {
      // many code ...
      $conflictResult = $this->isConflictExist();
      if(array_key_exists(self::SAVE_TYPE_ERROR, $conflictResult)) {
           return false;
      }
      // other code ...
      return true;
    } else {
      return false;
    }
    

    isConflictExist() 内

    // some code ...
    if($conflictType == self::CONFLICT_TYPE_ROOM && $this->room->devicenumber == 0 &&
       $this->room->category == Room::CATEGORY_CLASSROOM) {
       echo "\nclassroom! no conflict!!room_id = {$this->room_id}, room->id = {$this->room->id}";
       continue;
    } else {
      //many code ...
    }
    

    room关系(Labclass外键room_id)

        public function getRoom()
        {
            return $this->hasOne(Room::className(), ['id' => 'room_id']);
        }
    

    测试代码(else部分)

                $count = 10;
                while ($count--) {
                    $model1 = \frontend\models\Labclass::findOne($id);
                    $model2 = \frontend\models\Labclass::findOne($id);
                    $model1->room_id = $idLabRoom;
                    $model2->room_id = $idClassRoom;
                    //\frontend\models\Labclass::getDb()->transaction(function ($db) use ($model1) {
                        echo "\nmodel1: ".($model1->save() ? 1 : 0);
                    //});
                    //sleep(2);
                    //\frontend\models\Labclass::getDb()->transaction(function ($db) use ($model2) {
                        echo "\nmodel2: ".($model2->save() ? 1 : 0);
                    //});
                    $labclass = \frontend\models\Labclass::findOne($id);
                    //var_dump($labclass->attributes);
                    echo "\nUpdate 1--".$labclass->room_id."\t".$labclass->room->name;
                    sleep(rand(120, 180));
               }
    

    输出结果:
    false at 0.18354200 1507697371
    model1: 0
    classroom! no conflict!!room_id = 39, room->id = 39
    true at 0.19770900 1507697371
    model2: 1
    Update 1--39 长安教室(通用名)
    classroom! no conflict!!room_id = 36, room->id = 39
    true at 0.22769700 1507697516
    model1: 1
    classroom! no conflict!!room_id = 39, room->id = 39
    true at 0.25091500 1507697516
    model2: 1
    Update 1--36 多媒体机房二
    false at 0.29225100 1507697646
    model1: 0
    classroom! no conflict!!room_id = 39, room->id = 39
    true at 0.30175200 1507697646
    model2: 1
    Update 1--39 长安教室(通用名)
    false at 0.33835600 1507697811
    model1: 0
    classroom! no conflict!!room_id = 39, room->id = 39
    true at 0.35387100 1507697811
    model2: 1
    Update 1--39 长安教室(通用名)
    classroom! no conflict!!room_id = 36, room->id = 39
    true at 0.38394400 1507697931
    model1: 1
    classroom! no conflict!!room_id = 39, room->id = 39
    true at 0.40254100 1507697931
    model2: 1
    Update 1--36 多媒体机房二
    false at 0.44047400 1507698111
    model1: 0
    classroom! no conflict!!room_id = 39, room->id = 39
    true at 0.45133800 1507698111
    model2: 1
    Update 1--39 长安教室(通用名)
    classroom! no conflict!!room_id = 36, room->id = 39
    true at 0.48433500 1507698235
    model1: 1
    classroom! no conflict!!room_id = 39, room->id = 39
    true at 0.50019700 1507698235
    model2: 1
    Update 1--36 多媒体机房二
    false at 0.52970100 1507698386
    model1: 0
    classroom! no conflict!!room_id = 39, room->id = 39
    true at 0.53908300 1507698386
    model2: 1
    Update 1--39 长安教室(通用名)
    classroom! no conflict!!room_id = 36, room->id = 39
    true at 0.57195000 1507698543
    model1: 1
    classroom! no conflict!!room_id = 39, room->id = 39
    true at 0.58906600 1507698543
    model2: 1
    Update 1--36 多媒体机房二
    false at 0.62750300 1507698672
    model1: 0
    classroom! no conflict!!room_id = 39, room->id = 39
    true at 0.63890300 1507698672
    model2: 1
    Update 1--39 长安教室(通用名)

    对于$this这个模型实例,同一个位置的代码 $this->room_id 获得的值和 $this->room->id 获得的值居然有时候不同!!有没有谁了解背后的机制??

  • CREATE TABLE tbl_temp (
    id int(11) NOT NULL,
    name varchar(64) NOT NULL,
    value int(10) UNSIGNED NOT NULL,
    count int(10) UNSIGNED NOT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

                $model1 = \app\models\Temp::find()->asArray()->one();
                var_dump($model1);
                $model2 = \app\models\Temp::find()->one();
                $a = \yii\helpers\ArrayHelper::toArray($model2);
                var_dump($a);
                echo \yii\helpers\Json::encode($a);
    

    /home/x201/PhpstormProjects/myblog/views/site/index.php:25:
    array (size=4)
    'id' => string '1' (length=1)
    'name' => string 'aaa' (length=3)
    'value' => string '123' (length=3)
    'count' => string '456' (length=3)
    /home/x201/PhpstormProjects/myblog/views/site/index.php:28:
    array (size=4)
    'id' => int 1
    'name' => string 'aaa' (length=3)
    'value' => int 123
    'count' => int 456

    {"id":1,"name":"aaa","value":123,"count":456}

    没有遇到楼主的问题啊,楼主怎么试出来的?

  • 在你的controller中,添加成员变量 public $layout = 'main'; 整个controller都用这个布局文件,或者 controller 的某个 action 中 $this->layout = 'main'; 可以更细地让某个 视图用指定的布局文件
    如果你要 main 布局中嵌套 header 等部分,可以参考人家博客 http://blog.csdn.net/xihuangwutong/article/details/50177699

    个人一般直接做好几个布局文件,到时候用一下(一般也不会要很多的),很少用嵌套方式

  • 貌似楼主的写法不太对,afterSave返回的是void啊(beforeSave才有true/false),应该

    public function afterSave($insert, $changedAttributes)
    {
      if ($insert) {
        //是 插入 INSERT
      } else {
        //是 修改 UPDATE
        // 注意,如果你数据库类型是 int,那么传过来 $changedAttributes 中是字符串,也被认为是修改的属性了!!例如:
        // 周次 week 你数据库是 整数2,而 $changedAttributes['week'] 是字符串 '2',也会记录在 $changedAttributes数组中
      }
      return parent::afterSave($insert, $changedAttributes);  // 传递给父类
    }
    
见习主管 等级规则
200/500
资料完整度
30/100
用户活跃度
0/100

Ta的关注

0

Ta的粉丝

1

Ta的访客

5