Reputation: 67
I want to implement a search using ajax however, I keep facing this error:
Entity 'App\Entity\Repas' has no field 'string'. You can therefore not call 'findBystring' on the entities' repository.
I tried changing the name of the Function but it says that it should start by findBy
, I don't know where else to look honestly.
Controller code:
use App\Entity\Repas;
use App\Form\RepasType;
use App\Repository\RepasRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\DependencyInjection\ContainerInterface;
public function searchAction(Request $request)
{
$repository = $this->getDoctrine()->getRepository(repas::class);
$requestString= $request->get('searchValue');
$repas = $repository->findBystring($requestString);
$jsonContent = $Normalizer->normalize($repas, 'json',['groups'=>'repas']);
$retour=json_encode($jsonContent);
return new Response($retour);
}
Doctrine Repository function:
public function findByString($nom){
return $this->createQueryBuilder('repas')
->where('repas.nom like :nom')
->setParameter('nom', '%'.$nom.'%')
->getQuery()
->getResult();
}
Twig code :
<h1 id="dd1"> List</h1>
</br>
<div style="margin-right:50px;" class="btn btn-primary">
<a href="#"> Add</a>
</div>
</div>
<input type="text" id="search" class="form-control" placeholder="Search">
<div>
<table border="1" id="t" class="table table-hover table-dark">
<thead class="thead-dark">
<tr>
<td>ID</td>
<td>Nom</td>
<td>desc</td>
</tr>
</thead>
<tbody id="all">
{% for repa in repas %}
<tr>
<td>
{{ repa.id }}
</td>
<td>
{{ repa.nom }}
</td>
<td>
{{ repa.desc }}
</td>
</tr>
{% endfor %}
</tbody>
<tbody id="search"></tbody>
</table>
</div>
Javascript code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#search").keyup(function(e) {
var value = $(this).val();
$.ajax({
url: "{{ path('list') }}",
type: 'GET',
data: {
'searchValue': value
},
success: function(retour) {
if (retour) {
$('#t tbody#search').empty();
$.each(JSON.parse(retour), function(i, obj) {
$('#t tbody#all').hide();
$('#t tbody#search').append('<tr><td> ' + obj.id + ' </td><td> ' + obj.nom ' </td><td>' + obj.desc + ' </td><td><a href="#/' + obj.id + '">update</a> </br><a href="#/' + obj.id + '">Delete</a></td></tr>');
});
} else {
$('#t tbody#all').show();
$('#t tbody#search').empty();
$('#t tbody#search').fadeIn('fast');
}
},
});
return false;
});
});
</script>
Upvotes: 0
Views: 453
Reputation: 11
You repository RepasRepository is not called, so you function is not recognized. This is the correction of your action body:
$repository = $this->getDoctrine()->getRepository(RepasRepository::class); // repa => RepasRepository
$requestString= $request->get('searchValue');
$repas = $repository->findByString($requestString); // findBystring => findByString
$jsonContent = $Normalizer->normalize($repas, 'json',['groups'=>'repas']);
$retour=json_encode($jsonContent);
return new Response($retour);
Upvotes: 1
Reputation: 1289
This is a typo/Caps error!
You defined a function inside your repository called findByString()
, and inside your controller, you are calling it findBystring()
Inside your controller Change:
$repas = $repository->findBystring($requestString);
To:
$repas = $repository->findByString($requestString);
Upvotes: 2
Reputation: 133
Try to use findBy
with such syntax:
$repository->findBy(['nom'=> $requestString]);
That hovewer will return only entities matching the string exactly.
Have you tried just renaming the repository method?
Upvotes: 0