Validation & stuff
The validation within a domain object would be restricted only to the business rules that it encompasses. For example, when calling $student->isValid()
, the domain object would see, if the name and surname has been set and whether they do not contain number in strange places (like, "Frederick William III" is perfectly valid, while improbable, name).
That does not include data integrity checks (like checking whether student's email address has not already been registered, thus violating UNIQUE
constraint). The data integrity is verified by mappers. If you are using PDO for persistence abstraction, then in case of integrity error it will throw an exception, which you can handle internally in the mapper and set an error state on the domain object which you were attempting to store.
Identification of self
The "identify themselves" is kinda murky description. I would assume that it would refer to such operation as comparing the objects (like: $studentA === $studentB
to verify whether it's the same student).
The reference thing
recognize which [of their] references indicate aggregation and which ones indicate association
This bit talks about interaction between domain objects. Like calling $course->addStudent($student);
which would define some sort of relationship between a Course
and a Student
instance. I am bit rust on the UML front, but I think this would indicate that:
- course contains an aggregation of students
- student is associated to a course
An simplified and kinda clumsy example, which illustrates all the issues that you were having would probably looks like this:
$student = new Student;
$studentMapper = new StudentMapper($pdo);
$student->setId(42);
if (!$studentMapper->fetch($student)) {
// error , missing student
return;
}
// found the one with ID 42
$course = new Course;
$courseMapper = new CourseMapper($pdo);
$course->setId(31);
if (!courseMapper->fetch($course)) {
// missing course
return;
}
$course->addStudent($student);
if ($course->isValid() && $student->isValid()) {
// check if there are free spots in course
// and student has prerequisites
$courseMapper->store($course);
$studentMapper->store($student);
}
Whether there is course-student double booking gets checked, when mappers try to store the data.
Note: you also might notice that this starts to smell like a case for transactions. That's one of the reasons why in large scale OOP codebases it is common to use Units of Work.
Note nr.2: keep in mind that "domain object" and "domain object collection" are two separate things. Same goes for persistence of these entities. Kinda like described in this old post.