使用 Ajax 和 jquery 的 Symfony 高级搜索
Symfony advanced search with Ajax and jquery
我想使用 ajax 执行搜索,但是,我一直遇到这个错误:
Entity 'App\Entity\Repas' has no field 'string'. You can therefore not
call 'findBystring' on the entities' repository.
我尝试更改函数的名称,但它说它应该以 findBy
开头,老实说,我不知道还有什么地方可以看。
控制器代码:
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 功能:
public function findByString($nom){
return $this->createQueryBuilder('repas')
->where('repas.nom like :nom')
->setParameter('nom', '%'.$nom.'%')
->getQuery()
->getResult();
}
树枝代码:
<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代码:
<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>
尝试使用 findBy
这样的语法:
$repository->findBy(['nom'=> $requestString]);
那个 hovewer 将 return 只有与字符串完全匹配的实体。
您是否尝试过重命名存储库方法?
这是一个 typo/Caps 错误!
您在存储库中定义了一个名为 findByString()
的函数,在您的控制器中,您称它为 findBystring()
在您的控制器中更改:
$repas = $repository->findBystring($requestString);
收件人:
$repas = $repository->findByString($requestString);
您的存储库 RepasRepository 未被调用,因此您的功能无法识别。这是对你的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);
我想使用 ajax 执行搜索,但是,我一直遇到这个错误:
Entity 'App\Entity\Repas' has no field 'string'. You can therefore not call 'findBystring' on the entities' repository.
我尝试更改函数的名称,但它说它应该以 findBy
开头,老实说,我不知道还有什么地方可以看。
控制器代码:
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 功能:
public function findByString($nom){
return $this->createQueryBuilder('repas')
->where('repas.nom like :nom')
->setParameter('nom', '%'.$nom.'%')
->getQuery()
->getResult();
}
树枝代码:
<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代码:
<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>
尝试使用 findBy
这样的语法:
$repository->findBy(['nom'=> $requestString]);
那个 hovewer 将 return 只有与字符串完全匹配的实体。
您是否尝试过重命名存储库方法?
这是一个 typo/Caps 错误!
您在存储库中定义了一个名为 findByString()
的函数,在您的控制器中,您称它为 findBystring()
在您的控制器中更改:
$repas = $repository->findBystring($requestString);
收件人:
$repas = $repository->findByString($requestString);
您的存储库 RepasRepository 未被调用,因此您的功能无法识别。这是对你的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);