ZF2 Directory/Namespace Model Object Location - zend-framework2

In the zf2 database and models tutorial a path such as this leads to a directory containing my model classes:
module/Album/src/Album/Model
I have created two more classes at:
module/Album/src/Album/Model/AlbumRelatedClass.php
module/Album/src/Album/Model/AlbumRelatedTable.php
I would like to put these classes at
module/Album/src/AlbumRelated/Model/
I have created duplicates of the model classes there for the purposes of this test, unfortunately, my module config for Album tells me that it cannot find the classes at the AlbumRelated location. I have tried changing the namespaces in the AlbumRelated location to AlbumRelated\Model and directly referring to the class location (\Album\Model\AlbumRelated()) without success.
Does anyone know how I can do this? If I shouldn't be doing this, can somebody explain why? I'm also interested to know why the folder structure is Album/Model/Album (it seems redundant, and I wasn't clear on the explanation in the tutorial).
Any help given would be great, thanks :)

The short answer is that it can't find the class because you probably didn't set up the autoloader config to know about a new "AlbumRelated" namespace. Look at what happens in \Album\Module::getAutoloaderConfig(). You probably want to add a key/value pair to namespaces like 'AlbumRelated' => __DIR__ . '/src/AlbumRelated'. That will tell the autoloader where to look for classes under the AlbumRelated namespace.
That said,
If I shouldn't be doing this, can somebody explain why?
You probably shouldn't be doing it that way. You've got a top-level namespace called \Album. Then you have \Album\Model namespace under it. Any models related to Albums ought to live there. Imagine you had an entity in your system for Record Labels, so you could associate an Album with the label that released it. The ZF2 expectation is that you'd create a class called \Album\Model\RecordLabel that lived in module/Album/src/Album/Model/RecordLabel.php. As long as we assume that RecordLabels are primarily related to albums, that all makes perfect sense.
Of course, you could stick RecordLabel in it's own module as well, if you had a lot of workflow around managing them.
I'm also interested to know why the folder structure is Album/Model/Album
I assume you mean Album/src/Album. That redundancy is to keep a nice clean PSR-0 setup. So, the first Album is identifying the module, and the second is the top-level of the Album namespace. This provides maximum flexibility, but isn't a hard and fast rule. See Rob Allen's recent blog post about alternative module layouts in zf2 for an exploration.

Related

ist there something like __FILE__ in coffeescript at 'compiletime'?

