[英]Add new field to table and Edit Form Symfony
我是Symfony的新手,繼承了一個項目,原來是php custom,現在和Symfony 3.
我對 Symfony 知之甚少,但在 php 工作了很多。
我想在名為“majoritem”的產品表中添加一個字段
我已將 majoritem 添加到產品 class,將其添加到表單生成器,通過 phpmyadmin 添加到表中。
當應用程序構建表單時,我可以看到新的表單項。
如果我手動更改數據庫中的字段,它不會反映在表單中。
我的假設是我沒有將該字段綁定到表單字段?
任何幫助將非常感激。
在表單生成器中,我現在有以下內容(縮短..):
public function buildForm(FormBuilderInterface $builder, array $options)
{
$companyId = $builder->getData()->getCompanyId();
$builder
->add('productCode')
->add('name')
->add('productType', ChoiceType::class, [
'choices' => [
'Primary' => 'Primary',
'Secondary' => 'Secondary'
]
])
->add('majoritem', ChoiceType::class, [
'choices' => [
'No' => '0',
'Yes' => '1'
]
])
]);
}
窗體的更新操作如下:
public function updateAction(Request $request, $productId)
{
$product = $this->getDoctrine()->getRepository('EcoModelBundle:Products')
->find($productId);
if (!$product)
{
throw new NotFoundHttpException('The requested product could not be found');
}
$form = $this->createForm(ProductsType::class, $product);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
$product = $form->getData();
try
{
$em = $this->getDoctrine()->getManager();
$em->persist($product);
$em->flush();
$request->getSession()->getFlashBag()->add('success', 'Product updated successfully');
}
catch (\Exception $e)
{
$request->getSession()->getFlashBag()->add('error', 'There was an error while saving the product. '
. $e->getMessage());
}
}
else if ($form->isSubmitted() && !$form->isValid())
{
$request->getSession()->getFlashBag()->add('error', 'The submitted data is invalid');
}
return $this->render('@EcoProducts/Product/update.html.twig', [
'product' => $product,
'form' => $form->createView()
]);
}
最后,Products class如下:
class Products
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var integer
*
* @ORM\Column(name="company_id", type="integer", nullable=false)
*/
private $companyId;
/**
* @var string
*
* @ORM\Column(name="product_code", type="string", length=255, nullable=false)
*/
private $productCode;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=100, nullable=false)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="description", type="text", length=65535, nullable=false)
*/
private $description;
/**
* @var integer
*
* @ORM\Column(name="majoritem", type="integer", nullable=false)
*/
private $majoritem;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set companyId
*
* @param integer $companyId
*
* @return Products
*/
public function setCompanyId($companyId)
{
$this->companyId = $companyId;
return $this;
}
/**
* Get companyId
*
* @return integer
*/
public function getCompanyId()
{
return $this->companyId;
}
/**
* Set productCode
*
* @param string $productCode
*
* @return Products
*/
public function setProductCode($productCode)
{
$this->productCode = $productCode;
return $this;
}
/**
* Get productCode
*
* @return string
*/
public function getProductCode()
{
return $this->productCode;
}
/**
* Set name
*
* @param string $name
*
* @return Products
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set description
*
* @param string $description
*
* @return Products
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set majoritem
*
* @param integer $majoritem
*
* @return Products
*/
public function setMajoritem($majoritem)
{
$this->majoritem = $majoritem;
return $this;
}
/**
* Get majoritem
*
* @return integer
*/
public function getMajoritem()
{
return $this->majoritem;
}
/**
* Constructor
*/
public function __construct()
{
$this->details = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add detail
*
* @param \EcoModelBundle\Entity\ProjectDetails $detail
*
* @return Products
*/
public function addDetail(\EcoModelBundle\Entity\ProjectDetails $detail)
{
$this->details[] = $detail;
return $this;
}
/**
* Remove detail
*
* @param \EcoModelBundle\Entity\ProjectDetails $detail
*/
public function removeDetail(\EcoModelBundle\Entity\ProjectDetails $detail)
{
$this->details->removeElement($detail);
}
/**
* Get details
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getDetails()
{
return $this->details;
}
}
所以,有趣的是我做了一些改變..以下是我的產品 class
/**
* Set majoritem
*
* @param integer $majoritem
*
* @return Products
*/
public function setMajoritem($majoritem)
{
$this->majoritem = $majoritem;
return $this;
}
/**
* Get majoritem
*
* @return integer
*/
public function getMajoritem()
{
return $this->majoritem;
}
那些似乎不起作用,或者至少我應該說 getMajoritem 不起作用?
如果我將表單字段更改為簡單的文本字段而不是下拉列表,則當字段的基礎數據為零時,表單顯示為空白。
但是,如果我輸入 1 並提交表單,它會將數據庫字段更新為 1。
所以我想知道我的問題是否真的是表單生成器沒有檢索字段值並將其添加到表單中?
使用 doctrine 和以下命令檢查映射是否正確:
php bin/console doctrine:schema:validate.
如果正確,您可以使用我們之前驗證過的模式更新數據庫模式,使用以下命令:
php bin/console doctrine:schema:update --force.
這將確保通過 Doctrine 完成映射,如果介質出現錯誤,ORM Doctrine 將負責通知您。
我還建議您通過使用遷移來更改架構,檢查您的項目是否正在這樣做,也許您應該通過遷移更新架構:
php bin/console doctrine:migrations:diff
這將在src/Migrations/Version[timestamp].php
中創建一個文件
要更新數據庫的模式,您必須按以下方式進行:
php bin/console doctrine:migrations:execute --up [timestamp]
如果你想反轉架構更新,你可以這樣做:
php bin/console doctrine:migrations:execute --down [timestamp].
使用schema:update
和migrations
之間的優勢在於,使用后者您可以反轉模式更新操作
為您的表單嘗試 integer 值而不是字符串值,因為實體中的映射字段的類型為 integer:
->add('majoritem', ChoiceType::class, [
'choices' => [
'No' => 0, // <--- instead of '0'
'Yes' => 1 // <--- instead of '1'
]
])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.