मैं एक सिम्फनी प्रोजेक्ट पर काम कर रहा हूं और मैं एक फॉर्म क्लास बनाना चाहता था। लेकिन जब मैं जेनरेट क्लास PropertyType
बनाता हूं, तो यह नहीं मिलता है, और मुझे संपादन विधि में एक त्रुटि है:
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()
]);
}
और संपत्ति वर्ग (इकाई):
<?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;
}
}
सिम्फनी के साथ उत्पन्न संपत्ति प्रकार वर्ग।
<?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,
]);
}
}
2 जवाब
मेरा मानना है कि ऐसा इसलिए है क्योंकि आपके गेटर्स मेल नहीं खाते हैं। तो इसके बजाय:
public function getPostalCode(): ?string
{
return $this->postal_code;
}
प्रयत्न:
public function getPostal_code(): ?string
{
return $this->postal_code;
}
अपने created_at
और अपने सेटर्स के लिए भी ऐसा ही करें।
ccoding Standard (CamelCase here) का उपयोग करने से आपका जीवन आसान हो जाएगा। तो postal_code
के बजाय postalCode
आदि...
मुझे एक समान समस्या का सामना करना पड़ रहा है जहां मैं अपनी इकाई में अंडरस्कोर का उपयोग कर रहा हूं लेकिन फॉर्म ऊंट केस संस्करण की तलाश में है (उदा: फॉर्म फर्स्टनाम की तलाश में है लेकिन इकाई में first_name है)।
मेरा मानना है कि यह सिम्फनी के नवीनतम संस्करण के साथ एक समस्या हो सकती है, यह देखते हुए कि आपका प्रश्न हाल ही में कैसे है और जिस प्रोजेक्ट पर मैं काम कर रहा हूं वह सिम्फनी 4.3 का उपयोग कर रहा है। मेरी सिम्फनी 4.2 परियोजना इकाई में अंडरस्कोर का उपयोग करती है और प्रपत्र निर्माता के साथ इस समस्या का अनुभव नहीं करती है। तो मैं एक कूबड़ पर जा रहा हूं और कहता हूं कि यह एक सिम्फनी मुद्दा है जिसमें कुछ हालिया बदलाव उन्होंने इस मामूली रिलीज में किए हैं। मैं गिट पर इस मुद्दे की रिपोर्ट करने पर ध्यान दूंगा।
मिश्रित मामले का उपयोग करने के लिए अपनी संस्थाओं को बदलना एक वैकल्पिक समाधान है; हालाँकि जब आप पहले से ही इकाइयाँ बना चुके होते हैं और संबंध बनाए जाते हैं तो यह किसी ऐसी चीज़ के लिए एक लंबा काम है, जो बस काम करना चाहिए।
संबंधित सवाल
नए सवाल
php
PHP एक व्यापक रूप से उपयोग किया जाता है, उच्च-स्तरीय, गतिशील, वस्तु-उन्मुख, और व्याख्या की गई स्क्रिप्टिंग भाषा मुख्य रूप से सर्वर-साइड वेब विकास के लिए डिज़ाइन की गई है। PHP भाषा के बारे में सवालों के लिए इस्तेमाल किया।