In a symfony command script, i find entities like this :
$this->em->getRepository('AppBundle:House')->findBy(...);
Foreach element, i call a service method with my element as an argument.
In my Service, i have an error when i try to create an element linked to the "house"object
this doesn't works :
$glass = $this->em->getRepository('AppBundle:Glass')->findOneBy(['house' => $house->getId(), 'name' => $name]);
if (! $glass instanceof Glass) {
$newGlass= new Glass($house, $name);
$this->em->persist($newGlass);
$this->em->flush();
}
this works but i don't want to have to find again my existing house i already founded in my command script :
$existingHouse = $this->em->getRepository('House')->find($house->getId());
$glass = $this->em->getRepository('AppBundle:Glass')->findOneBy(['house' => $house->getId(), 'name' => $name]);
if (! $glass instanceof Glass) {
$newGlass= new Glass($existingHouse, $name);
$this->em->persist($newGlass);
$this->em->flush();
}
Can you help me to understang what's wrong ?
Glass constructor
public function __construct(House $house, string $name)
{
$this->name= $name;
$house->addGlass($this);
$this->house= $house;
}
house orm file :
oneToMany:
glasses:
targetEntity: Glass
mappedBy: house
cascade: ["persist"]
glass orm file :
manyToOne:
house:
targetEntity: House
cascade: { }
fetch: LAZY
mappedBy: null
inversedBy: glasses
joinColumns:
house_id:
referencedColumnName: id
orphanRemoval: false
Related
I have two Entities Company and Storage with One-To-Many Bidirectional relationship. Entities and their relations are cached (doctrine second level cache). The issue is that, when i create a new Storage entity, Company storages collection doesn't have this new entity until I clear the cache manually.
AppBundle\Entity\Main\Company:
type: entity
table: main.company
cache:
usage: NONSTRICT_READ_WRITE
id:
id:
type: integer
nullable: false
id: true
generator:
strategy: IDENTITY
fields:
legalName:
type: string
nullable: false
length: 255
options:
fixed: false
column: legal_name
oneToMany:
storages:
targetEntity: AppBundle\Entity\Main\Storage
mappedBy: company
cascade: ["all"]
orphanRemoval: true
cache:
usage: NONSTRICT_READ_WRITE
AppBundle\Entity\Main\Storage:
type: entity
table: main.storage
cache:
usage: NONSTRICT_READ_WRITE
id:
id:
type: integer
nullable: false
options:
unsigned: false
id: true
generator:
strategy: IDENTITY
fields:
storageName:
type: string
nullable: true
length: 255
options:
fixed: false
column: storage_name
manyToOne:
company:
targetEntity: AppBundle\Entity\Main\Company
cascade: ["all"]
fetch: LAZY
mappedBy: null
inversedBy: storages
joinColumns:
company_id:
referencedColumnName: id
orphanRemoval: false
cache:
usage: NONSTRICT_READ_WRITE
This is action where Storage is created. There is nothing unusual.
public function addAction(Request $request)
{
$form = $this->createForm(StorageAddType::class, null);
$form->handleRequest($request);
if (!$form->isSubmitted()) {
throw new \RuntimeException('Некорректный запрос');
}
if (!$form->isValid()) {
throw new \Symfony\Component\Validator\Exception\ValidatorException((string)$form->getErrors(true));
}
$doctrine = $this->getDoctrine();
/**
* #var Storage $storage
*/
$storage = $form->getData();
$manager = $doctrine->getManager();
$manager->persist($storage);
$manager->flush();
return $this->createAjaxDataResponse($this->createSuccessMessage('Storage successfully added'));
}
Such behavior is watched only when i try to create new Entity (Storage). Then on update/delete actions - Storages collection of Company are updated.
You are clearly wrong with persisting data. You try to persist unserialized object from form into uknown repository via manager.
Try this:
public function addAction(Request $request)
{
$form = $this->createForm(StorageAddType::class, null);
$form->handleRequest($request);
$em = $this->getDoctrine()->getManager();
if($form->isSubmitted() && $form->isValid())
{
$storage = new Storage();
$storage->setVal1($form->get('Val1'));
$storage->setVal2($form->get('Val2'));
$em->persist($storage);
$em->flush();
return $this->createAjaxDataResponse($this->createSuccessMessage('Storage successfully added'));
}
return $this->render('YOUR_TWIG_LAYOUT', [
'form' => $form->createView()
]);
}
You can also try to persist whole object, if form is seriaized properly by serializing data into entity. Write method like setValsFromForm($data) and serialize vars from $data form.
Then change these lines:
$storage->setVal1($form->get('Val1'));
$storage->setVal2($form->get('Val2'));
into
$storage->setValsFromForm($form->getData());
Also:
Exceptions and form validations should be handled by Form Validator in form class, not in controller. Exception is when you create form via formbuilderinterface in the controller, but you add logic there, not outside $form class.
I created a task link and a contextual one for base_route: entity.node.canonical
mymodule.routing.yml
mymodule.mycustomroute:
path: '/node/{node}/custom-path'
defaults:
_form: '\Drupal\mymodule\Form\MyForm'
requirements:
_permission: 'my permission'
node: '[0-9]+'
mymodule.links.tasks.yml
mymodule.mycustomroute:
route_name: mymodule.mycustomroute
base_route: entity.node.canonical
title: 'my title'
mymodule.links.contextual.yml
mymodule.mycustomroute:
route_name: mymodule.mycustomroute
group: node
My link shows up next to View / Edit / Delete links on each node as I wanted.
Now I am wondering how is it possible to make these links available only for specific node type(s)?
mymodule/mymodule.routing.yml :
mymodule.mycustomroute:
path: '/node/{node}/custom-path'
defaults:
_form: '\Drupal\mymodule\Form\MyForm'
requirements:
_permission: 'my permission'
_custom_access: '\Drupal\mymodule\Access\NodeTypeAccessCheck::access'
_node_types: 'node_type_1,node_type_2,node_type_n'
node: '\d+'
mymodule/src/Access/NodeTypeAccessCheck.php :
namespace Drupal\mymodule\Access;
use Drupal\Core\Access\AccessCheckInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\node\NodeInterface;
use Symfony\Component\Routing\Route;
/**
* Check the access to a node task based on the node type.
*/
class NodeTypeAccessCheck implements AccessCheckInterface {
/**
* {#inheritdoc}
*/
public function applies(Route $route) {
return NULL;
}
/**
* A custom access check.
*
* #param \Drupal\node\NodeInterface $node
* Run access checks for this node.
*/
public function access(Route $route, NodeInterface $node) {
if ($route->hasRequirement('_node_types')) {
$allowed_node_types = explode(',', $route->getRequirement('_node_types'));
if (in_array($node->getType(), $allowed_node_types)) {
return AccessResult::allowed();
}
}
return AccessResult::forbidden();
}
}
Or you can specify route parameters in the mymodule.links.menu.yml file:
mymodule.add_whatever:
title: 'Add whatever'
description: 'Add whatever'
route_name: node.add
route_parameters: { node_type: 'name_of_node_type' }
menu_name: main
weight: 7
I'm using Symfony 2.3 and ElasticSearchBundle 3.0. I implemented two fields for the search. The search works correctly but it doesn't display all results. For example: when I search for the a keyword, the number of hits are 33 hits but it returns only 10 results.
config.php
fos_elastica:
clients:
default: { host: localhost, port: 9200 }
serializer:
callback_class: FOS\ElasticaBundle\Serializer\Callback
serializer: serializer
indexes:
hortis:
finder: ~
client: default
settings:
index:
analysis:
analyzer:
custom_search_analyzer:
type: custom
tokenizer: standard
filter : [standard, lowercase, asciifolding]
custom_index_analyzer:
type: custom
tokenizer: standard
filter : [standard, lowercase, asciifolding, custom_filter]
filter:
custom_filter:
type: edgeNGram
side: front
min_gram: 3
max_gram: 100
types:
business:
mappings:
name: { search_analyzer: custom_search_analyzer, index_analyzer: custom_index_analyzer, type:string }
enabled: ~
gouvernaurat: ~
delegation: ~
postal_code: ~
# activities.principal: { search_analyzer: custom_search_analyzer, index_analyzer: custom_index_analyzer, type:string }
activities :
type : object
properties :
principal : ~
persistence:
driver: orm
model: Toto\AdminBundle\Entity\EntityName
provider: ~
listener: ~
finder: ~
controller.php
public function searchEngineAction(Request $request) {
$finder = $this->container->get('fos_elastica.index.hortis.business');
// get data from both fields
$querystring = strip_tags($request->get('name'));
$querystring2 = strip_tags($request->get('location'));
$boolQuery = new \Elastica\Query\Bool();
// if both fields are empty then display all businesses
if (empty($querystring) and empty($querystring2)) {
$query = new \Elastica\Query\MatchAll();
$boolQuery->addMust($query);
} else {
// create a boolean query
if (!empty($querystring)) {
$fieldQuery = new \Elastica\Query\QueryString();
$fieldQuery->setFields(array('name', 'activities.principal'));
$fieldQuery->setQuery($querystring);
$boolQuery->addMust($fieldQuery);
}
if (!empty($querystring2)) {
$fieldQuery2 = new \Elastica\Query\QueryString();
$fieldQuery2->setFields(array(
'gouvernaurat', 'delegation', 'postal_code'));
$fieldQuery2->setQuery($querystring2);
$boolQuery->addMust($fieldQuery2);
}
}
// select only enbaled business
$enabled = new \Elastica\Query\Term();
$enabled->setTerm('enabled', true);
$boolQuery->addMust($enabled);
$findAll = \Elastica\Query::create($boolQuery);
$findAll->setSize(27);
// trigger search function
$elasticaResultSet = $finder->search($findAll);
dump($elasticaResultSet);
// get results from
$findbusinesses = $elasticaResultSet->getResults();
$noresult = '';
if (!$findbusinesses) {
$noresult = 'no result';
}
$em = $this->getDoctrine()->getManager();
$FrontSettings = $em->getRepository('TotoAdminBundle:FrontSettings')->getFrontSettings();
if (!$FrontSettings) {
throw $this->createNotFoundException('Unable to find frontSettings entity');
}
// get all categories and activities
$categories = $em->getRepository('TotoAdminBundle:Category')
->findBy(array(), array('order' => 'ASC'));
if (!$categories) {
throw $this->createNotFoundException('unable to find categories and activities');
}
$paginator = $this->get('knp_paginator');
$businesses = $paginator->paginate(
$findbusinesses, $this->get('request')->query->get('page', 1)/* page number */, 9/* limit per page */
);
return $this->render('TotoFrontBundle:Front:search_result.html.twig', array(
'querystring' => $querystring, 'businesses' => $businesses,
'FrontSettings' => $FrontSettings, 'noresult' => $noresult,
'categories' => $categories,
));
}
How can I display the all the hits?
Elasticsearch by default only returns the first 10 results. This setting can be modified by specifing the from and size parameters. Note that it rarely makes sense to display all results on one page, instead use a pagination with a controllable amount of viewed items.
If you want all hits on one page also consider using the scroll api as deep pagination can get very inefficiently when having a high amount of results.
types:
product:
mappings:
title: { search_analyzer: custom_search_analyzer, index_analyzer: custom_index_analyzer, type: string }
status:
brand.name: { search_analyzer: custom_search_analyzer, index_analyzer: custom_index_analyzer, type: string }
brand:
type: "nested"
properties:
status: ~
persistence:
driver: orm
model: MyBundle\Entity\Product\Product
provider:
query_builder_method: customProductQueryBuilderElastica
listener: ~
finder: ~
This is my mappings for type product. customProductQueryBuilderElastica contains code which populates only products which have active status and have related brand status active. It is working perfectly if i change products from my admin.
what i want to do is when i change my brand status to inactive, all related products should be removed from ES.
For that i have used brand as nested of product and created listener for it as explained here and now i am able to change brand status for every products in my ES automatically but i want to remove such products when brand status sets to inactive. How can this be achieved in better way?.
After many tries. i finally achieved what i want. I am posting my code here and try to help others.
Thanks to #maercky. i have taken reference to his answer which is given here
Here is my config.yml file.
types:
product:
mappings:
title: { search_analyzer: custom_search_analyzer, index_analyzer: custom_index_analyzer, type: string }
status:
brand.name: { search_analyzer: custom_search_analyzer, index_analyzer: custom_index_analyzer, type: string }
brand:
type: "nested"
properties:
status: ~
persistence:
driver: orm
model: XXX\MyBundle\Entity\Product\Product
provider:
query_builder_method: customProductQueryBuilderElastica
listener: ~
finder: ~
This code will go to service.yml
fos_elastica.listener.brand.product:
class: 'XXX\MyBundle\Listener\ElasticaBrandListener'
arguments:
- #fos_elastica.object_persister.search.product
- ['postPersist', 'postUpdate', 'postRemove', 'preRemove']
- #fos_elastica.indexable
calls:
- [ setContainer, [ '#service_container', #fos_elastica.object_persister.search.product ] ]
tags:
- { name: 'doctrine.event_subscriber' }
and finally this is my Listener for Brand
<?php
namespace XXX\MyBundle\Listener;
use FOS\ElasticaBundle\Doctrine\Listener as BaseListener;
use Doctrine\Common\EventArgs;
use Symfony\Component\DependencyInjection\ContainerInterface;
use XXX\MyBundle\Entity\Supplier\Brand;
use FOS\ElasticaBundle\Persister\ObjectPersister;
class ElasticaBrandListener extends BaseListener
{
/** #var \Symfony\Component\DependencyInjection\ContainerInterface */
private $container;
private $objectPersisterProducts;
public function setContainer(ContainerInterface $container,ObjectPersister $objectPersisterProduct) {
$this->container = $container;
$this->objectPersisterProducts = $objectPersisterProduct;
}
/**
* #param Doctrine\Common\EventArgs $eventArgs
*/
public function postUpdate(EventArgs $eventArgs)
{
/** #var $brand Brand */
$brand = $eventArgs->getEntity();
if ($brand instanceof Brand) {
$this->scheduledForUpdate[] = $brand;
foreach ($brand->getProducts() as $product) {
$brand_status = $brand->getStatus();
$product_status = $product->getStatus();
if($brand_status == 'active' && $product_status == 'active'){
$this->objectPersisterProducts->replaceOne($product);
}else{
$this->objectPersisterProducts->deleteOne($product);
}
}
}
}
}
?>
All this works for me well and so i am contributing this for others.
I defined the following service:
my_project.widget_listing_content_resolver:
class: MyProject\Widget\ListingBundle\Resolver\WidgetListingContentResolver
arguments:
- "#router"
tags:
- { name: my_project.widget_content_resolver, alias: Listing }
And I want to declare an alias of this service, with a different tag:
my_project.widget_domain_operations_content_resolver:
alias: my_project.widget_listing_content_resolver
tags:
- { name: my_project.widget_content_resolver, alias: DomainOperations }
But in my ContentResolverChain, the service aliased "DomainOperations" is not present. Is there a way to solve this ?
EDIT:
I tried the following configuration:
my_project.widget_listing_content_resolver:
class: MyProject\Widget\ListingBundle\Resolver\WidgetListingContentResolver
arguments:
- "#router"
tags:
- { name: my_project.widget_content_resolver, alias: Listing }
- { name: my_project.widget_content_resolver, alias: DomainOperations }
It results that the "my_project.widget_listing_content_resolver" service is only tagged as "Listing". My problem now is: "How to tag a service with multiple tag aliases"
I found the solution to that problem. The service alias was tagged as espected, but the additionnal tag was not read by my CompilerPass:
This was the errored CompilerPass:
$taggedServices = $container->findTaggedServiceIds(
'my_project.widget_content_resolver'
);
foreach ($taggedServices as $id => $attributes) {
$definition->addMethodCall(
'addResolver',
array($attributes[0]['alias'], new Reference($id))
);
}
As you see, it took only the first alias found ($attributes[0])
I had to change it to:
$taggedServices = $container->findTaggedServiceIds(
'my_project.widget_content_resolver'
);
foreach ($taggedServices as $id => $attributes) {
foreach ($attributes as $attribute) {
$definition->addMethodCall(
'addResolver',
array($attribute['alias'], new Reference($id))
);
}
}