2

I have ZF2 module, and simultaneously I use Propel genereted models hosted in root-directory/generated-classes. Can I make them share selfsame namespace - like Bookstore or so?

From Zend\Loader\StandardAutoloader I see:

public function registerNamespace($namespace, $directory)
{
    $namespace = rtrim($namespace, self::NS_SEPARATOR) . self::NS_SEPARATOR;
    $this->namespaces[$namespace] = $this->normalizeDirectory($directory);
    return $this;
}

so if I provide two directories in Module.php, the last will prevail.

There is also:

public function setFallbackAutoloader($flag)
{
    $this->fallbackAutoloaderFlag = (bool) $flag;
    return $this;
}

Can I resort to it and how do I leverage this option? Any other (better) options?

edigu
  • 9,878
  • 5
  • 57
  • 80
iVenGO
  • 384
  • 1
  • 4
  • 17

1 Answers1

3

I wouldn't put my models directly in /your-application/root. This will be against ZF2's recommended directory scaffolding. Instead of that, I'd create a /FooModule/src/FooModule/Model directory and put all of my models inside this folder using namespace FooModule\Model namespace definition in model class.

Another detail is; trying to pointing two different directories for same namespace is absolutely bad idea. This will be against PSR-4 Autoloading Standard and lot of open source libraries & frameworks including Zend Framework 2 which heavily depends on this standard.

I would look at the problem from a different angle. Just ask: Why I need to point one of my namespaces to the two different directories?

I think actually you mean Domain Entities by "Propel generated models". If this is correct (i mean Bookstore) is an Entity rather than a Model. You may also want to read this great answer.

So, you can try to create an Entity namespace in your Application (or whatever) ZF2 module and write your Entity classes under a sub-namespace inside that. This is perfectly valid. For example:

Application\src\Entity\Bookstore.php - namespace is Application\Entity
Application\src\Entity\Book.php - namespace is Application\Entity
Application\src\Entity\Author.php - namespace is Application\Entity

Or this is also valid scenario too (Bookstore is a module):

Bookstore\src\Entity\Book.php - namespace is Bookstore\Entity
Bookstore\src\Entity\Author.php - namespace is Bookstore\Entity

In both example scenarios, Book.php and Author.php are your auto-generated domain entities and they shares same namespace while not conflicting ZF2 or PSR-4 autoloading mechanisms.

Community
  • 1
  • 1
edigu
  • 9,878
  • 5
  • 57
  • 80
  • **thnx!** I am using Propel for a very few time, so after failing to find any config options to change destination folder for output, I decided kind of "symlink" generated-classes into ZF2 directory scaffolding via autoloading "black magic". Which was erroneus by far. Fortunately, there is awesome `[--output-dir="..."]` option supported by command line `propel model:build` – iVenGO Sep 15 '14 at 11:22
  • btw, ZF2 supports multiple namespaces for a module, thus - should'nt it be Application/src/**Application**/Entity/Book.php and Bookstore/src/**Bookstore**/Entity/Book.php respectively? – iVenGO Sep 15 '14 at 11:27