gpt4 book ai didi

symfony - 使用 Doctrine 和 Symfony2 进行数据库搜索

转载 作者:行者123 更新时间:2023-12-04 23:26:52 26 4
gpt4 key购买 nike

所以我目前正在尝试使用 Symfony2 和 Doctrine 执行简单的搜索。类似的东西:http://docs.doctrine-project.org/projects/doctrine1/en/latest/en/manual/searching.html

我目前有以下 YAML文件设置以生成我的实体。它生成我的 class Style实体正确地作为一个类。

...\Style:
type: entity
table: styles
id:
id:
type: integer
generator:
strategy: IDENTITY
actAs:
Searchable:
fields: [title]
batchUpdates: true
fields:
title:
type: string
length: 150
unique: true

在我的 Controller 中,我试图根据字符串对该表进行搜索。
public function searchAction($pattern) 
{
$repository = $this->getDoctrine()->getRepository('..:Style');
$search = $repository->search($pattern);

return $this->outputize($search);
}

但是,当我尝试执行代码时,出现以下异常。
Undefined method 'search'. The method name must start with either findBy or findOneBy!

我是否正确生成了我的实体,还是我明显遗漏了什么?

附带说明一下,当我查看我的 Entity/Style.php 时生成后,没有明确的方法 ->search() ,这里的函数应该是由Symfony生成的吗?

最佳答案

search()不是 Symfony2 支持的功能。您正在查看 Symfony 1.x 文档,Symfony2 与 Symfony 1.x 确实不同,因此作为引用,您应该始终使用 the doc .

在 Symfony2 中有几种获取实体的方法。这里有一些例子:


  • $user = $this->getDoctrine()
    ->getRepository('UserBundle:User')
    ->find($user_id)
    ;
  • 数据质量:
    $query = $em->createQuery(
    'SELECT b FROM YourBundle:Bid b WHERE b.property_id = :property_id ORDER BY b.amount DESC'
    )->setParameter('property_id', $property_id);
    try {
    $bids = $query->getResult();
    } catch (\Doctrine\Orm\NoResultException $e) {
    //Handle No Result Exception here
    }

  • 请参阅此处的 Symfony2 Doctrine 指南: http://symfony.com/doc/current/book/doctrine.html

    关于symfony - 使用 Doctrine 和 Symfony2 进行数据库搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11528435/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com