Symfony2:无法在搜索表单中指定字符串

时间:2017-04-24 09:04:28

标签: string forms symfony search

我试图根据客户公司名称创建搜索表单。

我的客户实体

<?php

namespace ProjectBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Intl\Intl;

/**
 * Client
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="ProjectBundle\Entity\CustomerRepository")
 */
 class Customer {

/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="company_name", type="string", length=255)
 */
private $companyName;

/**
 * @var string
 *
 * @ORM\Column(name="contact_name", type="string", length=127)
 */
private $contactName;

/**
 * @var string
 *
 * @ORM\Column(name="contact_function", type="string", length=127)
 */
private $contactFunction;

/**
 * @var string
 *
 * @ORM\Column(name="contact_phone", type="string", length=64)
 */
private $contactPhone;

/**
 * @var string
 *
 * @ORM\Column(name="contact_mobile_phone", type="string", length=64)
 */
private $contactMobilePhone;

/**
 * @var string
 *
 * @ORM\Column(name="town", type="string", length=255)
 */
private $town;

/**
 * @var string
 *
 * @ORM\Column(name="country", type="string", length=2)
 */
private $country;

/**
 * @var boolean
 *
 * @ORM\Column(name="actif", type="boolean")
 */
private $actif;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="created_at", type="datetime")
 */
private $createdAt;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="updated_at", type="datetime")
 */
private $updatedAt;

public function __construct() {
    // Par défaut, la date de l'annonce est la date d'aujourd'hui
    $this->createdAt = new \Datetime();
    $this->actif = true;
}

/**
 * Get id
 *
 * @return integer
 */
public function getId() {
    return $this->id;
}

/**
 * Set companyName
 *
 * @param string $companyName
 *
 * @return Client
 */
public function setCompanyName($companyName) {
    $this->updatedAt = new \DateTime();
    $this->companyName = $companyName;

    return $this;
}

/**
 * Get companyName
 *
 * @return string
 */
public function getCompanyName() {
    return $this->companyName;
}

/**
 * Set contactName
 *
 * @param string $contactName
 *
 * @return Client
 */
public function setContactName($contactName) {
    $this->contactName = $contactName;

    return $this;
}

/**
 * Get contactName
 *
 * @return string
 */
public function getContactName() {
    return $this->contactName;
}

/**
 * Set contactPhone
 *
 * @param string $contactPhone
 *
 * @return Client
 */
public function setContactPhone($contactPhone) {
    $this->contactPhone = $contactPhone;

    return $this;
}

/**
 * Get contactPhone
 *
 * @return string
 */
public function getContactPhone() {
    return $this->contactPhone;
}

/**
 * Set contactMobilePhone
 *
 * @param string $contactMobilePhone
 *
 * @return Client
 */
public function setContactMobilePhone($contactMobilePhone) {
    $this->contactMobilePhone = $contactMobilePhone;

    return $this;
}

/**
 * Get contactMobilePhone
 *
 * @return string
 */
public function getContactMobilePhone() {
    return $this->contactMobilePhone;
}

/**
 * Set contactFunction
 *
 * @param string $contactFunction
 *
 * @return Client
 */
public function setContactFunction($contactFunction) {
    $this->contactFunction = $contactFunction;

    return $this;
}

/**
 * Get contactFunction
 *
 * @return string
 */
public function getContactFunction() {
    return $this->contactFunction;
}

/**
 * Set town
 *
 * @param string $town
 *
 * @return Client
 */
public function setTown($town) {
    $this->town = $town;

    return $this;
}

/**
 * Get town
 *
 * @return string
 */
public function getTown() {
    return $this->town;
}

/**
 * Set country
 *
 * @param string $country
 *
 * @return Client
 */
public function setCountry($country) {
    $this->country = $country;

    return $this;
}

/**
 * Get country
 *
 * @return string
 */
public function getCountry() {
    return $this->country;
}

/**
 * Set actif
 *
 * @param boolean $actif
 *
 * @return Client
 */
public function setActif($actif) {
    $this->actif = $actif;
}

/**
 * Get actif
 *
 * @return boolean
 */
public function getActif() {
    return $this->actif;
}

/**
 * Set createdAt
 *
 * @param \DateTime $createdAt
 *
 * @return Client
 */
public function setCreatedAt($createdAt) {
    $this->createdAt = $createdAt;

    return $this;
}

/**
 * Get createdAt
 *
 * @return \DateTime
 */
