Doctrine ODM for MongoDB and Zend Framework 2 - mongodb

Where is my mistake in the configuration?
I try to implement Doctrine ODM in ZF2 - Have a look to my Configuration File:
namespace Application;
return array(
'doctrine' => array(
'connection' => array(
'odm_default' => array(
'server' => 'localhost',
'port' => '27017',
'user' => null,
'password' => null,
'dbname' => 'homeup',
'options' => array()
),
),
'configuration' => array(
'odm_default' => array(
'metadata_cache' => 'array',
'driver' => 'odm_default',
'generate_proxies' => true,
'proxy_dir' => 'data/DoctrineMongoODMModule/Proxy',
'proxy_namespace' => 'DoctrineMongoODMModule\Proxy',
'generate_hydrators' => true,
'hydrator_dir' => 'data/DoctrineMongoODMModule/Hydrator',
'hydrator_namespace' => 'DoctrineMongoODMModule\Hydrator',
'default_db' => null,
'filters' => array(), // array('filterName' => 'BSON\Filter\Class'),
'logger' => null // 'DoctrineMongoODMModule\Logging\DebugStack'
)
),
'driver' => array(
'odm_default' => array(
/* 'drivers' => array(
'class' => 'Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver',
'namespace' => 'Application\Document',
'paths' => array('module/Application/src/Application/Document'),
*/
'odm_driver' => array(
'class' => 'Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver',
'paths' => array(__NAMESPACE__ .'/src/' . __NAMESPACE__ . '/Document')
),
'odm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Document' => 'odm_driver'
)
)
)
)
),
'documentmanager' => array(
'odm_default' => array(
'connection' => 'odm_default',
'configuration' => 'odm_default',
'eventmanager' => 'odm_default'
)
),
'eventmanager' => array(
'odm_default' => array(
'subscribers' => array()
)
),
);
Info:
Used tutorial : https://github.com/doctrine/DoctrineMongoODMModule
ZF2 Version 2.2.4
Doctrine dev-master
MongoDB PHP Driver works (with plain PHP)
Wastet hours of time & searched the web
EDIT
Fatal error: Uncaught exception
'Zend\Stdlib\Exception\BadMethodCallException' with message 'The
option "odm_driver" does not have a matching setOdmDriver setter
method which must be defined' in
C:\xampp\htdocs\homeup.dev\vendor\zendframework\zendframework\library\Zend\Servi‌​ceManager\ServiceManager.php
on line 859
It was easier than I thought...
Solution is here:
example working config for Doctrine ODM zf2 Module?
Less is more.

Related

TYPO3 8 dbal mapping with other server

I've a problem.
I just installed a new TYPO3 8 with adodb and dbal extensions.
Now I have an other server with a MySQLi Server and some custom tables in one database.
I want to show and edit the data from that other MySQLi Server Database Table named account in my TYPO3. For that, I have created my own extension with an model named tx_base_domain_model_account with as example 2 fields.
After that I created a dbal mapping with the following configuration:
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['dbal'] = array(
'handlerCfg' => array(
'_DEFAULT' => array(
'type' => 'native',
'config' => array(
'driver' => 'mysqli'
)
),
'_otherServer' => array(
'type' => 'native',
'config' => array(
'username' => 'username',
'password' => 'password',
'host' => '192.168.177.XX',
'database' => 'account',
)
),
),
'table2handlerKeys' => array(
'account' => '_otherServer'
),
'debugOptions' => array(
'enabled' => true,
'printErrors' => true,
'EXPLAIN' => 1,
'parseQuery' => 1,
'joinTables' => 1
),
'mapping' => array(
'tx_base_domain_model_account' => array(
'mapTableName' => 'account',
'mapFieldNames' => array (
'uid' => 'id',
'pid' => 119,
'login' => 'login',
'password' => 'password',
'cruser_id' => 1
)
)
)
);
But I can't see, edit or whatever the data from that other server database table.
Can you help me?
Thank you
With the switch to doctrine-dbal in core v8, the core extensions 'dbal' and 'adodb' have been obsoleted. See https://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/Database/Configuration/Index.html for configuration details on how to map tables to a different dbms using doctrine-dbal.

Routing in the Zend Framework 2

It's not long since I'm using Zend Framework 2 and I'm trying to write a route for a product that have the rule something like that:
/[slug]-test[ID][av|ai|cv|ci|svb|sib|svc|sic|svi|sii|tv|ti]
An exemple for this rule would look like this:
/freezer-in-good-shape-test12345cv
I try to this with REGEX:
'details' => array(
'type' => 'Zend\Mvc\Router\Http\Regex',
'options' => array(
'route' => '(?<slug>[a-zA-Z0-9_-]+)-test(?<id>[0-9]+)(?<sufix>(av|ai|cv|ci|svb|sib|svc|sic|svi|sii|tv|ti))',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'List',
'action' => 'details',
),
'spec' => '%slug%-test%id%%sufix%',
)
),
and with SEGMENT too:
'details' => array(
'type' => 'Segment',
'options' => array(
'route' => '/:slug-test:id:sufix',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'List',
'action' => 'details',
)
'constraints' => array(
'slug' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]*',
'sufix' => '[av|ai|cv|ci|svb|sib|svc|sic|svi|sii|tv|ti]'
)
)
),
but it's not working. Can anyone tell what is that i'm doing wrong? Thanks!
I’d go with Segment here. The only issue I’ve noticed in your route config is the sufix constraint: instead of [av|ai|…] you should type (av|ai|…).
please try with this-
'details' => array(
'type' => 'Segment',
'options' => array(
'route' => '/:slug[-test]:id:sufix',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'List',
'action' => 'details',
)
'constraints' => array(
'slug' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]*',
'sufix' => '[av|ai|cv|ci|svb|sib|svc|sic|svi|sii|tv|ti]'
)
)
),
on this line 'route' => '/:slug-test:id:sufix',
you had the issues, as it considers "slug-test" as single variable.
so for this you should separate "-test" with "slug" variable.
'route' => '/:slug[-test]:id:sufix',

Zend Framework 2 routing error: resolves to invalid controller class or alias

I'm trying to learn Zend Framework 2 and I have their skeleton application up and running. In order to access it I visit http://localhost:8080/. When visiting that link it displays their generic Zend page. What I want to be able to do is visit http://localhost:8080/application/test and have it bring me to a different page with a different layout.
Here is the module.config.php
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* #link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
* #copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* #license http://framework.zend.com/license/new-bsd New BSD License
*/
return array(
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'index',
),
),
),
// The following is a route to simplify getting started creating
// new controllers and actions without needing to create a new
// module. Simply drop new controllers in, and you can access them
// using the path /application/:controller/:action
'application' => array(
'type' => 'Literal',
'options' => array(
'route' => '/application',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
)
),
'service_manager' => array(
'abstract_factories' => array(
'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
'Zend\Log\LoggerAbstractServiceFactory',
),
'aliases' => array(
'translator' => 'MvcTranslator',
),
),
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
'controllers' => array(
'invokables' => array(
'Application\Controller\Index' => 'Application\Controller\IndexController'
),
),
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
// Placeholder for console routes
'console' => array(
'router' => array(
'routes' => array(
),
),
),
);
In order to make it work for me this is what I tried:
First I created a controller named TestController in Application/src/Application/Controller/.
Next I added 'application/test/index' => DIR . '/../view/application/test/index.phtml' to the template_map array in view_manager.
I also added 'Application\Controller\Test' => 'Application\Controller\TestController' to the controllers array.
When I visit http://localhost:8080/application/test I get the error:
test(resolves to invalid controller class or alias: test)
I'm obviously doing something wrong but tbh the documentation on Zend is not newb friendly at all and it's becoming very frustrating. Could someone point me in the correct direction? There's just so much configuration that I'm sure I'm missing something small. Thanks!
At the moment the route named application (the parent) defines a URL route of /application. The child route default however requires the controller name to be passed in as the first argument, followed by
action.
This means the URL would be
/application/[:controller]/[:action]
So visting
/application/test
You are inadvertently trying to fetch the 'test' controller; hence the error.
Resolves to invalid controller class or alias : test
To resolve this I would strongly recomend against using the a :controller route parameter but instead use a route per controller action.
'application' => [
'type' => 'Literal',
'options' => [
'route' => '/application',
'defaults' => [
'controller' => 'Application\Controller\Index',
'action' => 'index',
],
'may_terminate' => true,
'child_routes' => [
'test' => [
'type' => 'Literal',
'options' => [
'route' => '/test',
'defaults' => [
// controller value is inherited from parent!
'action' => 'test',
],
],
],
],
],
],

How to set a variable in ObjectSelect criteria params in DoctrineModule

I'd like to know how to set a variable in ObjectSelect criteria params.
My code is as follow:
$this->add(
array(
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'name' => 'shop',
'attributes' => array(
'class' => 'chosen-select form-control'
),
'options' => array(
'object_manager' => $this->objectManager,
'target_class' => '\Godana\Entity\Shop',
'property' => 'name',
'label' => 'Shop',
'label_attributes' => array(
'class' => 'col-sm-3 control-label',
),
'find_method' => array(
'name' => 'findBy',
'params' => array(
'criteria' => array('owner' => $this->shopOwner),
),
),
),
)
);
and it returns empty value but if I use a static value like 'criteria' => array('owner' => 1) it returns data from my db.
shouldn't it be $this->shopOwner->getId() ?

ZF2 routing configuration

I've just built a zf2 project and i'm having a configuration problem.
When i go to mydomain.com this routs as specified in configuration files to Application module, index controller, index action.
But If i type mydomain.com/otheraction this is not routed to Application module, index controller, otheraction action. Of course, since it isn't configured to do this.
So my question would be how do i configure the app to do that? I've added my Application/config/module.config.php file and the main config file. Thank you!
module.config.php
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* #link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
* #copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* #license http://framework.zend.com/license/new-bsd New BSD License
*/
return array(
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'index',
),
),
),
// The following is a route to simplify getting started creating
// new controllers and actions without needing to create a new
// module. Simply drop new controllers in, and you can access them
// using the path /application/:controller/:action
'application' => array(
'type' => 'Literal',
'options' => array(
'route' => '/application',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
),
),
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
),
),
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
'controllers' => array(
'invokables' => array(
'Application\Controller\Index' => 'Application\Controller\IndexController'
),
),
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
);
application.config.php
<?php
return array(
'modules' => array(
'Application',
'ZfcBase',
'ZfcUser',
),
'module_listener_options' => array(
'config_glob_paths' => array(
'config/autoload/{,*.}{global,local}.php',
),
'module_paths' => array(
'./module',
'./vendor',
),
),
);
The default route configured is to accept any url of this format, this is the standard format
mydomain.com/
mydomain.com/applicatiom/controller/action (standard format)
in your case mydomain.com/otheraction the router expect a module otheraction, but its not present.
if you need to have a route as specified above have a entry under route
'otheraction' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/otheraction',
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'otheraction',
),
),
),
In the routes subarray, add a next entry similar to the 'home' entry, for example:
'user' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/user',
'defaults' => array(
'controller' => 'Application\Controller\User',
'action' => 'some-action',
),
),
),
That would route yourpage.com/user to someActionAction in your UserController.