Symfony:从DB获取数据时出现问题

时间:2014-05-08 18:25:38

标签: symfony doctrine

我是Symfony的初学者,我必须用它开发一个网站,所以我创建了所有的功能,但我在搜索功能中被阻止了我必须根据输入的关键字从我的数据库中获取数据文本域。我创建了本段下面显示的所有代码,但是当我尝试搜索某些内容时,我收到此错误:

  

[语义错误]第0行,第14列附近   'Tunipharma \ PharmacieBundle \ Pharmacie':错误:类   'Tunipharma \ PharmacieBundle \ Pharmacie'没有定义。

这是控制器中的功能:

public function searchAction() {
//----------------------------------------------------------------------
// -- Recherche Pharmacie :
//----------------------------------------------------------------------
$sform = $this->createForm(new SearchPharmacieFormType());

$request = $this->get('request');
if ($request->getMethod() == 'POST') {
$sform->bind($request);

    if ($sform->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $data = $this->getRequest()->request->get('SearchPharmacieFormType');

        $pharmacie = $em->getRepository("PharmacieBundle:Pharmacie")-  
        >findPharmacieByParametres($data);

        return $this->render('Tunipharma:PharmacieBundle:Default:recherche.html.twig',    
        array('Pharmacies' => $pharmacie));
       }
       }
//----------------------------------------------------------------------

return $this->render('PharmacieBundle:Default:recherche.html.twig', array(
            'formSearchPharmacie' => $sform->createView(),
));
}

这是存储库类:

  namespace Tunipharma\PharmacieBundle\Entity;

  use Doctrine\ORM\EntityRepository;
   use Tunipharma\PharmacieBundle\Entity\Pharmacie;
  /**
 * PharmacieRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
  class PharmacieRepository extends EntityRepository
   {

    public function findPharmacieByParametres($search)
    {
        return $this->createQueryBuilder('p')
                    ->add('select','p')
                    ->add('from','Pharmacie p')
                    ->add('where','p.nom = :name')
                    ->setParameter('name',$search)
                    ->getQuery()
                    ->getResult();
    }

表格:

namespace Tunipharma\PharmacieBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class SearchPharmacieFormType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
                ->add('Pharmacie', 'search', array('required' => true))
        ;
    }

    public function getName() {

    }

   }

将显示表单和查询结果的块:

      <div class="body3">
       <div class="main zerogrid">


        <form action="{{ path('search_page') }}" method="POST">

                       {{ form_widget(formSearchPharmacie) }}
            <input type="submit"/>

          </form>



        <div id="googleMap"></div>


        <div class="pharmalist"></div> 



      </div>
    </div>

并谢谢:)

1 个答案:

答案 0 :(得分:0)

您可以像这样简化您的存储库:

public function findPharmacieByParametres($search)
{
    $qb = $this->createQueryBuilder('p');

    return $qb
        ->andWhere($qb->expr()->like('p.nom', ':name'))
        ->setParameter('name', $search)
        ->getQuery()
        ->getResult();
}

你的表格:

public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder
        ->add('search', 'text', array(
            'required' => true,
        ));
}

你的控制器:

public function searchAction()
{
    $sform = $this->createForm(new SearchPharmacieFormType());
    if ($sform->handleRequest($this->get('request'))->isValid()) {
        $em = $this->getDoctrine()->getManager();

        $search = $sform->get('search')->getData();
        $pharmacies = $em->getRepository('TunipharmaPharmacieBundle:Pharmacie')->findPharmacieByParametres($search);

        return $this->render('TunipharmaPharmacieBundle:Default:recherche.html.twig',
            array('Pharmacies' => $pharmacies)
        );
    }

    return $this->render('TunipharmaPharmacieBundle:Default:recherche.html.twig', array(
        'formSearchPharmacie' => $sform->createView(),
    ));
}