我使用zend framework 2和doctrine 2有很多关系。 这种关系是在类别和产品之间。
我的产品实体
<?php
namespace Application\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Zend\ServiceManager\ServiceManager;
use Doctrine\ORM\PersistentCollection;
use Application\Entity\Categories;
/**
* Products
*
* @ORM\Table(name="products", indexes={
* @ORM\Index(name="PRIMARY", columns={"product_id"}),
* @ORM\Index(name="created_by_user_id", columns={"created_by_user_id"}),
* @ORM\Index(name="retail_status_id", columns={"retail_status_id"}),
* })
* @ORM\Entity(repositoryClass="Application\Entity\Repository\ProductsRepository")
* @ORM\HasLifecycleCallbacks
*/
class Products extends AbstractEntity
{
/**
* @var integer
*
* @ORM\Column(name="product_id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $productId;
/**
* @var \Application\Entity\AttributeSets
*
* @ORM\ManyToOne(targetEntity="Application\Entity\AttributeSets", inversedBy="products")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="attribute_set_id", referencedColumnName="attribute_set_id")
* })
*/
private $attributeSet;
/**
* @var \Application\Entity\RetailStatus
*
* @ORM\ManyToOne(targetEntity="Application\Entity\RetailStatus", inversedBy="products")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="retail_status_id", referencedColumnName="retail_status_id")
* })
*/
private $retailStatus;
/**
* @var string
*
* @ORM\Column(name="price", type="decimal", precision=9, scale=2, nullable=false)
*/
private $price;
/**
* @var integer
*
* @ORM\Column(name="active", type="integer", nullable=false)
*/
private $active;
/**
* @var \DateTime
*
* @ORM\Column(name="date_created", type="datetime")
*/
private $dateCreated;
/**
* @var \DateTime
*
* @ORM\Column(name="date_modified", type="datetime")
*/
private $dateModified;
/**
* @var \Application\Entity\Users
*
* @ORM\ManyToOne(targetEntity="Application\Entity\Users")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="created_by_user_id", referencedColumnName="user_id")
* })
*/
private $authUser;
/**
* @var \Application\Entity\ProductTitles
*
* @ORM\OneToOne(targetEntity="Application\Entity\ProductTitles", cascade={"persist", "persist"}, mappedBy="product")
*/
private $title;
/**
* @var \Application\Entity\ProductDescriptions
*
* @ORM\OneToOne(targetEntity="Application\Entity\ProductDescriptions", cascade={"persist", "persist"}, mappedBy="product")
*/
private $description;
/**
* @var \Application\Entity\ProductDimentions
*
* @ORM\OneToOne(targetEntity="Application\Entity\ProductDimentions", cascade={"persist", "persist"}, mappedBy="product")
*/
private $productDimentions;
/**
* @var \Doctrine\ORM\PersistentCollection
*
* @ORM\ManyToMany(targetEntity="Application\Entity\Categories", cascade={"persist"}, inversedBy="products")
* @ORM\JoinTable(name="product_to_catagory",
* joinColumns={@ORM\joinColumn(name="product_id", referencedColumnName="product_id")},
* inverseJoinColumns={@ORM\joinColumn(name="catagory_id", referencedColumnName="catagory_id")}
* )
*/
private $categories;
/**
* @var \Doctrine\ORM\PersistentCollection
*
* @ORM\OneToMany(targetEntity="Application\Entity\ProductAttributeValues", cascade={"persist", "persist"}, mappedBy="product")
*/
private $values;
public function __construct(ServiceManager $serviceManager = NULL)
{
parent::__construct($serviceManager);
$this->categories = new ArrayCollection();
}
/**
*
* @return integer
*/
public function getProductId()
{
return $this->productId;
}
/**
*
* @return \Application\Entity\AttributeSets
*/
public function getAttributeSet()
{
return $this->attributeSet;
}
/**
*
* @return \Application\Entity\RetailStatus
*/
public function getRetailStatus()
{
return $this->retailStatus;
}
/**
*
* @return string
*/
public function getPrice()
{
return $this->price;
}
/**
*
* @return boolean
*/
public function isActive()
{
return (bool) $this->active;
}
/**
*
* @return \DateTime
*/
public function getDateCreated()
{
return $this->dateCreated;
}
/**
*
* @return \DateTime
*/
public function getDateModified()
{
return $this->dateModified;
}
/**
*
* @return \Application\Entity\Users
*/
public function getAuthUser()
{
return $this->authUser;
}
/**
*
* @return \Application\Entity\ProductTitles
*/
public function getTitle()
{
return $this->title;
}
/**
*
* @return \Application\Entity\ProductDescriptions
*/
public function getDescription()
{
return $this->description;
}
/**
*
* @return \Application\Entity\ProductDimentions
*/
public function getProductDimentions()
{
return $this->productDimentions;
}
/**
*
* @return \Doctrine\ORM\PersistentCollection
*/
public function getCategories()
{
return $this->categories;
}
/**
*
* @return \Doctrine\ORM\PersistentCollection
*/
public function getValues()
{
return $this->values;
}
/**
*
* @param \Application\Entity\AttributeSets $attributeSet
* @return \Application\Entity\Products
*/
public function setAttributeSet(\Application\Entity\AttributeSets $attributeSet)
{
$this->attributeSet = $attributeSet;
return $this;
}
/**
*
* @param \Application\Entity\RetailStatus $retailStatus
* @return \Application\Entity\Products
*/
public function setRetailStatus(\Application\Entity\RetailStatus $retailStatus)
{
$this->retailStatus = $retailStatus;
return $this;
}
/**
*
* @param string $price
* @return \Application\Entity\Products
*/
public function setPrice($price)
{
$this->price = $price;
return $this;
}
/**
*
* @param integer $active
* @return \Application\Entity\Products
*/
public function setActive($active)
{
$this->active = $active;
return $this;
}
/**
*
* @param \DateTime $dateCreated
* @return \Application\Entity\Products
*/
public function setDateCreated(\DateTime $dateCreated)
{
$this->dateCreated = $dateCreated;
return $this;
}
/**
*
* @param \DateTime $dateModified
* @return \Application\Entity\Products
*/
public function setDateModified(\DateTime $dateModified)
{
$this->dateModified = $dateModified;
return $this;
}
/**
*
* @param \Application\Entity\Users $authUser
* @return \Application\Entity\Products
*/
public function setAuthUser(\Application\Entity\Users $authUser)
{
$this->authUser = $authUser;
return $this;
}
/**
*
* @param \Application\Entity\ProductTitles $title
* @return \Application\Entity\Products
*/
public function setTitle(\Application\Entity\ProductTitles $title)
{
$this->title = $title;
return $this;
}
/**
*
* @param \Application\Entity\ProductDescriptions $description
* @return \Application\Entity\Products
*/
public function setDescription(\Application\Entity\ProductDescriptions $description)
{
$this->description = $description;
return $this;
}
/**
*
* @param \Application\Entity\ProductDimentions $productDimentions
* @return \Application\Entity\Products
*/
public function setProductDimentions(\Application\Entity\ProductDimentions $productDimentions)
{
$this->productDimentions = $productDimentions;
return $this;
}
public function addCategory(Categories $category)
{
$this->categories->add($category);
}
/**
*
* @param \Doctrine\Common\Collections\ArrayCollection $categories
* @return \Application\Entity\Products
*/
public function addCategories(\Doctrine\Common\Collections\ArrayCollection $categories)
{
foreach ($categories as $category) {
$this->addCategory($category);
}
// \Freedom\Logger\InfoLogger::vardump($this->categories);
return $this;
}
public function removeCategories(\Doctrine\Common\Collections\ArrayCollection $categories)
{
foreach ($categories as $category) {
$this->categories->removeElement($category);
}
return $this;
}
/**
* @ORM\PrePersist
* @return \Application\Entity\Users
*/
public function prePersist()
{
$this->dateCreated = $this->getCurrentDateTime();
$this->dateModified = $this->getCurrentDateTime();
$this->authUser = $this->getAutherizedUser();
// $this->categories = new PersistentCollection($this->getEntityManager(), NULL, $this->categories);
// \Freedom\Logger\InfoLogger::vardump($this->categories->count());
// \Freedom\Logger\InfoLogger::vardump($this->categories);
return $this;
}
/**
* @ORM\PreUpdate
* @return \Application\Entity\Users
*/
public function preUpdate()
{
// $this->dateModified = $this->getCurrentDateTime();
return $this;
}
public function toArray()
{
$array = array();
foreach (get_object_vars($this) as $var => $value) {
switch ($var) {
case 'authUser':
if (!is_null($value)) {
$array[$var] = $value->toArray();
}
break;
default:
$array[$var] = $value;
}
}
return $array;
}
}
和我的类别实体
<?php
namespace Application\Entity;
use Doctrine\ORM\Mapping as ORM;
use Zend\ServiceManager\ServiceManager;
use Application\Entity\StaticPages;
/**
* Categories
*
* @ORM\Table(name="catagories", indexes={
* @ORM\Index(name="PRIMARY", columns={"catagory_id"}),
* @ORM\Index(name="created_by_user_id", columns={"created_by_user_id"}),
* })
* @ORM\Entity(repositoryClass="Application\Entity\Repository\CategoriesRepository")
* @ORM\HasLifecycleCallbacks
*/
class Categories extends AbstractEntity
{
/**
* @var integer
*
* @ORM\Column(name="catagory_id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $catagoryId;
/**
* @var string
*
* @ORM\Column(name="category", type="string", length=30, nullable=false)
*/
private $category;
/**
* @var \DateTime
*
* @ORM\Column(name="date_created", type="datetime")
*/
private $dateCreated;
/**
* @var \DateTime
*
* @ORM\Column(name="date_modified", type="datetime")
*/
private $dateModified;
/**
* @var \Application\Entity\Users
*
* @ORM\ManyToOne(targetEntity="Application\Entity\Users")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="created_by_user_id", referencedColumnName="user_id")
* })
*/
private $authUser;
/**
* @var \Application\Entity\Categories
*
* @ORM\ManyToOne(targetEntity="Application\Entity\Categories", inversedBy="childCategories")
* @ORM\JoinColumn(name="parent_catagory_id", referencedColumnName="catagory_id")
*/
protected $parentCategory;
/**
* @var \Doctrine\ORM\PersistentCollection
*
* @ORM\OneToMany(targetEntity="Application\Entity\Categories", mappedBy="parentCategory")
*/
protected $childCategories;
/**
* @var Doctrine\ORM\PersistentCollection
*
* @ORM\ManyToMany(targetEntity="Application\Entity\Products", cascade={"persist"}, orphanRemoval=true, mappedBy="categories")
*/
private $products;
/**
* @var \Application\Entity\Sites
*
* @ORM\ManyToOne(targetEntity="Application\Entity\Sites", inversedBy="categories")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="site_id", referencedColumnName="site_id")
* })
*/
private $site;
/**
* @var \Application\Entity\StaticPages
*
* @ORM\OneToOne(targetEntity="Application\Entity\StaticPages", mappedBy="category", cascade="persist")
*/
private $staticPage;
/**
* @var integer
*
* @ORM\Column(name="active", type="integer", nullable=false)
*/
private $active;
/**
* @var string
*
* @ORM\Column(name="url_key", type="string", length=30, nullable=false)
*/
private $urlKey;
/**
* @var string
*
* @ORM\Column(name="description", type="text", length=200, nullable=false)
*/
private $description;
/**
* @var integer
*
* @ORM\Column(name="sort_order", type="integer", nullable=false)
*/
private $sortOrder;
public function __construct(ServiceManager $serviceManager = NULL)
{
parent::__construct($serviceManager);
$this->parentCategory = NULL;
$this->urlKey = NULL;
}
/**
*
* @return integer
*/
public function getCatagoryId()
{
return $this->catagoryId;
}
/**
*
* @return string
*/
public function getCategory()
{
return $this->category;
}
/**
*
* @return \DateTime
*/
public function getDateCreated()
{
return $this->dateCreated;
}
/**
*
* @return \DateTime
*/
public function getDateModified()
{
return $this->dateModified;
}
/**
*
* @return \Application\Entity\Users
*/
public function getAuthUser()
{
return $this->authUser;
}
/**
*
* @return boolean
*/
public function hasChildCategories()
{
return (bool) $this->getChildCategories()->count();
}
/**
*
* @return \Doctrine\ORM\PersistentCollection
*/
public function getChildCategories()
{
return $this->childCategories;
}
/**
*
* @return boolean
*/
public function hasParentCategory()
{
return (bool) $this->getParentCategory();
}
/**
*
* @return \Application\Entity\Categories
*/
public function getParentCategory()
{
return $this->parentCategory;
}
public function hasProducts()
{
return (bool) $this->getProducts()->count();
}
/**
*
* @return \Doctrine\ORM\PersistentCollection
*/
public function getProducts()
{
return $this->products;
}
/**
*
* @return \Application\Entity\Sites
*/
public function getSite()
{
return $this->site;
}
/**
*
* @return \Application\Entity\StaticPages
*/
public function getStaticPage()
{
return $this->staticPage;
}
/**
*
* @return boolean
*/
public function isActive()
{
return (bool) $this->active;
}
/**
*
* @return strng
*/
public function getUrlKey()
{
return $this->urlKey;
}
/**
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
*
* @return integer
*/
public function getSortOrder()
{
return $this->sortOrder;
}
/**
*
* @param string $category
* @return \Application\Entity\Categories
*/
public function setCategory($category)
{
$this->category = $category;
return $this;
}
/**
*
* @param \DateTime $dateCreated
* @return \Application\Entity\Categories
*/
public function setDateCreated(\DateTime $dateCreated)
{
$this->dateCreated = $dateCreated;
return $this;
}
/**
*
* @param \DateTime $dateModified
* @return \Application\Entity\Categories
*/
public function setDateModified(\DateTime $dateModified)
{
$this->dateModified = $dateModified;
return $this;
}
/**
*
* @param \Application\Entity\Sites $site
* @return \Application\Entity\Categories
*/
public function setSite(\Application\Entity\Sites $site)
{
$this->site = $site;
return $this;
}
public function hasStaticPage()
{
return (bool) $this->getStaticPage();
}
/**
*
* @param \Application\Entity\StaticPages $staticPage
* @return \Application\Entity\Categories
*/
public function setStaticPage(\Application\Entity\StaticPages $staticPage)
{
$this->staticPage = $staticPage;
return $this;
}
public function removeStaticPage()
{
if ($this->getStaticPage() instanceof StaticPages) {
$entityManager = $this->getEntityManager();
$entityManager->remove($this->staticPage);
$entityManager->flush();
}
$this->staticPage = NULL;
}
/**
*
* @param \Application\Entity\Users $authUser
* @return \Application\Entity\Categories
*/
public function setAuthUser(\Application\Entity\Users $authUser)
{
$this->authUser = $authUser;
return $this;
}
/**
*
* @param \Application\Entity\Categories $parentCategory
* @return \Application\Entity\Categories
*/
public function setParentCategory(\Application\Entity\Categories $parentCategory)
{
$this->parentCategory = $parentCategory;
return $this;
}
/**
*
* @param integer $active
* @return \Application\Entity\Categories
*/
public function setActive($active)
{
$this->active = $active;
return $this;
}
/**
*
* @param string $urlKey
* @return \Application\Entity\Categories
*/
public function setUrlKey($urlKey)
{
$escapeUri = $this->getServiceManager()->get('ViewHelperManager')->get('escapeUrl');
$this->urlKey = $escapeUri($urlKey);
return $this;
}
/**
*
* @param string $description
* @return \Application\Entity\Categories
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
*
* @param integer $sortOrder
* @return \Application\Entity\Categories
*/
public function setSortOrder($sortOrder)
{
$this->sortOrder = $sortOrder;
return $this;
}
/**
* @ORM\PrePersist
* @return \Application\Entity\Users
*/
public function prePersist()
{
$this->dateCreated = $this->getCurrentDateTime();
$this->dateModified = $this->getCurrentDateTime();
$this->authUser = $this->getAutherizedUser();
return $this;
}
/**
* @ORM\PreUpdate
* @return \Application\Entity\Users
*/
public function preUpdate()
{
$this->dateModified = $this->getCurrentDateTime();
return $this;
}
public function toArray()
{
$array = array();
foreach (get_object_vars($this) as $var => $value) {
switch ($var) {
case 'products':
foreach ($value as $product) {
$array[$var][] = $product->toArray();
}
break;
case 'authUser':
if (!is_null($value)) {
$array[$var] = $value->toArray();
}
break;
default:
$array[$var] = $value;
}
}
return $array;
}
}
可以看出,产品和类别通过表product_to_catagory链接。
我正在使用doctrines object multi复选框来使用
获取产品的类别$this->add(array(
'name' => 'categories',
'type' => 'Admin\DoctrineModule\Form\Element\ObjectMultiCheckbox',
'options' => array(
'label' => _('Categories:'),
'label_attributes' => array('class' => 'required'),
'object_manager' => $this->getEntityManager(),
'target_class' => 'Application\Entity\Categories',
'property' => 'category',
'is_method' => true,
'find_method' => array(
'name' => 'FindAll',
),
'disable_inarray_validator' => true,
),
));
表单绑定到产品实体
提交表单后,产品实体中的类别将返回Doctrine \ Common \ Collections \ ArrayCollection
我遇到的问题是,当我清空产品实体时,product_to_catagory表中的数据库没有保存在数据库中。
有谁知道我做错了什么?
非常感谢提前。
EDIT 将拥有方改为产品,代码更新。