I once spent over an hour reverse engineering the xPDO constructor to figure out how to load base packages upon instantiation.
Unfortunately, I have lost that little snippet of code! And I'm left with this.
$this->database = new xPDO(
"mysql:host=" . $this->config->item('hostname', XPDO) .
";dbname=" . $this->config->item('database', XPDO),
$this->config->item('username', XPDO),
$this->config->item('password', XPDO),
$this->config->item('dbprefix', XPDO)
);
// This is line I would like to pass to the constructor.
$this->database->addPackage('packageName', $this->config->item('core_path') . "components/package/model/", '_packagePrefix');
I cannot find this anywhere in the documentation.
EDIT With xPDO, you have to specifically add packages that are not loaded by default. And by default, xPDO doesn not load any packages on instantiation.
However, I did once spend a considerable amount of time, deconstructing the constructor of xPDO and found that there is an optional parameter that allows you to define an array of packages that will be loaded on instantiation.
My problem is that I cannot remember how to do this.
You can load base packages passing the right options to the xPDO constructor. This is the constructor definition:
$xpdo= new xPDO($dsn, $username= '', $password= '', $options= array(), $driverOptions= null)
The options array support many different configurations, the one you are looking for is xPDO::OPT_BASE_PACKAGES:
xPDO::OPT_BASE_PACKAGES — A comma-separated string of package names/paths (separated by a colon) to be loaded upon instantiation.
Basically, you can do what you want modifying your code in this way:
$this->database = new xPDO(
"mysql:host=" . $this->config->item('hostname', XPDO) .
";dbname=" . $this->config->item('database', XPDO),
$this->config->item('username', XPDO),
$this->config->item('password', XPDO),
array(xPDO::OPT_BASE_PACKAGES => "package1:path1;prefix, package2:path2, ...")
);
Here's a link to the documentation where you can find further details about the options array: http://rtfm.modx.com/xpdo/2.x/getting-started/fundamentals/xpdo,-the-class/the-xpdo-constructor
EDIT
The format of the string is as follow:
"package_name:absolute_path;prefix"
prefix is optional. I have updated the code above with this format string.