Symfony 2.1 Doctrine filters (enable/disable) - symfony

I'm currently implementing Doctrine filters in my Symfony2.1 project with the following setup:
<?php
namespace Acme\Bundle\Entity;
class Article {
/**
* #ORM\Column(type="string")
*/
private $status;
...
}
//app/config/config.yml
doctrine:
orm:
filters:
status:
class: Acme\Bundle\Filter\StatusFilter
enabled: false
....
//src/Acme/Bundle/Filter/StatusFilter.php
namespace Acme\Bundle\Filter;
use Acme\Bundle\Entity\Status;
class StatusFilter extends SQLFilter {
public function addFilterConstraint(ClassMetadata $target, $alias)
{
$filter =
$target->reflClass->implementsInterface('Acme\Bundle\Entity\Status')?
$alias . '.status = ' . Status::PUBLISHED : '';
return $filter;
}
}
Where Acme\Bundle\Entity\Status is just an interface.
The code is working as expected when the filter is enabled in config.yml.
The problem is that I cannot retrieve all articles for administration!
Is there a way to enable this filter for a certain bundle?
p.s. I know how to enable and disable the filter with the EntityManager,
I just cannot find the proper place to do it for the frontend Bundle.
my admin section is accessible by route prefix myadmin
www.example.com/myadmin/ -> admin section = disable filter (disabled by default in config)
www.example.com/... -> anything else = enable filter.

Looking at the Doctrine code, there are methods to enable and disable filters.
Once you have defined your filter in the config.yml file, you can enable/disable in a controller or service:
// 'status' is the unique name of the filter in the config file
$this->getDoctrine()->getManager()->getFilters()->enable('status');
$this->getDoctrine()->getManager()->getFilters()->disable('status');
Note: this was taken from Symfony 2.3. You would need to test this with previous versions of Symfony/Doctrine.

there is no notion of bundle at Doctrine level. The only way I see would be to detect which controller is used, by parsing its className (reflection, ...) during a kernel.request event, or a kernel.controller event.
Then, if you detect that your controller is in FrontendBundle, just disable/enable your doctrine filter.
If you prefer using routing to detect when to disable/enable, just use kernel.request event. You will have access to all request parameters, via $event->getRequest()->attributes->get('_controller') for example.

Related

Constraints violations always empty when use Symfony validator component

I have encourred a strange problem when use a Symfony validator, the constraints violations is always empty even if entity does not respect constraints.
Entity :
use Symfony\Component\Validator\Constraints as Assert;
class Author
{
/**
* #Assert\NotBlank()
*/
public $name;
}
The code to use validator :
$Author = new Author();
$validator = Validation::createValidator();
var_dump( count($validator-> validate($Author)) );
Result : 0
Do you have an idea on the origin of this problem ?
Thanks in advance.
Jérémy
You do not use the validator pre-configured by Symfony. Is that intended? Usually you validate objects by either injecting the validator into your service or by pulling it from the container (its id is validator).
In your case, you are going to create a new validator instance. However, if not enabled explicitly, annotation support is turned off. You would have to enable it yourself (but the approach of using the configured validator service as written above is usually what you want to do):
$validator = Validation::createValidatorBuilder()
->enableAnnotationMapping()
->getValidator();
enabled in the main config file:
# config/packages/framework.yaml
framework:
validation: { enabled: true }
enable_annotations:
# config/packages/framework.yaml
framework:
validation: { enable_annotations: true }
official doc:
Validation Configuration

FOSElasticaBundle: Setting analyzer for custom properties

I am using the FOSElasticaBundle in Symfony 3.3. I have registered an event listener on the POST_TRANSFORM event that computes and adds a custom property like this:
public function addCustomProperty(TransformEvent $event)
{
$document = $event->getDocument();
$custom = $this->anotherService->calculateCustom($event->getObject());
$document->set('custom', $custom);
}
Now I need to set the analyzer to be used for this property. How can I do that?
I already tried to add the custom field name to the type definition in my fos_elastica config but that causes an exception as the bundle then expects that property on my entity as well.
I finally found out that I could use dynamic_templates to set the desired analyzer like this:
fos_elastica:
indexes:
app:
types:
myentity:
dynamic_templates:
custom_field:
path_match: customfield.*
mapping:
analyzer: myanalyzer
properties:
...

Symfony2; domain/host based info in controller and base template

