Announcement

Collapse
No announcement yet.

Want to run query and calculate a field called "tot_hours" in the Task entity?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Want to run query and calculate a field called "tot_hours" in the Task entity?

    Code:
    $sql = "SELECT * FROM task";
    $res = $conn->query($sql);
    $len = $res->num_rows;

    if($len > 0){
    while($row = $res->fetch_array(MYSQLI_ASSOC){
    if($row['status'] == "Completed" && empty($row['tot_hours'])){
    $sql = "UPDATE task SET tot_hours = TIME_TO_SEC(TIMEDIFF(date_end,date_start))/3600 WHERE status = 'Completed'";
    }
    else{
    continue;
    }
    }
    }

    (Not familiar with PDO, but something like this in application/espo/core/utils/cron/job.php)

    protected function updateHours(){
    $pdo = $this->getEntityManager->getPDO();
    $query = "SELECT * FROM task";
    $sth = $pdo->prepare($query);
    $sth->execute();

    while($row = $sth->fetch(PDO::FETCH_ASSOC)){
    if($row['status'] == "Completed" && empty($row['tot_hours'])){
    $sec_query = "UPDATE task SET tot_hours = TIME_TO_SEC(TIMEDIFF(date_end,date_start)/3600 WHERE status = 'Completed' ";

    $fst = prepare($sec_query)->execute();
    else{
    continue;
    }
    }
    }

    Not sure where to execute the code? Need assistance on how and where to execute the code above to calculate the difference of datetime. Maybe run it as a cron job.
    Last edited by worldmiros; 12-15-2015, 04:48 PM.
Working...
X