简体   繁体   English

CakePHP / PHP和MySQL数据库架构的重复事件插件

[英]Recurring Events Plugin for CakePHP/PHP and MySQL database schema

Introduction 介绍

Hello, I have spent a good bit of time researching to find the appropriate solution for recurring events on an upcoming project. 您好,我花了很多时间进行研究,以找到适当的解决方案以应对即将发生的项目中的重复事件。 I have worked with events and recurring events before and I have never found or developed a solution I have liked. 我以前曾处理过事件和重复发生的事件,但从未找到或开发出自己喜欢的解决方案。 So I am thinking of building a Plugin for CakePHP to handle recurrence. 因此,我正在考虑为CakePHP构建一个插件来处理重复发生。

I have two use cases that I would like to use this plugin/classes for: 我有两个用例,我想将此插件/类用于:

  1. Recurring Events - Calendar or List View 周期性事件-日历或列表视图
  2. Recurring Payments - Sometimes I don't want to use the gateways recurring payments or I want to do notifications of upcoming payments, etc 重复付款-有时我不想使用网关重复付款,或者我想通知即将付款等

I am sure there are many other use cases but those are the two I encounter the most frequently, actually the database schema and basic source code should be similar if not completely reusable between the various use cases, anyways. 我敢肯定还有许多其他用例,但那是我最常遇到的两个用例,实际上,即使在各种用例之间不能完全重用,数据库模式和基本源代码也应该是相似的。

So what I need help with is deciding the appropriate database schema to use for recurring events. 所以我需要帮助的是确定适当的数据库架构以用于重复发生的事件。 I have two ideas and I think each have their pros and cons, but need to settle on the best overall approach for flexibility, maintainability & performance. 我有两个想法,我想每个都有各自的优点和缺点,但是需要选择最佳的总体方法来实现灵活性,可维护性和性能。

So in either solution I need to be able to add, edit, delete and list the events. 因此,在两种解决方案中,我都需要能够添加,编辑,删除和列出事件。 I would like to be able to edit a recurring event with the option to edit or delete all days the event occurs or a specific day(s). 我希望能够通过选择编辑或删除事件发生的所有天数或特定日期的选项来编辑重复发生的事件。 I would like to have recurrence rules/options similar google calendar. 我想要类似Google日历的重复规则/选项。 Options like daily, weekly, monthy, yearly, etc. 每天,每周,每月,每年等选项。

First Idea 第一个想法

One table called events to store the recurring events. 一个表称为事件,用于存储重复发生的事件。 In this approach I would need to parse the recurrence option and essentially loop and create an individaul event based on the recurrence option. 在这种方法中,我将需要解析重现选项并本质上循环并基于重现选项创建一个个性化事件。 So for example if I want to create an event called "Weekly Meeting" with start date of Jan 1st 2012 and end date of Dec 31st 2012 that occurs every Monday, I would create 52 records in the events table. 因此,例如,如果我想创建一个名为“每周会议”的事件,该事件的开始日期为2012年1月1日,结束日期为2012年12月31日,发生在每个星期一,那么我将在事件表中创建52条记录。 I think this approach would be harder to manage editing and deleting and would take longer to save the event but listing the data should be simple as just querying a date range. 我认为这种方法将更难以管理编辑和删除,并且需要更长的时间保存事件,但是列出数据应该很简单,就像查询日期范围一样。 It might be a nightmare if you have to change the recurrence for that event. 如果您必须更改该事件的重复发生,这可能是一场噩梦。

CREATE TABLE `events` (                                                               
      `id` int(10) unsigned NOT NULL AUTO_INCREMENT,                                      
      `parent_id` int(10) unsigned NOT NULL,                                              
      `start_date` datetime NOT NULL,                                                     
      `end_date` datetime NOT NULL,                                                       
      `title` varchar(150) CHARACTER SET latin1 NOT NULL,                                 
      `description` text CHARACTER SET latin1,                                            
      `created` datetime NOT NULL,                                                        
      `modified` datetime NOT NULL,                                                       
      PRIMARY KEY (`id`)                                                                  
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8

Second Idea 第二个想法

Have multiple tables to handle the recurrence. 有多个表来处理重复。 This would eliminate the editing and deleting issues and saving should be faster too, but I would think querying the data to get the events would be complex (thinking the mysql queries will be complex) and slower. 这样可以消除编辑和删除问题,并且保存也应该更快,但是我认为查询数据以获取事件会很复杂(认为mysql查询会很复杂)并且速度较慢。 I should mention that this system is going to be reading far more than it is writing events. 我应该提到,该系统将比编写事件更多地读取内容。 But I want a general solution that can be reused. 但是我想要一个可以重用的通用解决方案。 I can also use caching to improve reading so it is not a huge concern. 我还可以使用缓存来改善读取,因此这不是一个大问题。 Using the example from above instead of 52 entries we would have only one in the events table. 使用上面的示例,而不是52个条目,事件表中将只有一个。

Not sure about the exact schema any ideas? 不确定确切的架构有什么想法吗?

Thanks in advance for your help and ideas! 预先感谢您的帮助和想法! Want to get a sense of how others handle recurring events. 想要了解其他人如何处理重复发生的事件。

CREATE TABLE `events` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `parent_id` int(10) unsigned NOT NULL,
    `title` varchar(150) CHARACTER SET latin1 NOT NULL,
    `description` text CHARACTER SET latin1,
    /** Some fields about how the recurring is applied  **/
   `created` datetime NOT NULL,
   `modified` datetime NOT NULL,
   PRIMARY KEY (`id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8

CREATE TABLE `event_occurrences` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `event_id` int(10) unsigned NOT NULL,
    `date` DATE NOT NULL,
   PRIMARY KEY (`id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8

If the recurring details, start or end fields aren't changed there is no need to mess with the event_occurrences. 如果重复的详细信息,开始或结束字段未更改,则无需弄乱event_occurrences。 When modifying any of those details DELETE FROM event_occurrences WHERE event_id=? 修改任何这些详细信息时,请DELETE FROM event_occurrences WHERE event_id=? and then have a method that will generate the event_occurrences for a given event id. 然后有一个方法将为给定的事件ID生成event_occurrences。 The same method could be used for creating and modifying. 可以使用相同的方法来创建和修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM