Working but hidden/missing entities - doctrine-orm

I have several modules in my zf2 application, they all have their own entities and all on the same connection.
A little diagram to explain the situation :
-Module 1
-Entity A
-Entity B
-Module 2
-Entity C
-Entity B
(all on the same database connection)
The problem is all the entities are working, i can fetch, update them, etc. but some are "invisible". I have some cross-module relations and they are working fine too.
For exemple, all entities from module 2 does not appear when I use the php public/index.php orm:info command nor in the Zend Developer Toolbar.
When I edit an entity, I have to manually update the database as php public/index.php orm:schema-tool:update says Nothing to update - your database is already in sync with the current entity metadata.
The entities can be fetched, they are just not "seen" by the ZDT nor the terminal, I don't know what I did wrong.
Thanks for your help

This is often caused when the path to the entities has not been declared in your config:
<?php
return array(
'doctrine' => array(
'driver' => array(
// defines an annotation driver with two paths, and names it `my_annotation_driver`
'my_annotation_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(
'path/to/my/entities',
'another/path'
),
),
// default metadata driver, aggregates all other drivers into a single one.
// Override `orm_default` only if you know what you're doing
'orm_default' => array(
'drivers' => array(
// register `my_annotation_driver` for any entity under namespace `My\Namespace`
'My\Namespace' => 'my_annotation_driver'
)
)
)
)
);

Related

Zend 2 Framework - Doctrine generates tables from one entity from 2 given

I'm working with Zend 2 from a few days, and I have a little trouble. We are using doctrine, and trying to generate the data base schema from the entities. I specify in each module where doctrine should find the entities using module.config.php file of each module. Currently I have 2 modules with 2 diferent module.config files and these are the lines where I specify the paths where Doctrine should find the entities:
module.config.php (MailTemplates (Module))
'doctrine' => array(
'driver' => array(
'application_entities' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => (__DIR__ . '/../src/MailTemplates/Model')
),
'orm_default' => array(
'drivers' => array(
'MailTemplates\Model' => 'application_entities'
),
),
),
),
and module.config.php (Application (Module))
'doctrine' => array(
'driver' => array(
'application_entities' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => (__DIR__ . '/../src/Application/Entity')
),
'orm_default' => array(
'drivers' => array(
'Application\Entity' => 'application_entities'
),
),
),
),
);
When I execute the command to generate the schema from the entities (./vendor/bin/doctrine-module orm:schema-tool:create) it only creates the MailTemplate tables, and ignore the Application module ones. If I comment the lines of the module.config.php of MailTemplate module, and execute the command again, I can se that tables from the Application Module entity have been created succesfully. So I suppose that somehow the info from the 2 module.config files is being overwritten.
I need to generate the DB shema from diferent entities from different modules and I don't know how.
Thanks!!
The reason for this is that you are setting the same name to the drivers. Although zend merge the configurations, it will override configurations with same names.
module.config.php (MailTemplates (Module))
[...]
'drivers' => array(
'MailTemplates\Model' => 'application_entities' <-- rename this
)
[..]
module.config.php (Application (Module))
[...]
'drivers' => array(
'Application\Entity' => 'application_entities'
)
[..]
Zf2 module.config.php file is not overwrite. zf2 merge all module.config.php into a single file.
Zend\ModuleManager\Listener\ConfigListener triggers a special event, Zend\ModuleManager\ModuleEvent::EVENT_MERGE_CONFIG, after merging all configuration, but prior to it being passed to the ServiceManager. By listening to this event, you can inspect the merged configuration and manipulate it.
See how zend merge configuration files https://framework.zend.com/manual/2.4/en/tutorials/config.advanced.html#configuration-mapping-table

Its there a way to use Firebird/Ibase with Doctrine in Zend Framework 2?

Configuration of doctrine in Zend Framework:
This is the configuration to use mysql driver but i want tu use the PDO database driver for ibase.
<?php
return array(
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8'
),
),
),
)
));
?>
Dbal Doctrine driver for firebird is a work in progress not yet merged on github
http://www.doctrine-project.org/jira/browse/DBAL-95
I have requested the patch and it will be integrated soon
On the Zend Framework side it should be possible to use the pdo addapter without dbal (unsupported yet)
https://zf2.readthedocs.org/en/latest/modules/zend.db.adapter.html#zend-db-adapter
Thanks!
Looks like meanwhile i can use the PDO for Ibase with a Zend Adapter
return array(
'db' => array(
'driver' => 'PDO_FIREBIRD',
'database' => 'localhost:C:\Base.FDB',
'username' => 'SYSDBA',
'password' => 'masterkey',
),
);

Switching Doctrine Entity in Zend Framework 2