I'm building an symfony2 app that is configurable up to some point based on what domain is used to access the site.
For ease of this question, lets say there is an "Domain" entity in the database containing the hostname and further configuration.
Think about minor template differences, some differences in header/footer. A difference in products being offered.
The routes available would not be different.
There are 2 places where I need this Domain object.
* in a Controller::action
* in a base template (even if the controller didn't need it)
I would not need it somewhere else, if I did, I could simply pass it from the controller.
What would be the best way to get this object without creating too much overhead and not fetching it when we don't actually need it.
Some thoughts I got so far:
* I could override the ControllerResolver and determine the Domain object based on the Request object. Although I don't seem to have access to the ServiceContainer there.
* I could add some method to a BaseController that can retrieve the domain for me when I'm in a Controller:Action.
* For usage in the template I could create a TwigExtension that adds a global variable. But it would need access to the Request object or RequestStack. Also, this would only help me in the template, I might be doing the same thing twice.
Any suggestions what might be a good approach here?
Don't know if this is the best solution, but worked well for me so far.
Since the domain information depends on the request it is NOT a service, so don't try to inject it in services or you'll get a bad headache. The most natural place to set information about the domain is in the request, and allow the controllers to read this information to interact with the services.
So, you can setup a Kernel event listeners which read the information from the database and set a domain Request attribute, like this:
<?php
namespace Acme\SiteBundle\EventListener;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Doctrine\ORM\EntityRepository;
class DomainSubscriber implements EventSubscriberInterface
{
protected $domainRepository;
public static function getSubscribedEvents()
{
return array(
KernelEvents::REQUEST => 'onKernelRequest'
);
}
public function __construct(EntityRepository $domainRepository)
{
$this->domainRepository = $domainRepository;
}
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
// Console/CLI commands don't have Domain info
if ($request === null)
return;
$domain = $this->domainRepository->find($request->getHost());
if ($domain === null)
throw new \RuntimeException(sprintf("Cannot find domain %s", $request->getHost()));
$request->attributes->set('domain', $domain);
}
}
Which must be registered in services.yml (or XML) with:
acme_site.manager:
class: Doctrine\ORM\EntityManager
factory_service: doctrine
factory_method: getManager
acme_site.domain_repository:
class: Doctrine\ORM\EntityRepository
factory_service: acme_site.manager
factory_method: getRepository
arguments:
- 'AcmeSiteBundle:Domain'
acme_site.domain_subscriber:
class: Acme\SiteBundle\EventListener\DomainSubscriber
arguments:
- "#acme_site.domain_repository"
tags:
- { name: kernel.event_subscriber }
In your Controller you can now access the data by simply doing this:
public function someAction(Request $request) {
$domain = $request->attributes->get('domain');
$domain->getWhatever();
}
And in Twig you can always access the request with this:
{% set domain = app.request.attributes.get('domain') %}
whatever: {{ domain.whatever }}
Hope this help!
DISCLAIMER: the code is copy-pasted and then edited, so it may contain some minor error.
NOTE: If you really need to inject the request in services, then I suggest you to read the docs about the RequestStack (Symfony 2.4+), or use a setRequest method and take care of container scopes.

Is it possible to persist different file entities with doctrine2 in OneupUploaderBundle?

I have many ORM entities which I want to link them to their corresponding files. Example: the entity Motors with MotorsFile, Houses with HousesFile, ...
These ORM entities can be easily definied. This is not a problem. But, my problem is: "Is it possible to define many ORM entities such us MotorsFile and HousesFile while using OneupUploaderBundle?"
I asked this question because processing these uploaded file with doctrine needs creating an event listener to PostUploadEvent and PostPersistEvent. The event listener would be something like that:
<?php
namespace Acme\HelloBundle\EventListener;
use Oneup\UploaderBundle\Event\PostPersistEvent;
use Minn\AdsBundle\Entity\MotorsAdsFile;
class UploadListener
{
protected $manager;
public function __construct(EntityManager $manager)
{
$this->manager = $manager;
}
public function onUpload(PostPersistEvent $event)
{
$file = $event->getFile();
$object = new MotorsFile();
$object->setFilename($file->getPathName());
$this->manager->persist($object);
$this->manager->flush();
}
}
But, this code will allow me only to persist one entity (for this example MotorsFile). So, is it possible to specify the uploaded file is corresponding to which entity?
Thanks...
You basically have two options.
Given you use different mappings for the different file uploads you can use generic events that will be dispatched for each mapping.
After each upload not only the listeners for the oneup_uploader.post_upload will be executed, but also the listeners for the special event oneup_uploader.post_upload.{mapping}, where {mapping} is the configured name in your config.yml.
Let's say you have a configuration similar to this one:
oneup_uploader:
mappings:
motors:
frontend: blueimp
storage:
directory: %kernel.root_dir%/../web/uploads/motors
houses:
frontend: blueimp
storage:
directory: %kernel.root_dir%/../web/uploads/houses
After uploading a file to the motors mapping, the generic event oneup_uploader.post_upload.motors will be dispatched. The same goes for the houses mapping.
If you don't want to use different upload handlers, you could also check for the type.
<?php
namespace Acme\HelloBundle\EventListener;
use Oneup\UploaderBundle\Event\PostPersistEvent;
use Minn\AdsBundle\Entity\MotorsAdsFile;
class UploadListener
{
public function onUpload(PostPersistEvent $event)
{
$type = $this->getType();
}
}
$type will be either motors or houses, depending with which mapping configuration the file was uploaded to the server.

How to fix "This repository can be attached only to ORM sortable listener" error in Gedmo sortable?

When usieing StofDoctrineExtensions (which is a Symfony2 port of Gedmo Doctrine Extensions) Sortable behaviour I kept on getting this error:
This repository can be attached only to ORM sortable listener
Since I could not easily find the answer in official docs I'm leaving an answer here for future reference.
You need to enable any listeners you are using. In this case, Sortable.
stof_doctrine_extensions:
default_locale: en_US
orm:
default:
sortable: true
For Symfony 4, add this configuration in /config/packages/stof_doctrine_extensions.yaml. For older versions of Symfony, add it to config.yml.
In order to use Sortable behaviour you need to add an event listener to your bundle's boot method
<?php
namespace Acme\DemoBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class AcmeDemoBundle extends Bundle
{
public function boot()
{
// get the doctrine 2 entity manager
$em = $this->container->get('doctrine.orm.default_entity_manager');
// get the event manager
$evm = $em->getEventManager();
$evm->addEventSubscriber(new \Gedmo\Sortable\SortableListener);
}
}

Resources