After selecting a user, I am attempting to display a list of contracts. To achieve this, I have written the following query:
/**
* @param $firstname
* @param $lastname
* @return mixed
* @throws DBALException
*/
public function getListPerUser($firstname, $lastname)
{
$em = $this->getEntityManager();
$query = '
SELECT clientname
FROM contact_end_client c
INNER JOIN client_contract cli ON cli.clientname_id = c.id AND cli.active
INNER JOIN user u ON u.id = cli.user_id
WHERE u.firstname = :firstname AND u.lastname = :lastname AND cli.active = 1
';
$stmt = $em->getConnection()->prepare($query);
$param = ['firstname' => $firstname, 'lastname' => $lastname];
$stmt->execute($param);
return $stmt->fetchAll();
}
Below is my controller code:
/**
* @Route(path="/newadmin/invoice/showAllContract", name="showAllContract")
* @param Request $request
* @return JsonResponse
*/
public function viewContract(Request $request)
{
$entityManager = $this->getDoctrine()->getManager();
$template_id = $request->get('user');
//dump($template_id);
$getName = explode(" ", $template_id);
$firstname = $getName[0];
$lastname = $getName[1];
$templateRepository = $entityManager->getRepository(ClientContract::class)->getListPerUser($firstname, $lastname);
return new JsonResponse($templateRepository);
}
The above code will return a JSON response. To iterate over it and display in Twig, refer to the following snippet:
{{ form_start(createInvoice) }}
{{ form_row(createInvoice.user) }}
{# your contract #}
{{ form_end(createInvoice) }}
</div>
</div>
</div>
<script>
$(document).ready(function () {
$('#invoice_manual_creation_user').change(function (message) {
$('#hidden').show();
let userName = $('#invoice_manual_creation_user option:selected').text();
console.log(userName)
$.get("{{ path('showAllContract') }}", {'user': userName})
});
});
</script>