I have two modules. 1) User Module and 2) RbacUser Module. RbacUser Module is depended on User Module. I extend my "User entity" from User Module in RbacUser Module and add some new attributes to User Entity like roles etc.
What i am to do is, When i used RbacUser Module, make RbacUser Module User Entity default. So schema tool know that which entity should use to create table ?
In Short Words:
if Only User Module is used then make its User Entity Default
or
if RbacUser Module is used then make its User Entity Default and ignore User's entity.
I found the solution
These are the configuration of my both modules
User Module Config file
User\Config\module.config.php
'driver' => array(
'user_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(
__DIR__.'/../src/User/Entity',
),
),
'orm_default' => array(
'drivers' => array(
'User\Entity\User' => 'user_driver'
)
)
),
RbacUser Module config file
RbacUser\Config\module.config.php
'doctrine' => array(
'driver' => array(
'rbacuser_driver' => array(
'class' => 'Doctrine\\ORM\\Mapping\\Driver\\AnnotationDriver',
'cache' => 'array',
'paths' => array(
__DIR__.'/../src/RbacUser/Entity',
),
),
'orm_default' => array(
'drivers' => array(
'RbacUser\Entity\User' => 'rbacuser_driver'
)
)
)
)
If i don't use "User" Module doctrine driver config then my problem is solved. Is their any way to check if RbacUser Module is used then don't use doctrine driver configuration in "User" Module
You must implement an IdentityProvider in your RbacUser module and specify this configuration key

Silex and Doctrine - ORMException: Unknown Entity namespace alias [semi fixed]

I'm trying to use the Doctrine components in my app built using silex. I was able to get it to work - well almost.
I have my "User" entity and the corresponding repository
When doing
$app['em']->getRepository('Foo\Entity\User')->findAll()
works as expected, however when trying to make a custom query
$this->getEntityManager()
->createQuery(
'SELECT
u
FROM
Foo:User u
WHERE c.id = :x'
)
->setParameter('x',$in)
->getResult();
I get this exception
ORMException: Unknown Entity namespace alias 'Foo'
Please ignore the fact that I can make a select with findById() or findBy(array('id'=>$in)) the problem is with the custom query
This are my configs
$app['orm.em.options'] = array(
'mappings' => array(
array(
'type' => 'annotation',
'namespace' => 'Foo\Entity',
'alias' => 'core',
'path' => '%app.path%/src/Foo/Entity',
'use_simple_annotation_reader' => false,
)
));
and
$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/src/Foo/Entity"));
$params = $app['db.options'];
$app['em'] = EntityManager::create($params, $config);
After some research possible solutions:
auto_mapping: true => tried, no success
registering the namespace => tried, not sure if right was done so may be the solution, please advice how to do it right
besides all this I have tried to search for git repos with similar 'usage' but didn't get it :(
UPDATE
for the moment I use the following line in my query and it works
FROM
InstaLikes\Entity\User u
When you create custom queries, you should use the fully namespace, in this case:
Foo\Entity\User
I am assuming you have checked the alias you have given in the mappings options?
$app['orm.em.options'] = array(
'mappings' => array(
array(
'type' => 'annotation',
'namespace' => 'Foo\Entity',
'alias' => 'core',
'path' => '%app.path%/src/Foo/Entity',
'use_simple_annotation_reader' => false,
)
));
Should that alias option not be set to Foo instead?

How to add a custom DQL function in Doctrine 2 using Zend Framework 2

Well the question pretty much lies in the title. I've read the docs and what I can't find out is how to register the function to my ORM Configuration.
Any help here? Thanks!
Edit: Okay I've done it as Sam said, and made my own class and registered it like
'numeric_functions' => array(
'LOG10' => 'Admin\Model\Log10',
),
However it can't find the class and gives the error
Class 'Admin\Model\Log10' not found in C:\webserver\apache\htdocs\test\vendor\doctrine\orm\lib\Doctrine\ORM\Query\Parser.php on line 3322
Any idea on why this happens?
Actually the link that #foozy gave you is all that you'd need. You simply extend your doctrine configuration array:
return array(
'doctrine' => array(
'connection' => array(
'orm_default' => array(
// Foo
)
),
'configuration' => array(
'orm_default' => array(
'numeric_functions' => array(
'MD5' => 'DoctrineExtensions\Query\Mysql\Md5'
),
'datetime_functions' => array(),
'string_functions' => array(),
'metadata_cache' => 'filesystem',
'query_cache' => 'filesystem',
'result_cache' => 'filesystem',
)
)
)
);
The error is connected to DoctrineExtensions autoload. For me both solutions from How to implement beberlei doctrine extensions in zend framework 2 (manual injection of custom functions and applying via config) worked.