//After inserting the record
DROP TRIGGER IF EXISTS `fjm`.`updatePhtoVideoCountOnInsert`//
CREATE TRIGGER `fjm`.`updatePhtoVideoCountOnInsert` AFTER INSERT ON `fjm`.`fjm_files`
FOR EACH ROW BEGIN
if NEW.modelName = 'listing' then
UPDATE fjm_listings b set photo_count = (SELECT COUNT(*) FROM fjm_files l WHERE l.attachment_id = NEW.attachment_id AND l.modelName = 'listing' GROUP BY l.attachment_id) WHERE b.id = NEW.attachment_id;
end if;
if NEW.modelName = 'listingvideos' then
UPDATE fjm_listings b set video_count = (SELECT COUNT(*) FROM fjm_files l WHERE l.attachment_id = NEW.attachment_id AND l.modelName = 'listingvideos' GROUP BY l.attachment_id) WHERE b.id = NEW.attachment_id;
end if;
END
//
//After update
DROP TRIGGER IF EXISTS `fjm`.`updatePhtoVideoCountOnUpdate`//
CREATE TRIGGER `fjm`.`updatePhtoVideoCountOnUpdate` AFTER UPDATE ON `fjm`.`fjm_files`
FOR EACH ROW BEGIN
if NEW.modelName = 'listing' then
UPDATE fjm_listings b set photo_count = (SELECT COUNT(*) FROM fjm_files l WHERE l.attachment_id = NEW.attachment_id AND l.modelName = 'listing' GROUP BY l.attachment_id) WHERE b.id = NEW.attachment_id;
end if;
if NEW.modelName = 'listingvideos' then
UPDATE fjm_listings b set video_count = (SELECT COUNT(*) FROM fjm_files l WHERE l.attachment_id = NEW.attachment_id AND l.modelName = 'listingvideos' GROUP BY l.attachment_id) WHERE b.id = NEW.attachment_id;
end if;
END
//
//After delete
DROP TRIGGER IF EXISTS `fjm`.`updatePhtoVideoCountOnDelete`//
CREATE TRIGGER `fjm`.`updatePhtoVideoCountOnDelete` AFTER DELETE ON `fjm`.`fjm_files`
FOR EACH ROW BEGIN
if OLD.modelName = 'listing' then
UPDATE fjm_listings b set photo_count = (SELECT COUNT(*) FROM fjm_files l WHERE l.attachment_id = OLD.attachment_id AND l.modelName = 'listing' GROUP BY l.attachment_id) WHERE b.id = OLD.attachment_id;
end if;
if OLD.modelName = 'listingvideos' then
UPDATE fjm_listings b set video_count = (SELECT COUNT(*) FROM fjm_files l WHERE l.attachment_id = OLD.attachment_id AND l.modelName = 'listingvideos' GROUP BY l.attachment_id) WHERE b.id = OLD.attachment_id;
end if;
END
//
DROP TRIGGER IF EXISTS `fjm`.`updatePhtoVideoCountOnInsert`//
CREATE TRIGGER `fjm`.`updatePhtoVideoCountOnInsert` AFTER INSERT ON `fjm`.`fjm_files`
FOR EACH ROW BEGIN
if NEW.modelName = 'listing' then
UPDATE fjm_listings b set photo_count = (SELECT COUNT(*) FROM fjm_files l WHERE l.attachment_id = NEW.attachment_id AND l.modelName = 'listing' GROUP BY l.attachment_id) WHERE b.id = NEW.attachment_id;
end if;
if NEW.modelName = 'listingvideos' then
UPDATE fjm_listings b set video_count = (SELECT COUNT(*) FROM fjm_files l WHERE l.attachment_id = NEW.attachment_id AND l.modelName = 'listingvideos' GROUP BY l.attachment_id) WHERE b.id = NEW.attachment_id;
end if;
END
//
//After update
DROP TRIGGER IF EXISTS `fjm`.`updatePhtoVideoCountOnUpdate`//
CREATE TRIGGER `fjm`.`updatePhtoVideoCountOnUpdate` AFTER UPDATE ON `fjm`.`fjm_files`
FOR EACH ROW BEGIN
if NEW.modelName = 'listing' then
UPDATE fjm_listings b set photo_count = (SELECT COUNT(*) FROM fjm_files l WHERE l.attachment_id = NEW.attachment_id AND l.modelName = 'listing' GROUP BY l.attachment_id) WHERE b.id = NEW.attachment_id;
end if;
if NEW.modelName = 'listingvideos' then
UPDATE fjm_listings b set video_count = (SELECT COUNT(*) FROM fjm_files l WHERE l.attachment_id = NEW.attachment_id AND l.modelName = 'listingvideos' GROUP BY l.attachment_id) WHERE b.id = NEW.attachment_id;
end if;
END
//
//After delete
DROP TRIGGER IF EXISTS `fjm`.`updatePhtoVideoCountOnDelete`//
CREATE TRIGGER `fjm`.`updatePhtoVideoCountOnDelete` AFTER DELETE ON `fjm`.`fjm_files`
FOR EACH ROW BEGIN
if OLD.modelName = 'listing' then
UPDATE fjm_listings b set photo_count = (SELECT COUNT(*) FROM fjm_files l WHERE l.attachment_id = OLD.attachment_id AND l.modelName = 'listing' GROUP BY l.attachment_id) WHERE b.id = OLD.attachment_id;
end if;
if OLD.modelName = 'listingvideos' then
UPDATE fjm_listings b set video_count = (SELECT COUNT(*) FROM fjm_files l WHERE l.attachment_id = OLD.attachment_id AND l.modelName = 'listingvideos' GROUP BY l.attachment_id) WHERE b.id = OLD.attachment_id;
end if;
END
//
No comments:
Post a Comment