public function getCreatedAt() {
    return $this->createdAt;
}

/**
 * Set updatedAt
 *
 * @param \DateTime $updatedAt
 *
 * @return Client
 */
public function setUpdatedAt($updatedAt) {
    $this->updatedAt = $updatedAt;

    return $this;
}

/**
 * Get updatedAt
 *
 * @return \DateTime
 */
public function getUpdatedAt() {
    return $this->updatedAt;
}

public function getCountryName() {
    return Intl::getRegionBundle()->getCountryName($this->getCountry());
}

public function __toString() {
    return $this->getCompanyName();
}

}

我的项目实体

<?php

 namespace ProjectBundle\Entity;

 use Doctrine\ORM\Mapping as ORM;
 use Doctrine\Common\Collections\ArrayCollection;
 use Symfony\Component\Intl\Intl;
 use ProjectBundle\Entity\Customer;
 use ProjectBundle\Entity\Construction;
 use ProjectBundle\Entity\ProjectState as State;

 /**
  * Project
  *
  * @ORM\Table()
  * @ORM\Entity(repositoryClass="ProjectBundle\Entity\ProjectRepository")
  */
 class Project {

/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=255)
 */
private $name;

/**
 * @ORM\ManyToOne(targetEntity="\ProjectBundle\Entity\Customer") 
 */
private $customer;

/**
 * @var string
 *
 * @ORM\Column(name="project_code", type="string", length=127)
 */
private $projectCode;

/**
 * @var string
 *
 * @ORM\Column(name="location", type="string", length=255)
 */
private $location;

/**
 * @var string
 *
 * @ORM\Column(name="country", type="string", length=2)
 */
private $country;

/**
 * @ORM\OneToMany(targetEntity="\ProjectBundle\Entity\Construction", mappedBy="project")
 * @ORM\JoinColumn(name="project_id", referencedColumnName="id", nullable=true) 
 */
private $constructions;

/**
 * @ORM\ManyToOne(targetEntity="\ProjectBundle\Entity\ProjectState", inversedBy="code") 
 * @ORM\JoinColumn(name="state_code", referencedColumnName="code")
 */
private $state;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="created_at", type="datetime")
 */
private $createdAt;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="updated_at", type="datetime")
 */
private $updatedAt;

public function __construct() {
    // Par défaut, la date de l'annonce est la date d'aujourd'hui
    $this->createdAt = new \Datetime();
    $this->constructions = new ArrayCollection();
}

/**
 * Get id
 *
 * @return integer
 */
public function getId() {
    return $this->id;
}

/**
 * Set name
 *
 * @param string $name
 *
 * @return Project
 */
public function setName($name) {
    $this->updatedAt = new \DateTime();
    $this->name = $name;

    return $this;
}

/**
 * Get name
 *
 * @return string
 */
public function getName() {
    return $this->name;
}

/**
 * Set customer
 *
 * @param string $customer
 *
 * @return Project
 */
public function setCustomer(Customer $customer) {
    $this->customer = $customer;

    return $this;
}

/**
 * Get gamme
 *
 * @return Customer
 */
public function getCustomer() {
    return $this->customer;
}

/**
 * Set projectCode
 *
 * @param string $projectCode
 *
 * @return Project
 */
public function setProjectCode($projectCode) {
    $this->projectCode = $projectCode;

    return $this;
}

/**
 * Get projectCode
 *
 * @return string
 */
public function getProjectCode() {
    return $this->projectCode;
}

/**
 * Set location
 *
 * @param string $location
 *
 * @return Location
 */
public function setLocation($location) {
    $this->location = $location;

    return $this;
}

/**
 * Get location
 *
 * @return string
 */
public function getLocation() {
    return $this->location;
}

/**
 * Set country
 *
 * @param string $country
 *
 * @return Project
 */
public function setCountry($country) {
    $this->country = $country;

    return $this;
}

/**
 * Get country
 *
 * @return string
 */
public function getCountry() {
    return $this->country;
}

public function addConstruction(Construction $construction) {
    $this->constructions[] = $construction;

    return $this;
}

public function removeConstruction(Construction $construction) {
    $this->constructions->removeElement($construction);
}

/**
 * @return Collection
 */
public function getConstructions() {
    return $this->constructions;
}

    /**
 * Set state
 *
 * @param smallint $state
 *
 * @return Project
 */
public function setState(State $state) {
    $this->state = $state;

    return $this;
}

