Вопрос

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.

Другие советы

While I'm not completely sure what you're asking for I suspect you are asking if there is some known method of passing an array of package names for packages with the same path. Assuming this is correct I have the following possible solution (and if it's not then please, as scragar requested, clarify what you are asking):

Having looked @ the file xpdo.class.php in the class xPDO & method addPackage() I see that it specifically verifies that the package name is a string & produces an error if it is not. Therefore this method, addPackage() certainly does NOT allow an array to be passed to it.

However, I suspect you may be remembering that, if you use addPackage() on a package/extra directory that has multiple xPDO classes & mapping files & a schema... that ALL of the database tables represented will be able to be instantiated & utilized to perform CRUD operations on.

As an example I have two packages (from separate extras) that have the following paths core/components/[packageName]/ (where the thing in the brackets would be the actual name of the particular package). In addition the model files are located in core/components/[packageName]/model/[packageName]/ as well as the subdirectory /mysql. In BOTH cases when I add the package [packageName] all classes are available to be instantiated, since I have not included/required any of the files then addPackage() appears to be including them & therefore making them available for use as xPDO objects by using the modx method newQuery()

(so in code thats:

$modx->newQuery(nameOfxPDOClassToBeInstantiated);

)

(NOTE: all of the necessary classes for my two packages where generated from custom DB tables utilizing the very handy tool provided by Bob Ray here and explained by him in a straightforward tutorial here).

Hope that helps.

As an afterthought, here's the xPDO documentation on addPackage: http://rtfm.modx.com/xpdo/2.x/class-reference/xpdo/xpdo.addpackage And here's the documentation on utilizing the objects... http://rtfm.modx.com/xpdo/2.x/getting-started/using-your-xpdo-model/retrieving-objects

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top