6

I am using MSSQL and want to implement the WITH function (as per Using ZF2, create a WITH statement?). To do so, I am extending the \Zend\Db\Sql\Select class adding the properties and methods required to add the WITH function. How do I now tell my application to use this Select class instead of the Zend one?

One approach is to specify an autoload in my composer.json file:

"autoload": {
        "psr-4": {
            "Zend\\Db\\Sql\\": "vendor/rpk/Rpk/Zend/Db/Sql"
        }
}

which will look in my vendor folder for any Sql namespace stuff before looking in the zend folder, but this requires me to copy the entirety of the zend select class into my select class - this is undesirable as my class won't benefit from future patches to the zend branch.

Community
  • 1
  • 1
Richard Parnaby-King
  • 14,703
  • 11
  • 69
  • 129
  • Why do you even need to force it to load in that namespace? manually instantiate your own class to use it.. – Andrew May 05 '17 at 08:47
  • I use TableGateways in my models. These instantiate `Zend\Db\Sql\Sql`, which in turn instantiate `Zend\Db\Sql\Select`. In order to get my select class used, I would have to create a new table gateway class and a new sql class, replacing all instantiations of `Sql` and `Select` as I go. I am hoping there is a more elegant solution - perhaps a handful of lines in the module bootstrap class or global config file? – Richard Parnaby-King May 05 '17 at 11:28

1 Answers1

0

Try using a class_alias() directive in your bootstrap script to alias your Select replacement class (with namespace) to the full namespace of ZF2's Select class -

class_alias("Your\\Namespace\\Select", "Zend\\Db\\Sql\\Select");

Unfortunately this is just a more targeted version of the autoload definition you give above, unfortunately you would still have to reimplement your version of the Select class to include all the current code in the Zend Select class, as as far as I am aware there is not functionality to extend one class from another and then alias the extending class over the original.

If you use something like BetterReflection to dynamically load and extract the code of the 'Zend' select class, you could rename the class in your code, (probably cache it somewhere, so this admittedly expensive operation does not need to be performed constantly) and extend your select class from that. Aliasing that extension to your original class name would then not be a problem.

Disclaimer: I know some of the Roave lot, in particular all of those that are listed as authors on BetterReflection. They are decent, helpful guys, if somewhat curmudgeonly.

Benjamin
  • 1,221
  • 11
  • 28