/**
 * Get state
 *
 * @return State
 */
public function getState() {
    return $this->state;
}

/**
 * Set createdAt
 *
 * @param \DateTime $createdAt
 *
 * @return Project
 */
public function setCreatedAt($createdAt) {
    $this->createdAt = $createdAt;

    return $this;
}

/**
 * Get createdAt
 *
 * @return \DateTime
 */
public function getCreatedAt() {
    return $this->createdAt;
}

/**
 * Set updatedAt
 *
 * @param \DateTime $updatedAt
 *
 * @return Project
 */
public function setUpdatedAt($updatedAt) {
    $this->updatedAt = $updatedAt;

    return $this;
}

/**
 * Get updatedAt
 *
 * @return \DateTime
 */
public function getUpdatedAt() {
    return $this->updatedAt;
}

public function getCountryName() {
    return Intl::getRegionBundle()->getCountryName($this->getCountry());
}

    public function __toString() {
    return $this->getName();
}

}

formType

<?php

 namespace ProjectBundle\Form;

 use Symfony\Component\Form\AbstractType;
 use Symfony\Component\Form\FormBuilderInterface;
 use Symfony\Component\OptionsResolver\OptionsResolverInterface;
 use Symfony\Component\Form\Extension\Core\Type\TextType;
 use ProjectBundle\Entity\ProjectStateRepository;

class ProjectSearchType extends AbstractType {

/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options) {


    $builder
            ->add('companyName', TextType::class, array('required' => false))
            ->add('state', 'entity', array(
                'query_builder' => function(ProjectStateRepository $repository) {
                    return $repository->createQueryBuilder('s')->orderBy('s.position', 'ASC');
                },
                'class' => 'ProjectBundle:ProjectState',
                'property' => 'state',
                'placeholder' => 'Etat',
                'required' => false
                    )
            )
            ->add('Filtrer', 'submit')
    ;
}

/**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver) {
    $resolver->setDefaults(array(
        'data_class' => 'ProjectBundle\Entity\Project'
    ));
}

/**
 * @return string
 */
public function getName() {
    return 'projectbundle_project_search';
}

}

我的控制器中的表单调用:

$form = $this->get('form.factory')->create(new ProjectSearchType());

项目回购的摘录:

     public function findProjectByParameters($data) {
    $query = $this->createQueryBuilder('p');

    if ($data['companyName']) {
        $query->innerJoin("p.customer", "c", "WITH", 'c.companyName LIKE :companyName')
                ->setParameter('companyName', '%' . $data['companyName'] . '%');
    }

    if ($data['state']) {
        $query->andWhere('p.state LIKE :state')
                ->setParameter('state', $data['state']);
    }



    $query->orderBy('p.name', 'ASC');

    return $query->getQuery()->getResult();
}

当我启动搜索时发出此错误:

  

财产&#34; companyName&#34;也不是其中一种方法&#34; getCompanyName()&#34;,&#34; companyName()&#34;,&#34; isCompanyName()&#34;,&#34; hasCompanyName()&# 34;,&#34; __ get()&#34;在课堂上存在且具有公共访问权限&#34; ProjectBundle \ Entity \ Project&#34;。

我不明白为什么它会向项目实体提供帮助......

我想得到一个字符串,并在合资企业的Customer实体的compagnyName中查找...

我不知道如何传递一个不应该被认为是项目属性的论据......

2 个答案:

答案 0 :(得分:2)

乍一看,我会说...删除此方法,可能工作。

/**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver) {
    $resolver->setDefaults(array(
        'data_class' => 'ProjectBundle\Entity\Project'
    ));
}

您告诉您的表单类型它代表Project实体。喜欢创建/编辑项目。这不是你想要的。您希望表单独立于您的实体操作,然后使用表单输入过滤结果集。

如果您想设置其他选项,您当然需要setDefaultOptions方法。所以至少要删除'data_class' => 'ProjectBundle\Entity\Project'

答案 1 :(得分:1)

抛出该错误是因为您的表单类有一个名为&#34; companyName&#34;的字段。同时与#34; Project&#34;数据类,不包含这样的getter / setter。

如果您需要添加未写入对象类的字段,则可以设置&#34;映射&#34;选项为false:

->add('companyName', TextType::class, array('mapped' => false, 'required' => false))

或者,如果您的表单根本不需要数据类,则只需从表单的默认选项中删除data_class属性。