I want to implement a search using ajax however, I keep facing this error:
Entity ‘AppEntityRepas’ 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 AppEntityRepas;
use AppFormRepasType;
use AppRepositoryRepasRepository;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentFormExtensionCoreTypeSubmitType;
use SymfonyComponentRoutingAnnotationRoute;
use SensioBundleFrameworkExtraBundleConfigurationMethod;
use SymfonyComponentDependencyInjectionContainerInterface;
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>
3
Answers
Try to use
findBy
with such syntax:That hovewer will return only entities matching the string exactly.
Have you tried just renaming the repository method?
This is a typo/Caps error!
You defined a function inside your repository called
findByString()
, and inside your controller, you are calling itfindBystring()
Inside your controller Change:
To:
You repository RepasRepository is not called, so you function is not recognized. This is the correction of your action body: