I am creating an api in Apigility and am using Doctrine as the database adapter.
In simple entities or Many2One I was able to do the insertion of the records without major problems, but now I have a problem in a Many2Many relationship.
The relationship in question would be between customers and vehicles, in case a customer may have multiple vehicles and one vehicle may have multiple customers as well
And in case the service that is doing the registration is the one of vehicles, therefore I will only register a new vehicle for the client and not a new client.
And so my Resource is like this:
VeiculosResource.php
class VeiculosResource extends AbstractResourceListener
{
/**
* \GenericResource
*/
protected $res;
/**
* \EntityManager
*/
protected $em;
/**
* Construct class
*
* @param $em | EntityManager
*/
public function __construct($em) {
$this->em = $em;
$this->res = new GenericResource('Oficina\V1\Entity\VeiculosEntity', $em);
}
/**
* Create a resource
*
* @param mixed $data
* @return ApiProblem|mixed
*/
public function create($data)
{
$data = (array) $data;
$resource = new VeiculosEntity($data);
$this->res->persist($resource);
$entityArray = $this->res->objectToArray($resource);
return new VeiculosCollection($entityArray);
}
}
And the method persist in the generic resource is thus:
/**
* Outsourced function of the same role of the Entity manager
*
* @param $entity | Object
* @see Doctrine\ORM\EntityManager;
*/
public function persist($entity){
$this->em->persist($entity);
$this->em->flush();
}
Good Morning,
I am creating an api in Apigility and am using Doctrine as the database adapter.
In simple entities or Many2One I was able to do the insertion of the records without major problems, but now I have a problem in a Many2Many relationship.
The relationship in question would be between customers and vehicles, in case a customer may have multiple vehicles and one vehicle may have multiple customers as well
My entities are like this:
ClienteEntity.php
VeiculosEntity.php
And in case the service that is doing the registration is the one of vehicles, therefore I will only register a new vehicle for the client and not a new client.
And so my Resource is like this:
VeiculosResource.php
And the method persist in the generic resource is thus:
I'm passing the following JSON in postman as test
*Other Vehicle Attributes (unrelated)
After execution the insertion is occurring only in the table of 'veiculos' and not in the intermediary table 'clientes_veiculos'
And as I see there is no error in the code, maybe not json I am sending