Just for clarification, it’s the other way round (related_object->get()->all) if you want to populate then access the all array.
The reason I was changing it to not auto-populate by default, was because of memory concerns, such as it auto-populating with 100,000 records when the developer most likely wont need that many, so forcing them to choose to get all or narrow it down is better for them. Still, if they don’t populate it, I’ll see if I can default it to populate with all, when you try and access a property. Can’t make any promises that I’ll be able to pull that off. Not having much luck so far 😕
UPDATE
Still not having luck with the auto-populate if not populated thing. Mainly because of the chaining, since when a __get kicks off from when a related object is accessed, I have no way of telling if the next part of the chain is ->all, which if its empty I should auto-populate, or if it’s a ->where or ->get() etc, which means an auto-populate is not needed since the developer is doing one.
I might have to just add a boolean setting that all related objects are auto-populated on first access if set to TRUE. If FALSE, you have to manually populate it yourself (better performance wise if you’ve got lots of records and don’t want them all loaded). I’ll be defaulting to FALSE.
______________________
Sorry but I wont be changing it to allow different names for the relationships. The reason it’s the singular is because accessing $a->book is accessing the first book object and $a->book->all is an array of all the book objects returned. This is how it should be for the DataMapper pattern, and keeping things similar to the usage shown at DataMapper.org.
// Get first author object
$a = new Author();
$a->get(1);
// Populate related object with a maximum of 10 objects
$a->book->get(10);
// Show properties of the first related book
echo $a->book->id . '
';
echo $a->book->name . '
';
echo $a->book->description . '
';
// Show properties of all related books
foreach ($a->book->all as $b)
{
echo $b->id . '
';
echo $b->name . '
';
echo $b->description . '
';
}
With the self referencing thing, I haven’t tried that yet but my first thought would be that you’d have to have a Model for employees who have a One to Many relationship with other employees (supervisor has multiple employees to supervise), and a Model for employees who have a One to One with other employees (employees have one supervisor).
This would require you have a joining table of “employees_employees”.
Supervisor Model
<?php
class Supervisor extends DataMapper
{
var $table = "employees";
var $has_many = array("underling" => "employees");
function Supervisor()
{
parent::DataMapper();
}
}
Underling Model
<?php
class Underling extends DataMapper
{
var $table = "employees";
var $has_one = array("supervisor" => "employees");
function Underling()
{
parent::DataMapper();
}
}
I haven’t tested that but I’m hoping it will work, lol 😊 Probably won’t since I suspect it will be looking for the same join key of (employee_id) and which you obviously can’t have two of.
UPDATE
Version 1.3 has been released!
View the Change Log to see what’s changed.
In short, the related objects has had an overhaul and automatic population of the related objects is now off by default (you now populate them in much the same way you do the normal objects) but can be turned back on easily if desired. Validation has been improved, with the ability to properly label fields for use in error messages and you can now validate non-Database Table fields.