I have asked my question on Stackoverflow but have not really gotten any answers so I am trying over here now.
I have the MySQL query that is ran in a PHP script below.
What it does is Inserts a new Project Task record if one does not exist with that ID number.
If a record exist with the ID number, then it UPDATE the fields that it should update.
The problem I have is I need it to only update the date_modified
column when one of these columns value changes... name
, description
, status
, type
, or priority
public function addOrUpdateTaskRecord($taskId, $projectId, $name, $description, $status, $priority, $type, $date_entered, $date_modified, $sort_order, $heading){
$sql = "
INSERT INTO
$this->tasksDbTableName(task_id, project_id, name, description, status, priority, type, date_entered, date_modified, sort_order, heading)
VALUES
('$taskId', '$projectId', '$name', '$description', '$status', '$priority', '$type', UTC_TIMESTAMP(), UTC_TIMESTAMP(), '$sort_order', '$heading')
ON DUPLICATE KEY UPDATE
name='$name',
description='$description',
status='$status',
priority='$priority',
type='$type',
date_modified=UTC_TIMESTAMP(),
sort_order='$sort_order',
heading='$heading'";
return $this->db->query($sql);
}
Another user on here Gordon Linoff had showed me that I might be able to change this date_modified=UTC_TIMESTAMP(),
and use this in place...
date_modified = (case when name <> values(name) or
description <> values(description) or
status <> values(status) or
type <> values(type) or
priority <> values(priority)
then UTC_TIMESTAMP()
else date_modified
end)
This looked really promising and produces no errors in MySQL. It still inserts new record when needed and updates when it is an existing record, but now it always updates the date_modified
field even when other fields have not changed in my CASE statement.
I could really use some help in getting such advanced functionality working or an alternative method?
Any help from the Database experts out there?
My Latest code....
public function addOrUpdateTaskRecord($taskId, $projectId, $name, $description, $status, $priority, $type, $date_entered, $date_modified, $sort_order, $heading){
$sql = "
INSERT INTO
$this->tasksDbTableName(task_id, project_id, name, description, status, priority, type, date_entered, date_modified, sort_order, heading)
VALUES
('$taskId', '$projectId', '$name', '$description', '$status', '$priority', '$type', UTC_TIMESTAMP(), UTC_TIMESTAMP(), '$sort_order', '$heading')
ON DUPLICATE KEY UPDATE
name='$name',
description='$description',
status='$status',
priority='$priority',
type='$type',
date_modified = (case when name <> values(name) or
description <> values(description) or
status <> values(status) or
type <> values(type) or
priority <> values(priority)
then UTC_TIMESTAMP()
else date_modified
end),
sort_order='$sort_order',
heading='$heading'";
return $this->db->query($sql);
}
My Database table looks like this...
CREATE TABLE IF NOT EXISTS apoll_web_projects_tasks (
task_id bigint(20) NOT NULL default '0',
project_id varchar(70) NOT NULL default '0',
name varchar(255) NOT NULL,
description varchar(36) default NULL,
status varchar(20) DEFAULT NULL,
priority varchar(20) DEFAULT NULL,
type varchar(20) DEFAULT NULL,
date_entered datetime DEFAULT NULL,
date_modified datetime DEFAULT NULL,
sort_order int(12) DEFAULT NULL,
heading int(2) NOT NULL default '0',
PRIMARY KEY (task_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
date_modified
?