【问题标题】:Symfony advanced search with Ajax and jquerySymfony 使用 Ajax 和 jquery 进行高级搜索
【发布时间】:2021-06-09 20:05:44
【问题描述】:

我想使用 ajax 实现搜索,但是我一直遇到这个错误:

实体“App\Entity\Repas”没有字段“字符串”。因此,您不能 在实体的存储库上调用“findBystring”。

我尝试更改函数的名称,但它说它应该以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>

【问题讨论】:

    标签: jquery ajax symfony search ajaxform


    【解决方案1】:

    这是一个错字/大写错误!

    您在存储库中定义了一个名为 findByString() 的函数,在您的控制器中,您将其称为 findBystring()

    在您的控制器内部更改:

    $repas = $repository->findBystring($requestString);
    

    收件人:

    $repas = $repository->findByString($requestString);
    

    【讨论】:

      【解决方案2】:

      未调用您的存储库 RepasRepository,因此无法识别您的功能。这是您的动作主体的更正:

      $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);
      

      【讨论】:

        【解决方案3】:

        尝试使用findBy 和这样的语法:

        $repository->findBy(['nom'=> $requestString]);
        

        那个 hovewer 将只返回与字符串完全匹配的实体。

        您是否尝试过重命名存储库方法?

        【讨论】:

        • 不,实际上我还是 symfony 的新手,我在理解一切方面遇到了一些问题
        • 好的,我的意思是将您在此处使用的 findByString 方法重命名为 findRepasByNameLike 之类的其他名称(只是一个示例),但我认为下面的答案是正确的,您有一个控制器代码中的拼写错误。
        猜你喜欢
        • 2017-06-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-10
        • 2021-10-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多