我试图将表单中的INSERT值转换为DB,我的一个表单字段是brand_id,它在提交时返回NULL。
有两个实体具有多对一关系
品牌
id
名称
模型
ID
brand_id(FK)
名
IMAGE_URL
评论
所有字段都返回一个值,但brand_id返回NULL
代码和实体如下:
品牌实体:
<?php
// src/coreBundle/Entity/Brand.php
namespace coreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use coreBundle\Entity\Model;
use Doctrine\Common\Collections\ArrayCollection;
/**
*@ORM\Entity
*@ORM\Table(name="brand")
*/
class brand
{
/**
* @var int
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
*@ORM\Column(type="string", length=100)
*/
private $name;
/**
*@ORM\OneToMany(targetEntity="model", mappedBy="brands")
*/
protected $models;
public function __construct()
{
$this->models = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*
* @return brand
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Add model
*
* @param \coreBundle\Entity\model $model
*
* @return brand
*/
public function addModel(\coreBundle\Entity\model $model)
{
$this->models[] = $model;
return $this;
}
/**
* Remove model
*
* @param \coreBundle\Entity\model $model
*/
public function removeModel(\coreBundle\Entity\model $model)
{
$this->models->removeElement($model);
}
/**
* Get models
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getModels()
{
return $this->models;
}
}
模型实体:
<?php
// src/coreBundle/Entity/Model.php
namespace coreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use coreBundle\Entity\Brand;
/**
*@ORM\Entity
*@ORM\Table(name="model")
*/
class model
{
/**
* @var int
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
*@ORM\Column(type="integer")
*/
public $brand_id;
/**
*@ORM\Column(type="string", length=100)
*/
private $name;
/**
*@ORM\Column(type="string", length=100)
*/
private $image_url;
/**
*@ORM\Column(type="string", length=200)
*/
private $comment;
/**
*@ORM\ManyToOne(targetEntity="brand", inversedBy="models")
*@ORM\JoinColumn(name="brand_id", referencedColumnName="id")
*/
protected $brands;
/**
* @ORM\OneToOne(targetEntity="model_item", mappedBy="models")
*/
private $model_items;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set brandId
*
* @param integer $brandId
*
* @return model
*/
public function setBrandId($brandId)
{
$this->brand_id = $brandId;
return $this;
}
/**
* Get brandId
*
* @return integer
*/
public function getBrandId()
{
return $this->brand_id;
}
/**
* Set name
*
* @param string $name
*
* @return model
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set imageUrl
*
* @param string $imageUrl
*
* @return model
*/
public function setImageUrl($imageUrl)
{
$this->image_url = $imageUrl;
return $this;
}
/**
* Get imageUrl
*
* @return string
*/
public function getImageUrl()
{
return $this->image_url;
}
/**
* Set comment
*
* @param string $comment
*
* @return model
*/
public function setComment($comment)
{
$this->comment = $comment;
return $this;
}
/**
* Get comment
*
* @return string
*/
public function getComment()
{
return $this->comment;
}
/**
* Set brands
*
* @param \coreBundle\Entity\brand $brands
*
* @return model
*/
public function setBrands(\coreBundle\Entity\brand $brands = null)
{
$this->brands = $brands;
return $this;
}
/**
* Get brands
*
* @return \coreBundle\Entity\brand
*/
public function getBrands()
{
return $this->brands;
}
}
我的控制器代码:
public function newModelAction(Request $request)
{
$product = $this->getDoctrine()
->getRepository('coreBundle:brand')
->findAll();
if (!$product) {
throw $this->createNotFoundException(
'No product found for id '.$productId
);
}
$model = new model();
$form = $this->createFormBuilder($model)
->add('brand_id',TextType::class,array('label'=>'Brand Id'))
->add('name',TextType::class,array('label'=>'Model Name'))
->add('comment',TextType::class,array('label'=>'Comments'))
->add('image_url',TextType::class,array('label'=>'Image URL'))
->add('save',SubmitType::class, array('label'=>'Add Model'))
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($model);
$em->flush();
return $this->render('coreBundle:layouts:newItem.html.twig',
array('form'=>$form->createView(),));
}
// ... do something, like pass the $product object into a template
return $this->render('coreBundle:layouts:newModel.html.twig',
array('form'=>$form->createView(),));
}
答案 0 :(得分:0)
尝试删除此内容:
*@ORM\Column(type="integer")
*/
public $brand_id;
因为在这一点上你已经有了品牌领域:
/**
*@ORM\ManyToOne(targetEntity="brand", inversedBy="models")
*@ORM\JoinColumn(name="brand_id", referencedColumnName="id")
*/
protected $brands;
您还需要删除brandId的getter和seeter,并从表单中删除brand_id并添加以下内容:
->add('brand', EntityType::class, array(
// query choices from this entity
'class' => 'AppBundle:Brand',
))
答案 1 :(得分:0)
试试这个
/**
*@ORM\ManyToOne(targetEntity="brand")
*@ORM\JoinColumn(nullable=false)
*/
protected $brands;