在 symfony 上构建表单,属性 不存在
Build form on symfony, property does not exist
我正在做一个 symfony 项目,我想创建一个表单 class。但是我创建generate class PropertyType
的时候没有找到,而且我的edit方法报错:
Property "postalCode" does not exist in class "App\Entity\Property"
我的代码:
/**
* @Route("/admin/{id}", name="admin.property.edit")
* @param Property $property
* @return Response
*/
public function edit (Property $property) : Response
{
$form = $this->createForm(PropertyType::class, $property);
return $this->render('admin/property/edit.html.twig', [
'property' => $property,
'form' => $form->createView()
]);
}
和 属性 class(实体):
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Cocur\Slugify\Slugify;
/**
* @ORM\Entity(repositoryClass="App\Repository\PropertyRepository")
*/
class Property
{
const HEAT = [
0 => 'Electrique',
1 => 'Gaz'
];
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $description;
/**
* @ORM\Column(type="integer")
*/
private $surface;
/**
* @ORM\Column(type="integer")
*/
private $rooms;
/**
* @ORM\Column(type="integer")
*/
private $bedrooms;
/**
* @ORM\Column(type="integer")
*/
private $floor;
/**
* @ORM\Column(type="integer")
*/
private $price;
/**
* @ORM\Column(type="integer")
*/
private $heat;
/**
* @ORM\Column(type="string", length=255)
*/
private $city;
/**
* @ORM\Column(type="string", length=255)
*/
private $address;
/**
* @ORM\Column(type="string", length=255)
*/
private $postal_code;
/**
* @ORM\Column(type="boolean", options={"default": false})
*/
private $sold = false;
/**
* @ORM\Column(type="datetime")
*/
private $created_at;
public function __construct ()
{
$this->created_at = new \DateTime();
}
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function getSlug() : string
{
return (new Slugify())->slugify($this->title);
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getSurface(): ?int
{
return $this->surface;
}
public function setSurface(int $surface): self
{
$this->surface = $surface;
return $this;
}
public function getRooms(): ?int
{
return $this->rooms;
}
public function setRooms(int $rooms): self
{
$this->rooms = $rooms;
return $this;
}
public function getBedrooms(): ?int
{
return $this->bedrooms;
}
public function setBedrooms(int $bedrooms): self
{
$this->bedrooms = $bedrooms;
return $this;
}
public function getFloor(): ?int
{
return $this->floor;
}
public function setFloor(int $floor): self
{
$this->floor = $floor;
return $this;
}
public function getPrice(): ?int
{
return $this->price;
}
public function getFormattedPrice() : string
{
return \number_format($this->price, 0, '', ' ');
}
public function setPrice(int $price): self
{
$this->price = $price;
return $this;
}
public function getHeat(): ?int
{
return $this->heat;
}
public function getHeatType(): string
{
return self::HEAT[$this->heat];
}
public function setHeat(int $heat): self
{
$this->heat = $heat;
return $this;
}
public function getCity(): ?string
{
return $this->city;
}
public function setCity(string $city): self
{
$this->city = $city;
return $this;
}
public function getAddress(): ?string
{
return $this->address;
}
public function setAddress(string $address): self
{
$this->address = $address;
return $this;
}
public function getPostalCode(): ?string
{
return $this->postal_code;
}
public function setPostalCode(string $postal_code): self
{
$this->postal_code = $postal_code;
return $this;
}
public function getSold(): ?bool
{
return $this->sold;
}
public function setSold(bool $sold): self
{
$this->sold = $sold;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->created_at;
}
public function setCreatedAt(\DateTimeInterface $created_at): self
{
$this->created_at = $created_at;
return $this;
}
}
属性类型 class 用 symfony 生成。
<?php
namespace App\Form;
use App\Entity\Property;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class PropertyType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title')
->add('description')
->add('surface')
->add('rooms')
->add('bedrooms')
->add('floor')
->add('price')
->add('heat')
->add('city')
->add('address')
->add('postal_code')
->add('sold')
->add('created_at')
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Property::class,
]);
}
}
我认为这是因为您的 getter 不匹配。所以不是:
public function getPostalCode(): ?string
{
return $this->postal_code;
}
尝试:
public function getPostal_code(): ?string
{
return $this->postal_code;
}
对你的 created_at
和你的二传手做同样的事情。
使用 ccoding standard(此处为 CamelCase)将使您的生活更轻松。所以 postalCode
而不是 postal_code
等等...
我遇到了一个类似的问题,我在我的实体中使用了下划线,但表单正在寻找驼峰式大小写变体(例如:表单正在寻找 firstName 但实体有 first_name)。
我认为这可能是最新版本的 Symfony 的问题,因为您的问题是最近的,而且我正在处理的项目是如何使用 Symfony 4.3 的。我的 Symfony 4.2 项目在实体中使用了下划线,并且在表单生成器中没有遇到这个问题。所以我会直觉地说这是一个 Symfony 问题,他们最近在这个次要版本中做了一些更改。我将研究在 Git 上报告该问题。
将您的实体更改为使用大小写混合 work-around;但是,当您已经生成了实体并创建了关系时,对于应该正常工作的东西来说,这是一个冗长的 work-around。
我正在做一个 symfony 项目,我想创建一个表单 class。但是我创建generate class PropertyType
的时候没有找到,而且我的edit方法报错:
Property "postalCode" does not exist in class "App\Entity\Property"
我的代码:
/**
* @Route("/admin/{id}", name="admin.property.edit")
* @param Property $property
* @return Response
*/
public function edit (Property $property) : Response
{
$form = $this->createForm(PropertyType::class, $property);
return $this->render('admin/property/edit.html.twig', [
'property' => $property,
'form' => $form->createView()
]);
}
和 属性 class(实体):
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Cocur\Slugify\Slugify;
/**
* @ORM\Entity(repositoryClass="App\Repository\PropertyRepository")
*/
class Property
{
const HEAT = [
0 => 'Electrique',
1 => 'Gaz'
];
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $description;
/**
* @ORM\Column(type="integer")
*/
private $surface;
/**
* @ORM\Column(type="integer")
*/
private $rooms;
/**
* @ORM\Column(type="integer")
*/
private $bedrooms;
/**
* @ORM\Column(type="integer")
*/
private $floor;
/**
* @ORM\Column(type="integer")
*/
private $price;
/**
* @ORM\Column(type="integer")
*/
private $heat;
/**
* @ORM\Column(type="string", length=255)
*/
private $city;
/**
* @ORM\Column(type="string", length=255)
*/
private $address;
/**
* @ORM\Column(type="string", length=255)
*/
private $postal_code;
/**
* @ORM\Column(type="boolean", options={"default": false})
*/
private $sold = false;
/**
* @ORM\Column(type="datetime")
*/
private $created_at;
public function __construct ()
{
$this->created_at = new \DateTime();
}
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function getSlug() : string
{
return (new Slugify())->slugify($this->title);
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getSurface(): ?int
{
return $this->surface;
}
public function setSurface(int $surface): self
{
$this->surface = $surface;
return $this;
}
public function getRooms(): ?int
{
return $this->rooms;
}
public function setRooms(int $rooms): self
{
$this->rooms = $rooms;
return $this;
}
public function getBedrooms(): ?int
{
return $this->bedrooms;
}
public function setBedrooms(int $bedrooms): self
{
$this->bedrooms = $bedrooms;
return $this;
}
public function getFloor(): ?int
{
return $this->floor;
}
public function setFloor(int $floor): self
{
$this->floor = $floor;
return $this;
}
public function getPrice(): ?int
{
return $this->price;
}
public function getFormattedPrice() : string
{
return \number_format($this->price, 0, '', ' ');
}
public function setPrice(int $price): self
{
$this->price = $price;
return $this;
}
public function getHeat(): ?int
{
return $this->heat;
}
public function getHeatType(): string
{
return self::HEAT[$this->heat];
}
public function setHeat(int $heat): self
{
$this->heat = $heat;
return $this;
}
public function getCity(): ?string
{
return $this->city;
}
public function setCity(string $city): self
{
$this->city = $city;
return $this;
}
public function getAddress(): ?string
{
return $this->address;
}
public function setAddress(string $address): self
{
$this->address = $address;
return $this;
}
public function getPostalCode(): ?string
{
return $this->postal_code;
}
public function setPostalCode(string $postal_code): self
{
$this->postal_code = $postal_code;
return $this;
}
public function getSold(): ?bool
{
return $this->sold;
}
public function setSold(bool $sold): self
{
$this->sold = $sold;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->created_at;
}
public function setCreatedAt(\DateTimeInterface $created_at): self
{
$this->created_at = $created_at;
return $this;
}
}
属性类型 class 用 symfony 生成。
<?php
namespace App\Form;
use App\Entity\Property;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class PropertyType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title')
->add('description')
->add('surface')
->add('rooms')
->add('bedrooms')
->add('floor')
->add('price')
->add('heat')
->add('city')
->add('address')
->add('postal_code')
->add('sold')
->add('created_at')
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Property::class,
]);
}
}
我认为这是因为您的 getter 不匹配。所以不是:
public function getPostalCode(): ?string
{
return $this->postal_code;
}
尝试:
public function getPostal_code(): ?string
{
return $this->postal_code;
}
对你的 created_at
和你的二传手做同样的事情。
使用 ccoding standard(此处为 CamelCase)将使您的生活更轻松。所以 postalCode
而不是 postal_code
等等...
我遇到了一个类似的问题,我在我的实体中使用了下划线,但表单正在寻找驼峰式大小写变体(例如:表单正在寻找 firstName 但实体有 first_name)。
我认为这可能是最新版本的 Symfony 的问题,因为您的问题是最近的,而且我正在处理的项目是如何使用 Symfony 4.3 的。我的 Symfony 4.2 项目在实体中使用了下划线,并且在表单生成器中没有遇到这个问题。所以我会直觉地说这是一个 Symfony 问题,他们最近在这个次要版本中做了一些更改。我将研究在 Git 上报告该问题。
将您的实体更改为使用大小写混合 work-around;但是,当您已经生成了实体并创建了关系时,对于应该正常工作的东西来说,这是一个冗长的 work-around。