The question:
How do I get somthing like that in coffeescript
modules_list[some_calculation(__FILE__)]=a_local_class.new
I have a module manager, that handles all my coffeescripts (js) at runtime - i.e. while in browser, that means:
I have
many modules, not all are in all situations loaded 'statically'
only some have dependencies
module manager resolves things like init and dynamic reload (ajax)
and dynamic init
A module looks like this in prinzip:
class Book
constructor: ->
...
init: =>
$$$.get_module('BookReader') #<- ajax loaded if not already
later_on: =>
$$$.get_module('LetterCounter').count_letters(#) #<- ajax loaded if not already
...
#$$$.modules_list['Book'] = ->
new Book
this all works (very) satisfying.
But I have a rendunancy in my Logic, because a 'module', has a module name, a class name and - thats the point - a file name
the class name is - thats clear - not a problem, I could name all 'Foo'. Its only not nice to do that.
But modulename ( modules_list['Book'] ) and the coffee- (js-) file name are redundant in sence of Rails CoC.
Any ideas how to get
#$$$.modules_list[some_calc(__FILE__)] = -> new Book
i was there, and there may be a solution for me, but if, I dont understand it.
thanks in advance
ps.: for those who want to know "why I do this":
I have 3 completely different apps for 3 different customers, I am far away to say what is the base for all, 2 apps a realy big with a huge amount of js, that is not always needed so I want to reload it dynamicaly. I "help" asset pipeline a bit with putting all files (staticaly) used in a single one without the ones I was working on the last 20 mins (dynamicaly) for better debugging, and I get rid of all (down-)load ordering problems
There is no need for __FILE__or something like that, in that case. I see that now. It would be the wrong direction of naming.
Its the same 'problem' that you have with Rails it self. If you have
module Base
class Fun
your filename should be base/fun.rb not vice versa!
The Filename resolves out of "its content" not the other direction.

How to add custom fields in ZFC-user module for zend 2 without doctrine?

I have tried using creating a different module and attaching the ZfcUser\Form\Register over init method. But it wasn't working.
I want to add few custom fields, with changing any thing in the vendor dir, as is it not a good practice. I also tried using user_entity_class ,creating a custom 'User' class, but it was creating some route issue in other modules, with zfc-user , I'm also using zfc-admin and zfc-adminuser, the error was coming in zfc-adminuser, Couldn't found the class was the error.
Thanks in advance.
Well there are some issue regarding the overriding of the module ZFC-User, But still you can overwrite it manually.
One way I have used is a bit old fashioned but working. What I have done is I have copied complete module the to module folder. Then pointing the form towards to my module where the changes are required, rest all are pointed to default.With this you can update your module. Make sure you point the user_entity_class to your module something like this:
'user_entity_class' => 'MyZfcUser\Entity\User',
you can find this in config\autoload\zfcuser.global.php

Including an external application in ZF2

I'm trying to use phpBB3 (forum app) along with ZF2. For that, I have to include a file from the phpBB3. In theory this is as simple as:
include('/path/to/phpbb3/common.php');
$user->session_begin(); //$user is defined in common.php file
In common.php a lot of globals are defined, and after that are required some files which are using those globals.
In ZF2 simply including the common.php would not work, because the scope of the globals will not span over the required files, so I tried a little trick:
//in Application/Forum/Service
public function callForumAPI(){
$zf_dir = getcwd();
chdir('/var/www/html/phpBB3');
include('common.php');
$user->session_begin();
chdir($zf_dir);
}
Neither in this case the scope of the global variables didn't span over the required files, so all the globals where NULL in those files.
How could I solve this issue?
I consider 2 main problems:
1. Loading resources
I dont know if you changed the code of phpBB3, since if you dont, your problem is other.
Phpbb3, as many systems, doesnt let you access directly to any file, you have to go through index.php. As you can see in common.php
if (!defined('IN_PHPBB'))
{
exit;
}
IN_PHPBB is defined in index.php, so you can simply use
Also, common.php and other files, makes use of $phpbb_root_path, that is defined in index.php.
So, at least, when you are going to include common.php you need
$zf_dir = getcwd();
chdir('/var/www/html/phpBB3');
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
include('common.php');
...
chdir($zf_dir);
probably there are some other things you have to take care about.
2. Variable scopes
Also, consider than in PHP, like in almost every language, a variable declared inside a function, is considered local, and will be undefined outside that function. So for sure, if you do that inside callForumAPI(), you wont have any variable outside, and moreover, depending on where you are doing that includes...it could be actually inside a function, no matter you can notice it or not, since ZF2 is a framenwork with a complex, non-obvius architecture.
So, what i recomend, as soon as you load the file, is to use the ZF2 service manager to store all the variables and object than you would use in your application. This is a good measure even if you didnt need it,since this way you can have everything integrated as much as possible, it is important to minimize and localize access to phpbb3, since it is not meant to be a library, maintenance could be tricky, so if everyhing is in the same file, and then you create your own internal api through the service manager, it will more encapsulated and nicer. I assume you already know how to do this, if you dont, just let me know.
try this, and tell me if its enough or we need more research

Zend Framework 2 Hierarchy Semantics?

I've been searching and trying to learn for ages but it just doesn't stick. There seems to be only 1 tutorial on Zend 2, and it's not very smart. Here we have a sample structure (and the tutorial proceeds with this application) http://framework.zend.com/manual/2.0/en/user-guide/modules.html :
zf2-tutorial/
/module
/Album
/config
/src
/Album
/Controller
/Form
/Model
/view
/album
/album
That's not cool - how do I know which album is which? In Zend 1 it made a lot of sense - you have modules, then you have controllers, and those controllers have actions.
Now I have a module called Album. is the src/Album/... a "single controller"? Would:
zend1application/modules/album/albumcontroller.php map to zend2application/modules/album/src/album/controller/albumcontroller.php? In that case, why are there 3 albums now? Like what happens if I change albumcontroller.php to indexcontroller.php? (I have been testing but there are so many changes it never sticks - I finally thought I should ask someone and if I know why I'll remember.
If I look at it a different way it seems the Album in module/Album and module/Album/src/Album should be the same - then why would we have it twice? Doesn't that just make room for error? Like why have a src/album folder? Why not put Controller, Form, and Model under module/Album?
Why is there a folder called Controller? There used to be a folder called controllers (plural, why singular now?) in a module before, that makes sense. But why is/are controller(s) inside a src/Album folder now?
Thank you for your time. I have tried to research but I think it's just too big absorb when (in my opinion) it seems so sparsely documented. Or, if someone could point me to a book like http://survivethedeepend.com/ but for ZF2, it'd be greatly apprecated.
Zend Framework 2 follows PHPfigs PSR-0 Standards. This means that the directory structure directly relates to the classname. But before i come close to that, let me explain the basic architecture.
First you have the ModuleNAME. Since the Module name needs to be unique, it only makes sense to map the Modulename to the Namespace of your model.
Inside the modules folder you have three sub-folders. One folder for configuration items named config. One folder for source-code files named src and one additional folder for the view files view
This separation is simply for overview. You separate configuration, view and sourcecode from another. It makes no sense to bunch them together and i guess you'd agree. This pretty much has been the same for ZF1, too.
The interesting part is the source-folder src. Earlier i mentioned about the PSR-0 Standard. And this is the place where it comes into effect. By default the source-files for each module will be looked upon from the source-folder src. So whenever you have a class, it will be using PSR-0 Standards based off of the source-folder. Mewning: My\Funky\Class would be found within src\My\Funky\Class.php
And that's basically all there is to it. Controllers usually have a FQCN like Mymodule\Controller\SomeController so you will find this class inside src\Mymodule\Controller\SomeController.php
The main question arising could be: Why are the folders sometimes all lowercase and sometimes ucfirst. The answer, once again, is PSR-Standards. Classnames and/or Namespaces are supposed to begin with an upperchar character. And since path-names are case-sensitive, the folders need to match the classnames exactly!
EDIT Another nice read i've just stumbled upon is Rob Allens latest blogpost: Thoughts on module directory structure. He explains how you can change the default setup quite easily to your likings.

IntelliJ Idea auto-complete for my own grails domain meta class methods?

I'm using IntelliJ Idea 10 IDE for my grails development and while it's great at working out the "standard" meta class methods on, for example, domain classes (save, findBy etc), it (obviously) can't pick up methods added by plugins or my own code.
While I don't expect the IDE to be able to pick these up automatically, I'm optimistically wondering if there's a way to tell IntelliJ that, for example, "myMethod" is added to all domain objects, and that it takes a map and returns "myType".
It's a long shot I know, but does anyone know how this might be done in config, a plugin, or by some smoke-and-mirrors so I can a) stop missing simple, stupid typos and b) get some auto-complete?
I think you're looking for the GroovyDSL scripting framework
http://confluence.jetbrains.net/display/GRVY/Scripting+IDE+for+DSL+awareness
its possible to save a *.gdsl file somethere in src dir, with content:
contributor(context()) {
def scope = com.intellij.psi.search.GlobalSearchScope.allScope(project);
delegatesTo(com.intellij.psi.JavaPsiFacade.getInstance(project).findClass('org.grails.datastore.gorm.GormStaticApi', scope)) delegatesTo(com.intellij.psi.JavaPsiFacade.getInstance(project).findClass('org.grails.datastore.gorm.GormEntity', scope))}

Resources