Welcome Guest, Not a member yet? Register   Sign In
Model(s) for one business logic with few database tables
#1

I use CodeIgniter 4 and I really like its Model class with all CRUD helpers.

My problem is I don't know how to design model(s) for business logic that uses multiple database tables. Let's say I want to handle meetings - each having a list of subjects to discuss. My database tables look like this (simplified):
Code:
CREATE TABLE meetings (
  id int(11) NOT NULL,
  date date NOT NULL,
  moderator varchar(128),
)
CREATE TABLE meeting_subjects (
  id int(11) NOT NULL,
  meeting_id int(11) NOT NULL,
  order int(11) NOT NULL,
  subject varchar(128) NOT NULL,
)

This would be very convenient to have two models like this:
PHP Code:
class MeetingsModel extends Model
{
    protected $table 'meetings';
    protected $primaryKey 'id';
    protected $allowedFields = [ 'date''moderator' ];

    public function insertMeeting($data$subjects)
    {
        $subjectsModel = new MeetingSubjectsModel();

        /* Simplified insert code */
        $this->insert($data);
        /* TODO: set "meeting_id" */
        $subjectsModel->insertBatch($subjects);
    }
}
class 
MeetingSubjectsModel extends Model
{
    protected $table 'meeting_subjects';
    protected $primaryKey 'id';
    protected $allowedFields = [ 'meeting_id''order''subject' ];


As far as I understand some people find above an anemic domain model anti-pattern and they claim it should not be used. Single business logic (e.g. handling meetings) should be implemented with a single model.


Do you have any suggestions on how to handle such cases in a clean way and without re-implementing code that Model already provices?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB