之前在播种期间没有被叫过

时间:2017-03-24 12:41:12

标签: cakephp cakephp-3.0

播种期间是否应该调用beforeSave()

当我浏览应用程序时(编辑和添加),我的模型表中的beforeSave()方法当前被调用,因为预期字段在表单提交后得到更新。

但是,当我在截断表后对数据库进行种子处理时,这些相同的字段保持默认值。

种子:

public function run()
{
    $data = [
            ['season_id'=>1, 'league_id'=>1, 'game_date'=>'2017-01-01', 'game_type_id'=>2, 'user_pt'=>1, 'opponent'=>'Vancouver', 'opponent_pt'=>2, 'is_decision'=>1, 'mins'=>60, 'ga'=>2, 'sog'=>24],
            ['season_id'=>1, 'league_id'=>1, 'game_date'=>'2017-01-02', 'game_type_id'=>2, 'user_pt'=>2, 'opponent'=>'Vancouver', 'opponent_pt'=>1, 'is_decision'=>1, 'mins'=>59, 'secs'=>45, 'ga'=>1, 'sog'=>27],
            ['season_id'=>1, 'league_id'=>1, 'game_date'=>'2017-01-03', 'game_type_id'=>2, 'user_pt'=>4, 'opponent'=>'Vancouver', 'opponent_pt'=>1, 'is_decision'=>1, 'mins'=>60, 'ga'=>1, 'sog'=>19],
            ['season_id'=>1, 'league_id'=>1, 'game_date'=>'2017-01-04', 'game_type_id'=>2, 'user_pt'=>5, 'opponent'=>'Edmonton', 'opponent_pt'=>2, 'is_decision'=>1, 'mins'=>58, 'secs'=>24, 'ga'=>1, 'sog'=>19],
            ['season_id'=>1, 'league_id'=>1, 'game_date'=>'2017-01-05', 'game_type_id'=>2, 'user_pt'=>3, 'opponent'=>'Calgary', 'opponent_pt'=>4, 'is_decision'=>1, 'mins'=>59, 'secs'=>10, 'ga'=>1, 'sog'=>19],
    ];

    $table = $this->table('games');
    $table->insert($data)->save();
}

BeforeSave

// In a table or behavior class
public function beforeSave($event, $entity, $options)
{
    // calculate record if part of decision
    if (isset($entity->is_decision) && $entity->is_decision == 1){
        //reset
        $entity->l = 0;
        $entity->w = 0;
        $entity->t = 0;
        $entity->otl = 0;
        $entity->is_shutout = 0;


        //calculate result of game
        if($entity->is_overtime == 0){
            //loss
            if($entity->opponent_pt > $entity->user_pt)
                $entity->l = 1;
            elseif($entity->opponent_pt < $entity->user_pt)
                $entity->w = 1;
            //tie
            elseif($entity->opponent_pt = $entity->user_pt)
                $entity->t = 1;
        }
        else{
            //ot loss
            if($entity->opponent_pt > $entity->user_pt)
                $entity->otl = 1;
        }

        //calculate shutout
        if($entity->opponent_pt == 0)
            $entity->is_shutout = 1;

        $entity->abs_game_time = 0;
        //calculate absolute game time
        if(isset($entity->mins) && is_numeric($entity->mins) && $entity->mins > 0)
        {

            if(isset($entity->secs) && is_numeric($entity->secs) && $entity->secs > 0){
                 $entity->abs_game_time = ($entity->mins * 60 + $entity->secs)/60;
            }
            else 
                $entity->abs_game_time = $entity->mins;
        }
    }
}

编辑#1

更新了代码段,但仍未执行

1 个答案:

答案 0 :(得分:0)

这不会起作用,因为种子不会使用Cake ORM。

your-complete-path/python setup.py install