PHP code example of radnan / rdn-doctrine

1. Go to this page and download the library: Download radnan/rdn-doctrine library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

radnan / rdn-doctrine example snippets



   return array(
       'modules' => array(
           'RdnDoctrine',
           // ...
       ),
   );
   ~~~

### Dependencies

This module relies on the modules: [RdnConsole](https://github.com/radnan/rdn-console), [RdnDatabase](https://github.com/radnan/rdn-database), and [RdnFactory](https://github.com/radnan/rdn-factory).

## How to use

Entity managers can be registered with the `RdnDoctrine\EntityManagerManager` service locator using the `rdn_entity_managers` configuration option.

~~~php


return array(
	'rdn_entity_managers' => array(
		'factories' => array(),
		'invokables' => array(),
	),
);
~~~

You can also use the **managers** key to quickly generate an entity manager using simple configuration options:

~~~php


return array(
	'rdn_entity_managers' => array(
		'managers' => array(
			'App' => array(
				/* configuration options */
			),
		),
	),
);
~~~

Configuring an entity manager is as simple as that! Here we've configured an entity manager with the name **App**. Assuming our module name is also **App** the library will set you up with some sane defaults, all of which you can override.

By default, the manager will expect your entities to live inside the `App\Entity` namespace (or more generally `<MANAGER-NAME>\Entity`).

[Configuration documentation](docs/01-config.md)

### Controller plugin

Once an entity manager has been configured, you can access both the entity manager or entity repositories from your controller using the `entity()` plugin.

Since you can register multiple entity managers with different names, by default the plugin will fetch the entity manager with the same name as the module name:

~~~php
namespace App\Controller;

use App\Entity;

class User
{
	public function createAction()
	{
		$user = new Entity\User;
		$user->setEmail('[email protected]');

		$this->entity()->persist($user);
		$this->entity()->flush();
	}


return array(
	'rdn_entity_managers' => array(
		'managers' => array(
			'App' => array(
				'table_prefixes' => array(
					'Foo' => 'foo__',
				),
			),
		),

		'modules' => array(
			'Foo' => 'App',
		),
	),
);
~~~

This will add the entities provided by **Foo** (in the `Foo\Entity` namespace) to the **App** entity manager and instruct all plugins to use the **App** entity manager from within the **Foo** module.

~~~php
namespace Foo\Controller;

class Bar
{
	public function editAction()
	{
		// We can now access the Foo entity repositories
		$bar = $this->entity('Bar')->find($id);
		// - OR - more explicitly
		$bar = $this->entity('Foo:Bar')->find($id);

		// We can also access the App entity repositories
		$user = $this->entity('User')->find($id);
		// - OR - more explicitly
		$user = $this->entity('App:User')->find($id);

		// We have access to the shared entity manager
		$this->entity()->flush();
	}