Hi.
I try to create custom Delete action - before removing a item I want to remove connected items.
I have two entities: DrugUnified, which contains Drugs.
DrugUnified, can't be removed when Drugs are attached.
<?php
namespace AppBundle\Action\DrugUnified;
use AppBundle\Entity\DrugUnified;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Doctrine\Common\Persistence\ManagerRegistry;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class DrugUnifiedDelete
{
private $doctrine,$em;
public function __construct(ManagerRegistry $doctrine){
$this->doctrine = $doctrine;
$this->em = $this->doctrine->getManager();
}
/**
* Add drugs to unified group
*
* @Route(
* name="api_drugunifieds_delete",
* path="/drug_unifieds/{id}",
* defaults={
* "_api_resource_class"=DrugUnified::class,
* "_api_item_operation_name"="drug_unified_delete"
* }
* )
* @Method("DELETE")
*/
public function __invoke(Request $request, $data)
{
$drugUnifiedDefault = $this->em->getRepository('AppBundle:DrugUnified')->findOneBy(['originalSymbol'=>'0000000']);
if(isset($data) && $data->getId() ){
$drugRepo = $this->doctrine->getRepository('AppBundle:Drug');
$drugs = $data->getDrugs();
foreach ($drugs as $drug){
$data->removeDrug($drug);
$drug->setDrugunified($drugUnifiedDefault);
$this->em->persist($drug);
$this->em->flush();
}
}
return $data;
}
}
@ApiResource(
attributes={
"filters"={"apiOrder", "searchFilter", "searchOrFilter"},
"pagination_items_per_page"=50,
"force_eager"=true,
"normalization_context"={"groups"={"drugs_unified"}},
},
itemOperations={
"get"={"method"="GET"},
"put"={"method"="PUT"},
"deleteDrugUnified"={
"route_name"="api_drugunifieds_delete",
"swagger_context" = {
"parameters" = {
{
"name" = "id",
"in" = "path",
"required" = "true",
"type" = "string"
}
},
"summary" = "Remove drugs_unified, and assosiated drugs move to 'not assigned' group "
}
},
"add"={
"route_name"="api_drugunifieds_add_drug",
"swagger_context" = {
"parameters" = {
{
"name" = "id",
"in" = "path",
"required" = "true",
"type" = "string"
},
{
"name" = "ids",
"in" = "body",
"type" = "string",
"schema": {
"type": "object",
"properties": {
"ids": {
"type": "string"
}
}
},
"style": "simple"
}
},
"summary" = "Add drugs to drugs_unified"
}
},
"remove"={
"route_name"="api_drugunifieds_remove_drug",
"swagger_context" = {
"parameters" = {
{
"name" = "id",
"in" = "path",
"required" = "true",
"type" = "string"
},
{
"name" = "ids",
"in" = "body",
"type" = "string",
"schema": {
"type": "object",
"properties": {
"ids": {
" type": "string"
}
}
},
"style": "simple"
}
},
"summary" = "Add drugs to drugs_unified"
}
}
}
)
In swagger I see it, but when I make request, the build in action goes.
api_assortment_add_drugunified PUT ANY ANY /api/assortments/{id}/drug_unifieds/add
api_assortment_remove_drugunified PUT ANY ANY /api/assortments/{id}/drug_unifieds/delete
api_drugunifieds_add_drug PUT ANY ANY /api/drug_unifieds/{id}/drugs/add
api_drugunifieds_delete DELETE ANY ANY /api/drug_unifieds/{id}
api_drugunifieds_remove_drug PUT ANY ANY /api/drug_unifieds/{id}/drugs/delete
api_entrypoint ANY ANY ANY /api/{index}.{_format}
api_doc ANY ANY ANY /api/docs.{_format}
api_jsonld_context ANY ANY ANY /api/contexts/{shortName}.{_format}
api_doctors_get_collection GET ANY ANY /api/doctors.{_format}
api_doctors_post_collection POST ANY ANY /api/doctors.{_format}
api_doctors_get_item GET ANY ANY /api/doctors/{id}.{_format}
api_doctors_put_item PUT ANY ANY /api/doctors/{id}.{_format}
api_doctors_delete_item DELETE ANY ANY /api/doctors/{id}.{_format}
api_drugs_get_collection GET ANY ANY /api/drugs.{_format}
api_drugs_post_collection POST ANY ANY /api/drugs.{_format}
api_drugs_get_item GET ANY ANY /api/drugs/{id}.{_format}
api_drugs_put_item PUT ANY ANY /api/drugs/{id}.{_format}
api_drugs_delete_item DELETE ANY ANY /api/drugs/{id}.{_format}
api_assortments_get_collection GET ANY ANY /api/assortments.{_format}
api_assortments_post_collection POST ANY ANY /api/assortments.{_format}
api_assortments_get_item GET ANY ANY /api/assortments/{id}.{_format}
api_assortments_put_item PUT ANY ANY /api/assortments/{id}.{_format}
api_assortments_delete_item DELETE ANY ANY /api/assortments/{id}.{_format}
api_drugstores_get_collection GET ANY ANY /api/drugstores.{_format}
api_drugstores_post_collection POST ANY ANY /api/drugstores.{_format}
api_drugstores_get_item GET ANY ANY /api/drugstores/{id}.{_format}
api_drugstores_put_item PUT ANY ANY /api/drugstores/{id}.{_format}
api_drugstores_delete_item DELETE ANY ANY /api/drugstores/{id}.{_format}
api_employees_get_collection GET ANY ANY /api/employees.{_format}
api_employees_post_collection POST ANY ANY /api/employees.{_format}
api_employees_get_item GET ANY ANY /api/employees/{id}.{_format}
api_employees_put_item PUT ANY ANY /api/employees/{id}.{_format}
api_employees_delete_item DELETE ANY ANY /api/employees/{id}.{_format}
api_drug_sales_get_collection GET ANY ANY /api/drug_sales.{_format}
api_drug_sales_post_collection POST ANY ANY /api/drug_sales.{_format}
api_drug_sales_get_item GET ANY ANY /api/drug_sales/{id}.{_format}
api_drug_sales_put_item PUT ANY ANY /api/drug_sales/{id}.{_format}
api_drug_sales_delete_item DELETE ANY ANY /api/drug_sales/{id}.{_format}
api_drug_unifieds_get_collection GET ANY ANY /api/drug_unifieds.{_format}
api_drug_unifieds_post_collection POST ANY ANY /api/drug_unifieds.{_format}
api_drug_unifieds_get_item GET ANY ANY /api/drug_unifieds/{id}.{_format}
api_drug_unifieds_put_item PUT ANY ANY /api/drug_unifieds/{id}.{_format} `
Also I wanted to make additional action for method 'Delete', but it always call default action instead, so I needed to create PUT actions: api_drugunifieds_remove_drug PUT ANY ANY /api/drug_unifieds/{id}/drugs/delete
Any idea, why always build in delete action is called for all Delete method actions?