- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 Symfony 的新手。我关注了 couple的 tutorials尝试学习依赖选择,因此我使用 DestinationFormType 和 DestinationController 来保存 Destination。 JS 部分运行良好,但现在当我尝试保存实体时抛出下一个异常:
Argument 1 passed to Symfony\Bridge\Doctrine\Form\ChoiceList\IdReader::getIdValue() must be an object or null, int given, called in \vendor\symfony\form\ChoiceList\ArrayChoiceList.php on line 200
<?php
namespace App\Form;
use App\Entity\City;
use App\Entity\Country;
use App\Entity\Destination;
use App\Entity\DestinationCategory;
use App\Entity\DestinationSubcategory;
use App\Entity\Region;
use App\Entity\State;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class DestinationFormType extends AbstractType
{
/** @var EntityManagerInterface */
private $em;
/**
* @param EntityManagerInterface $em
*/
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('coordinates')
->add('kidsCost')
->add('adultsCost')
->add('description')
;
$builder->addEventListener(FormEvents::PRE_SET_DATA, array($this, 'onPreSetData'));
$builder->addEventListener(FormEvents::PRE_SUBMIT, array($this, 'onPreSubmit'));
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Destination::class
]);
}
/**
* @param FormEvent $event
*/
public function onPreSubmit(FormEvent $event) {
$form = $event->getForm();
$data = $event->getData();
/** @var Region $region */
$region = $this->em
->getRepository(Region::class)
->find($data['region']);
/** @var Country $country */
$country = $this->em
->getRepository(Country::class)
->find($data['country']);
/** @var State $state */
$state = $this->em
->getRepository(State::class)
->find($data['state']);
/** @var City $city */
$city = $this->em
->getRepository(City::class)
->find($data['city']);
/** @var DestinationCategory $city */
$category = $this->em
->getRepository(DestinationCategory::class)
->find($data['category']);
/** @var DestinationCategory $city */
$subcategory = $this->em
->getRepository(DestinationSubcategory::class)
->find($data['subcategory']);
$this->addLocationDropdowns($form, $region, $country, $state, $city);
$this->addCategoryDropdowns($form, $category, $subcategory);
}
/**
* @param FormEvent $event
*/
public function onPreSetData(FormEvent $event) {
/** @var Destination $destination */
$destination = $event->getData();
$form = $event->getForm();
$category = $destination && $destination->getCategory() ? $destination->getCategory() : null;
$subcategory = $destination && $destination->getSubcategory() ? $destination->getSubcategory() : null;
$region = $destination && $destination->getRegion() ? $destination->getRegion() : null;
$country = $destination && $destination->getCountry() ? $destination->getCountry() : null;
$state = $destination && $destination->getState() ? $destination->getState() : null;
$city = $destination && $destination->getCity() ? $destination->getCity() : null;
$this->addLocationDropdowns($form, $region, $country, $state, $city);
$this->addCategoryDropdowns($form, $category, $subcategory);
}
/**
* @param FormInterface $form
* @param Region|null $region
* @param Country|null $country
* @param State|null $state
* @param City|null $city
*/
private function addLocationDropdowns(FormInterface $form, ?Region $region, ?Country $country, ?State $state, ?City $city) {
$this->addRegionDropDown($form, $region);
$this->addCountryDropdown($form, $region, $country);
$this->addStateDropdown($form, $country, $state);
$this->addCityDropdown($form, $state, $city);
}
/**
* @param FormInterface $form
* @param Region|null $region
*/
private function addRegionDropDown(FormInterface $form, ?Region $region)
{
$form->add('region', EntityType::class,[
'required' => true,
'data' => $region,
'placeholder' => 'Select a Region...',
'class' => Region::class
]);
}
/**
* @param FormInterface $form
* @param Region|null $region
* @param Country|null $country
*/
private function addCountryDropdown(FormInterface $form, ?Region $region, ?Country $country)
{
$countries = array();
if ($region) {
$countryRepository = $this->em->getRepository(Country::class);
$countries = $countryRepository->findByRegionId($region->getId());
}
$form->add('country', EntityType::class, [
'required' => true,
'data' => $country,
'placeholder' => 'Select a Region first ...',
'class' => Country::class,
'choices' => $countries
]);
}
/**
* @param FormInterface $form
* @param Country|null $country
* @param State|null $state
*/
private function addStateDropdown(FormInterface $form, ?Country $country, ?State $state)
{
$states = array();
if ($country) {
$stateRepository = $this->em->getRepository(State::class);
$states = $stateRepository->findByCountryId($country->getId());
}
$form->add('state', EntityType::class, [
'required' => true,
'data' => $state,
'placeholder' => 'Select a Country first ...',
'class' => State::class,
'choices' => $states
]);
}
/**
* @param FormInterface $form
* @param State|null $state
* @param City|null $city
*/
private function addCityDropdown(FormInterface $form, ?State $state, ?City $city)
{
$cities = array();
if ($state) {
$cityRepository = $this->em->getRepository(City::class);
$cities = $cityRepository->findByStateId($state->getId());
}
$form->add('city', EntityType::class, [
'required' => true,
'data' => $city,
'placeholder' => 'Select a State first ...',
'class' => State::class,
'choices' => $cities
]);
}
/**
* @param FormInterface $form
* @param DestinationCategory|null $category
* @param DestinationSubcategory|null $subcategory
*/
private function addCategoryDropdowns(FormInterface $form, ?DestinationCategory $category, ?DestinationSubcategory $subcategory)
{
$this->addCategoryDropDown($form, $category);
$this->addSubcategoryDropDown($form, $category, $subcategory);
}
/**
* @param FormInterface $form
* @param DestinationCategory|null $category
*/
private function addCategoryDropDown(FormInterface $form, ?DestinationCategory $category)
{
$form->add('category', EntityType::class,[
'required' => true,
'data' => $category,
'placeholder' => 'Select a Category...',
'class' => DestinationCategory::class
]);
}
/**
* @param FormInterface $form
* @param DestinationCategory|null $category
* @param DestinationSubcategory|null $subcategory
*/
private function addSubcategoryDropDown(FormInterface $form, ?DestinationCategory $category, ?DestinationSubcategory $subcategory)
{
$subcategories = array();
if ($category) {
$countryRepository = $this->em->getRepository(DestinationSubcategory::class);
$subcategories = $countryRepository->findByCategoryId($category->getId());
}
$form->add('subcategory', EntityType::class, [
'required' => true,
'data' => $subcategory,
'placeholder' => 'Select a Category first ...',
'class' => DestinationSubcategory::class,
'choices' => $subcategories
]);
}
}
这是我的 DestinationController
<?php
namespace App\Controller;
use App\Entity\Destination;
use App\Form\DestinationFormType;
use App\Repository\DestinationRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class DestinationController extends AbstractController
{
/**
* @Route("admin/destination/new", name="admin_destination_new")
*
* @param EntityManagerInterface $em
* @param Request $request
*
* @return Response
*/
public function new(EntityManagerInterface $em, Request $request)
{
$form = $this->createForm(DestinationFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** @var Destination $destination */
$destination = $form->getData();
$em->persist($destination);
$em->flush();
$this->addFlash('success', 'Destination created');
$this->redirectToRoute('admin_destination_list');
}
return $this->render('destination/new.html.twig', [
'destForm' => $form->createView()
]);
}
/**
* @Route("admin/destination/{id}/edit", name="admin_destination_edit")
*
* @param Destination $destination
* @param EntityManagerInterface $em
* @param Request $request
*
* @return Response
*/
public function edit(Destination $destination, EntityManagerInterface $em, Request $request)
{
$form = $this->createForm(DestinationFormType::class, $destination);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em->persist($destination);
$em->flush();
$this->addFlash('success', 'Destiny updated');
$this->redirectToRoute('admin_destination_list');
}
return $this->render('destination/edit.html.twig', [
'destForm' => $form->createView()
]);
}
/**
* @Route("admin/destination", name="admin_destination_list")
* @param DestinationRepository $destRepo
*
* @return Response
*/
public function list(DestinationRepository $destRepo){
/** @var Destination $destinations */
$destinations = $destRepo->findAll();
return $this->render('destination/list.html.twig', [
'destinations' => $destinations
]);
}
}
这是我的目的地实体
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\SoftDeleteable\Traits\SoftDeleteableEntity;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* @ORM\Entity(repositoryClass="App\Repository\DestinationRepository")
*/
class Destination
{
use TimestampableEntity;
use SoftDeleteableEntity;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=150)
*/
private $name;
/**
* @ORM\Column(type="string", length=150, nullable=true)
*/
private $coordinates;
/**
* @ORM\Column(type="string", length=150, unique=true)
* @Gedmo\Slug(fields={"name"})
*/
private $slug;
/**
* @ORM\Column(type="decimal", precision=8, scale=2, nullable=true)
*/
private $kidsCost;
/**
* @ORM\Column(type="decimal", precision=8, scale=2, nullable=true)
*/
private $adultsCost;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Package", mappedBy="destinations")
*/
private $packages;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $description;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $address;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\City")
* @ORM\JoinColumn(nullable=false)
*/
private $city;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\State")
* @ORM\JoinColumn(nullable=false)
*/
private $state;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Region", inversedBy="destinations")
* @ORM\JoinColumn(nullable=false)
*/
private $region;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Country", inversedBy="destinations")
* @ORM\JoinColumn(nullable=false)
*/
private $country;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\DestinationCategory", inversedBy="destinations")
* @ORM\JoinColumn(nullable=false)
*/
private $category;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\DestinationSubcategory", inversedBy="destinations")
* @ORM\JoinColumn(nullable=false)
*/
private $subcategory;
public function __construct()
{
$this->packages = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getCoordinates(): ?string
{
return $this->coordinates;
}
public function setCoordinates(?string $coordinates): self
{
$this->coordinates = $coordinates;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
public function getKidsCost(): ?string
{
return $this->kidsCost;
}
public function setKidsCost(?string $kidsCost): self
{
$this->kidsCost = $kidsCost;
return $this;
}
public function getAdultsCost(): ?string
{
return $this->adultsCost;
}
public function setAdultsCost(?string $adultsCost): self
{
$this->adultsCost = $adultsCost;
return $this;
}
/**
* @return Collection|Package[]
*/
public function getPackages(): Collection
{
return $this->packages;
}
public function addPackage(Package $package): self
{
if (!$this->packages->contains($package)) {
$this->packages[] = $package;
$package->addDestination($this);
}
return $this;
}
public function removePackage(Package $package): self
{
if ($this->packages->contains($package)) {
$this->packages->removeElement($package);
$package->removeDestination($this);
}
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getAddress(): ?string
{
return $this->address;
}
public function setAddress(?string $address): self
{
$this->address = $address;
return $this;
}
public function getCity(): ?City
{
return $this->city;
}
public function getState(): ?State
{
return $this->getCity() ? $this->getCity()->getState() : null;
}
public function getCountry(): ?Country
{
return $this->getCity()? $this->getState()->getCountry() : null;
}
public function getRegion() : ?Region
{
return $this->getCity()? $this->getCountry()->getRegion() : null;
}
public function setCity(?City $city): self
{
$this->city = $city;
return $this;
}
public function setState(?State $state): self
{
$this->state = $state;
return $this;
}
public function setRegion(?Region $region): self
{
$this->region = $region;
return $this;
}
public function setCountry(?Country $country): self
{
$this->country = $country;
return $this;
}
public function getCategory(): ?DestinationCategory
{
return $this->category;
}
public function setCategory(?DestinationCategory $category): self
{
$this->category = $category;
return $this;
}
public function getSubcategory(): ?DestinationSubcategory
{
return $this->subcategory;
}
public function setSubcategory(?DestinationSubcategory $subcategory): self
{
$this->subcategory = $subcategory;
return $this;
}
}
有人可以指出我解决这个问题的正确方向吗?
最佳答案
好吧,通过查看我的代码,答案并不明显,因为当你看到
$countries = $countryRepository->findByRegionId($region->getId());
你可能认为是返回一个国家数组,但实际上它是 JS 的一个助手,它只返回 id 和 name
/**
* @param int $regionId
* @return array
*/
public function findByRegionId(int $regionId)
{
return $this->createQueryBuilder('c')
->select(['c.id', 'c.name'])
->where('c.region = :id')
->orderBy('c.name')
->setParameter('id', $regionId)
->getQuery()
->execute();
}
所以以防万一其他人为此而苦恼:$choices 需要一个对象数组,而不是一个带有 id 和 name 的数组,所以我像这样修改了我的 addCountryDropDown 方法
private function addCountryDropdown(FormInterface $form, ?Region $region, ?Country $country)
{
$countries = array();
if ($region) {
$countryRepository = $this->em->getRepository(Country::class);
$countries = $countryRepository->findBy([
'region' => $region->getId()
]);
}
$form->add('country', EntityType::class, [
'required' => true,
'data' => $country,
'placeholder' => 'Select a Region first ...',
'class' => Country::class,
'choices' => $countries
]);
}
关于php - 传递给 Symfony\Bridge\Doctrine\Form\ChoiceList\IdReader::getIdValue() 的参数 1 必须是一个对象或 null,int 给定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62649386/
简而言之:我想从可变参数模板参数中提取各种选项,但不仅通过标签而且通过那些参数的索引,这些参数是未知的 标签。我喜欢 boost 中的方法(例如 heap 或 lockfree 策略),但想让它与 S
我可以对单元格中的 excel IF 语句提供一些帮助吗? 它在做什么? 对“BaselineAmount”进行了哪些评估? =IF(BaselineAmount, (Variance/Baselin
我正在使用以下方法: public async Task Save(Foo foo,out int param) { ....... MySqlParameter prmparamID
我正在使用 CodeGear RAD Studio IDE。 为了使用命令行参数测试我的应用程序,我多次使用了“运行 -> 参数”菜单中的“参数”字段。 但是每次我给它提供一个新值时,它都无法从“下拉
我已经为信用卡类编写了一些代码,粘贴在下面。我有一个接受上述变量的构造函数,并且正在研究一些方法将这些变量格式化为字符串,以便最终输出将类似于 号码:1234 5678 9012 3456 截止日期:
MySql IN 参数 - 在存储过程中使用时,VarChar IN 参数 val 是否需要单引号? 我已经像平常一样创建了经典 ASP 代码,但我没有更新该列。 我需要引用 VarChar 参数吗?
给出了下面的开始,但似乎不知道如何完成它。本质上,如果我调用 myTest([one, Two, Three], 2); 它应该返回元素 third。必须使用for循环来找到我的解决方案。 funct
将 1113355579999 作为参数传递时,该值在函数内部变为 959050335。 调用(main.c): printf("%d\n", FindCommonDigit(111335557999
这个问题在这里已经有了答案: Is Java "pass-by-reference" or "pass-by-value"? (92 个回答) 关闭9年前。 public class StackOve
我真的很困惑,当像 1 == scanf("%lg", &entry) 交换为 scanf("%lg", &entry) == 1 没有区别。我的实验书上说的是前者,而我觉得后者是可以理解的。 1 =
我正在尝试使用调用 SetupDiGetDeviceRegistryProperty 的函数使用德尔福 7。该调用来自示例函数 SetupEnumAvailableComPorts .它看起来像这样:
我需要在现有项目上实现一些事件的显示。我无法更改数据库结构。 在我的 Controller 中,我(从 ajax 请求)传递了一个时间戳,并且我需要显示之前的 8 个事件。因此,如果时间戳是(转换后)
rails 新手。按照多态关联的教程,我遇到了这个以在create 和destroy 中设置@client。 @client = Client.find(params[:client_id] || p
通过将 VM 参数设置为 -Xmx1024m,我能够通过 Eclipse 运行 Java 程序-Xms256M。现在我想通过 Windows 中的 .bat 文件运行相同的 Java 程序 (jar)
我有一个 Delphi DLL,它在被 Delphi 应用程序调用时工作并导出声明为的方法: Procedure ProduceOutput(request,inputs:widestring; va
浏览完文档和示例后,我还没有弄清楚 schema.yaml 文件中的参数到底用在哪里。 在此处使用 AWS 代码示例:https://github.com/aws-samples/aws-proton
程序参数: procedure get_user_profile ( i_attuid in ras_user.attuid%type, i_data_group in data_g
我有一个字符串作为参数传递给我的存储过程。 dim AgentString as String = " 'test1', 'test2', 'test3' " 我想在 IN 中使用该参数声明。 AND
这个问题已经有答案了: When should I use "this" in a class? (17 个回答) 已关闭 6 年前。 我运行了一些java代码,我看到了一些我不太明白的东西。为什么下
我输入 scroll(0,10,200,10);但是当它运行时,它会传递字符串“xxpos”或“yypos”,我确实在没有撇号的情况下尝试过,但它就是行不通。 scroll = function(xp
我是一名优秀的程序员,十分优秀!