Commit 07fced54 authored by Mat's avatar Mat

wip.. repository, querybuilder pour faire un ORDER BY FIELD(id,20,22,26..)

parent af73bfa8
......@@ -126,7 +126,7 @@ class OffreAPIController extends MasterAPIController
if ($form->isSubmitted() && $form->isValid())
{
$em->flush();
return $this->api($offre);
return $this->api([]);
}
return $this->api($form, Response::HTTP_BAD_REQUEST);
}
......
......@@ -9,11 +9,22 @@ class OffreLegumesAPIController extends MasterAPIController
/**
* @param $id
* @return \Symfony\Component\HttpFoundation\JsonResponse
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function readAction($id)
{
$em = $this->getDoctrine()->getManager();
$legumes = $em->getRepository('PotageBundle:OffreLegumes')->findAllForRead($id);
$offre = $em->getRepository('PotageBundle:Offre')->findOneForAPIRead($id);
// TODO utilise actuellement le repository d'une autre action !
if (count($offre->getSorted()) > 0)
{
$sorted = $offre->getSorted();
$legumes = $em->getRepository('PotageBundle:OffreLegumes')->findAllForSortedRead($id, $sorted);
}
else {
$legumes = $em->getRepository('PotageBundle:OffreLegumes')->findAllForRead($id);
}
return $this->api($legumes);
}
}
......@@ -328,12 +328,13 @@ class Offre
/**
* Set sorted
*
* @param array $sorted
* @param mixed $sorted
*
* @return Offre
*/
public function setSorted(array $sorted)
public function setSorted($sorted)
{
is_array($sorted) ? $sorted = explode(",", $sorted) : null;
$this->sorted = $sorted;
return $this;
......
......@@ -19,7 +19,7 @@ class OffreSortedType extends AbstractType
{
$builder->add('token', HiddenType::class, array(
$builder->add('sorted', HiddenType::class, array(
'data' => array(),
));
......
......@@ -51,4 +51,53 @@ class OffreLegumesRepository extends \Doctrine\ORM\EntityRepository
return $qb->getQuery()->getResult();
}
/**
* @param $id_offre
* @param $sorted
* @return array
*/
public function findAllForSortedRead($id_offre, $sorted)
{
/*
*
* SELECT id, id_offre, nom, reference, [...]
* FROM offre_legumes
* WHERE id_offre = 22
* ORDER BY FIELD(id, 46, 20, 45, 27, 22, 26)
*
* @param $id_offre = '22'
* @param $sorted = '20,26,22,27,45,46'
*
//1
$qb = $this->createQueryBuilder('ol')
->select(
'ol.id', 'ol.nom', 'ol.description', 'ol.origine', 'ol.image',
'ol.quantiteMin', 'ol.quantiteMax', 'ol.quantiteStep', 'ol.prixUnitaire', 'ol.unite',
':sorted AS HIDDEN field'
)
->where('ol.offre = :id_offre')
->orderBy('field')
->setParameters(array(
':id_offre' => $id_offre,
':sorted' => 'FIELD( ol.id, '. $sorted .')'
))
;
return $qb->getQuery()->getResult();
//2
return $this
->getEntityManager()
->createQuery("SELECT ol
FROM PotageBundle\Entity\OffreLegumes ol
WHERE ol.offre = :id_offre
ORDER BY FIELD(:sorted)"
)
->setParameters(array(
':id_offre' => $id_offre,
':sorted' => 'ol.id, '.$sorted
))
->getResult();
*/
}
}
......@@ -68,7 +68,7 @@ class OffreRepository extends \Doctrine\ORM\EntityRepository
public function findOneForSorted($id)
{
$qb = $this->createQueryBuilder('o')
->select('o.sorted')
//->select('o.sorted')
->where('o.id = :id')
->andWhere('o.status = :status ')
->setParameters(array(
......
......@@ -225,16 +225,30 @@
* Temporaire, le bouton sauvegarder affiche en console
* un tableau avec l'ordre souhaité
*/
let orderBtn = document.querySelector('.btn-order');
orderBtn.addEventListener('click', function()
let sortForm = document.querySelector('form[name=offre_sorted]');
sortForm.addEventListener('submit', function(e)
{
let list = [];
let items = document.querySelectorAll('.ui-state-default');
items.forEach(function(item) {
list.push(item.dataset.legumeId);
});
console.log(list);
let input = document.querySelector('input#offre_sorted_sorted');
input.value = list;
let form = this;
e.preventDefault();
AJAX(
'POST',
Routing.generate('api_offre_update_sorted', {'id': id_offre}),
function() {
alert('Enregistré');
},
form
);
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment