Php 一夫一妻制关系不存在';我拿不到身份证
我有两个实体(项目和产品) 一种商品只有一种产品。 一个产品可以有0个或N个项目 当我创建一个新项目时,除了product_id为null之外,其他都是正确的,但在dev环境中product_id是正确的Php 一夫一妻制关系不存在';我拿不到身份证,php,database,symfony,doctrine-orm,one-to-many,Php,Database,Symfony,Doctrine Orm,One To Many,我有两个实体(项目和产品) 一种商品只有一种产品。 一个产品可以有0个或N个项目 当我创建一个新项目时,除了product_id为null之外,其他都是正确的,但在dev环境中product_id是正确的 class Item{ /** * @var integer * * @ORM\Column(name="id", type="integer", nullable=false) * @ORM\Id * @ORM\GeneratedValue(strategy="IDENTITY")
class Item{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="Products", inversedBy="item", cascade={"persist"})
* @ORM\JoinColumn(name="product_id", referencedColumnName="product_id", onDelete="cascade")
* @var My\WebBundle\Entity\Products
*/
protected $products;
/**
* Get Id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set products
*
* @param My\WebBundle\Entity\Products $products
*/
public function setProducts(\My\WebBundle\Entity\Products $products)
{
$this->products = $products;
return $this;
}
/**
* get products
*
* @return My\WebBundle\Entity\Products
*/
public function getProducts()
{
return $this->products;
}
}
Class Product
class Products
{
/**
* @var integer
*
* @ORM\Column(name="product_id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $productId;
/**
* @ORM\OneToMany(targetEntity="Item", mappedBy="products", cascade={"persist", "remove"})
*/
protected $item;
/**
* Get productId
*
* @return integer
*/
public function getProductId()
{
return $this->productId;
}
public function addItem(\My\WebBundle\Entity\Item $item)
{
$this->item[] = $item;
$item->setProducts($this);
return $this;
}
/**
* Get item
*
* @return Doctrine\Common\Collections\Collection
*/
public function getItem()
{
return $this->item;
}
}
我的问题是,当我创建此项目时,我使用:
$p = $em->getRepository('MyWebBundle:Products')->findOneByCode($code);
$item->setProducts($p);
在我的数据库中的Item表中,product_id为null,但若我在dev环境中这样做,product_id是正确的
class Item{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="Products", inversedBy="item", cascade={"persist"})
* @ORM\JoinColumn(name="product_id", referencedColumnName="product_id", onDelete="cascade")
* @var My\WebBundle\Entity\Products
*/
protected $products;
/**
* Get Id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set products
*
* @param My\WebBundle\Entity\Products $products
*/
public function setProducts(\My\WebBundle\Entity\Products $products)
{
$this->products = $products;
return $this;
}
/**
* get products
*
* @return My\WebBundle\Entity\Products
*/
public function getProducts()
{
return $this->products;
}
}
Class Product
class Products
{
/**
* @var integer
*
* @ORM\Column(name="product_id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $productId;
/**
* @ORM\OneToMany(targetEntity="Item", mappedBy="products", cascade={"persist", "remove"})
*/
protected $item;
/**
* Get productId
*
* @return integer
*/
public function getProductId()
{
return $this->productId;
}
public function addItem(\My\WebBundle\Entity\Item $item)
{
$this->item[] = $item;
$item->setProducts($this);
return $this;
}
/**
* Get item
*
* @return Doctrine\Common\Collections\Collection
*/
public function getItem()
{
return $this->item;
}
}
有人知道吗?
提前谢谢
---编辑---
我有这样一个简单的控制器:
$em = $this->getDoctrine()->getManager();
$item = new Item();
$item->setName($name);
$p = $em->getRepository('MyWebBundle:Products')->findOneByCode($product);
$item->setProducts($p);
$em->persist($item);
$em->flush()
1) 你清除缓存了吗?2) 您在
dev
模式下使用的数据库与在prod
模式下使用的数据库不同吗?谢谢,但是是的,我清理了缓存,数据库也一样,(我有其他字段,所有字段都正确生成)啊,好的。似乎你已经遵守了所有关于拥有
和反向
关系的规则。你能告诉我们你还想用这些实体做什么吗(更多的代码)?是的,请看我的简单控制器,只有我输入了名称和产品,名称是对的,但产品没有出现。我真的不知道这里可能有什么错。:-/假设repository方法返回产品的托管实例,则应该查找所有内容。onDelete
约束是否会破坏关系?无论哪种方式,我都希望这会引起一些关注。。。