Reputation: 131
currently I'm building an application based on a legacy database. Within this Database there are UserClasses e.g.:
UserclassEntity:
so I have rewritten the core service and added my custom voter to security.voter:
security.role_hierarchy:
class: AppBundle\Security\Role\RoleHierarchy
arguments:
- '%security.role_hierarchy.roles%'
- '@app.user_class_repository'
security.access.admin_voter:
class: AppBundle\Security\Voter\AdminVoter
arguments: ['@security.role_hierarchy']
tags:
- { name: security.voter, priority: 400 }
public: false
So I implemented all needed MEthods based on Original RoleHierarchy Class:
class RoleHierarchy extends orig_RoleHierarchy
{
/**
* @var array
*/
private $hierarchy;
/**
* @var UserclassRepository
*/
private $userClassRepository;
/**
* @var RoleManager
*/
private $manager;
/**
* @var UserclassRepository
*/
private $userclassRepository;
/**
* @param UserclassRepository $userclassRepository
* @param array $hierarchy
*/
public function __construct(UserclassRepository $userclassRepository, array $hierarchy)
{
$this->hierarchy = $hierarchy;
$this->userclassRepository = $userclassRepository;
$map = $this->buildRolesTree($this->hierarchy);
parent::__construct($map);
}
/**
* Here we build an array with roles. It looks like a two-levelled tree - just
* like original Symfony roles are stored in security.yml
*
* @param array $hierarchy
*
* @return array
*/
private function buildRolesTree(array $hierarchy = [])
{
$userclasses = $this->userclassRepository->findAll();
foreach ($userclasses as $userclass) {
/** @var $userclass Userclass */
if ($userclass->getParent()) {
if (!isset($hierarchy['ROLE_' . $userclass->getName()])) {
$hierarchy['ROLE_' . $userclass->getName()] = array();
}
$hierarchy['ROLE_' . $userclass->getName()][] = 'ROLE_' . $userclass->getParent()->getName();
} else {
if (!isset($hierarchy['ROLE_' . $userclass->getName()])) {
$hierarchy['ROLE_' . $userclass->getName()] = array();
}
}
}
return $hierarchy;
}
}
But when i clear the cache for prod environment i get an error:
[Symfony\Component\Debug\Exception\FatalErrorException]
Compile Error: require(): Failed opening required 'E:\projects\sf2\app\var\cache\pro_/doctrine/orm/Proxies\__CG__AppBundleEntityUserclass.php' (include_path='.;C:\php\pear')
This Exception throws because of missing Proxy Class, but I really don't know how to do this in another way. I need this Entity and I need the Role_Hierarchy based on this Entity.
Any Suggestions in how to fix this?
As suggested some more files: doctrine's part of config.yml
doctrine:
dbal:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: LATIN1
mapping_types:
enum: string
orm:
auto_generate_proxy_classes: "%kernel.debug%"
entity_managers:
default:
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
dql:
string_functions:
COLLATE: AppBundle\DQL\CollateFunction
Doctrine ORM:
<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="AppBundle\Entity\Userclass" table="userclass" repository-class="AppBundle\Repository\UserclassRepository">
<indexes>
<index name="name" columns="name,classname,class,color"/>
</indexes>
<id name="id" type="integer" column="id">
<generator strategy="AUTO"/>
</id>
<field name="name" type="string" column="name" length="50" nullable="false">
<options>
<option name="fixed"/>
</options>
</field>
<field name="classname" type="string" column="classname" length="50" nullable="false">
<options>
<option name="fixed"/>
</options>
</field>
<field name="class" type="integer" column="class" nullable="false">
<options>
<option name="unsigned"/>
</options>
</field>
<field name="color" type="string" column="color" length="10" nullable="false">
<options>
<option name="fixed"/>
</options>
</field>
<many-to-one field="parent" target-entity="AppBundle\Entity\Userclass">
<join-column name="parent_id" referenced-column-name="id" />
</many-to-one>
</entity>
</doctrine-mapping>
Upvotes: 1
Views: 1122
Reputation: 131
I tried a lot of things but nothing really worked. As I found out, the proxy generate Task from doctrine:orm was removed in latest VErsion compatible with Symfony 3.1.x.
If someone struggle into this, the best way (I could find) was to set the auto_generate_proxy_classes from "%kernel.debug%" to true
orm:
auto_generate_proxy_classes: "%kernel.debug%"
As simple as hell, only in dev environment this parameter is true. Running the application in productive mode, turns this obviosly to false, so Proxyclasses are not generated as they are needed. The Role Hierarchy is somehow triggered before doctrine got a chance to generate those needed proxies.
By enabling this auto_generate-option those proxies are generated as they were needed, and also cached. So any minimal performance break will only happen once. After generation these classes als also cached.
On my application I was not able to find any relevant or even little performance issues.
Upvotes: 1