为什么repositoryClass在父表中阻塞INSERT?

时间:2016-04-18 18:20:23

标签: doctrine-orm symfony

我有两个实体,如果我在@ORM \ Entity声明中设置了一个实体repositoryClass - doctrine不要在父表中插入新行。

我的实体名称是类别和产品。如果我将repositoryClass设置为Category,则Doctrine不会在Product中插入行。这是一种非常奇怪的行为,我第一次面对它。

请帮助,如果可以......

实体代码:

namespace AJStudio\NestedSetBundle\Model;

use Doctrine\ORM\Mapping as ORM;

abstract class Node
{
/**
 * @ORM\Id()
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @var Node
 *
 * @ORM\ManyToOne(targetEntity="Node", inversedBy="children")
 * @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
 */
protected $parent;

/**
 * @var int
 *
 * @ORM\Column(name="lvl", type="smallint", nullable=true)
 */
protected $level;

/**
 * @var int
 *
 * @ORM\Column(name="left_key", type="smallint", nullable=true)
 */
protected $left;

/**
 * @var int
 *
 * @ORM\Column(name="right_key", type="smallint", nullable=true)
 */
protected $right;

/**
 * @var int
 *
 * @ORM\Column(name="position", type="smallint", nullable=true)
 */
protected $position;

/**
 * @var int
 *
 * @ORM\Column(name="name", type="string", nullable=false)
 */
protected $name;

/**
 * @return mixed
 */
public function getId()
{
    return $this->id;
}

/**
 * @param mixed $id
 */
public function setId($id)
{
    $this->id = $id;
}

/**
 * @return Node
 */
public function getParent()
{
    return $this->parent;
}

/**
 * @param Node $parent
 */
public function setParent($parent)
{
    $this->parent = $parent;
}

/**
 * @return int
 */
public function getLeft()
{
    return $this->left;
}

/**
 * @param int $left
 */
public function setLeft($left)
{
    $this->left = $left;
}

/**
 * @return int
 */
public function getRight()
{
    return $this->right;
}

/**
 * @param int $right
 */
public function setRight($right)
{
    $this->right = $right;
}

/**
 * @return int
 */
public function getPosition()
{
    return $this->position;
}

/**
 * @param int $position
 */
public function setPosition($position)
{
    $this->position = $position;
}

/**
 * @return int
 */
public function getName()
{
    return $this->name;
}

/**
 * @param int $name
 */
public function setName($name)
{
    $this->name = $name;
}

/**
 * @return int
 */
public function getLevel()
{
    return $this->level;
}

/**
 * @param int $level
 */
public function setLevel($level)
{
    $this->level = $level;
}
}


namespace Sunduk\AdminBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use AJStudio\NestedSetBundle\Model\Node;

/**
 * @ORM\Table(name="category")
 *     @ORM\Entity(repositoryClass="Sunduk\AdminBundle\Repository\CategoryRepository")
 */
class Category extends Node
{
/**
 * @var Category
 *
 * @ORM\ManyToOne(targetEntity="Category", inversedBy="children")
 * @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
 */
protected $parent;

/**
 * @var Category[]
 *
 * @ORM\OneToMany(targetEntity="Category", mappedBy="parent")
 */
protected $children;

/**
 * @var int
 *
 * @ORM\Column(name="menu_name", type="string", nullable=true)
 */
protected $menuName;

/**
 * @var int
 *
 * @ORM\Column(name="full_name", type="string", nullable=true)
 */
protected $fullName;

/**
 * @var int
 *
 * @ORM\Column(name="description", type="text", nullable=true)
 */
protected $description;

/**
 * @var int
 *
 * @ORM\Column(name="alias", type="string", nullable=true)
 */
protected $alias;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="published_at", type="datetime", nullable=true)
 */
protected $publishedAt;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="unpublished_at", type="datetime", nullable=true)
 */
protected $unpublishedAt;

/**
 * @return Category
 */
public function getParent()
{
    return $this->parent;
}

/**
 * @param Category $parent
 */
public function setParent($parent)
{
    $this->parent = $parent;
}

/**
 * @return Category[]
 */
public function getChildren()
{
    return $this->children;
}

/**
 * @param Category[] $children
 */
public function setChildren($children)
{
    $this->children = $children;
}

/**
 * @return int
 */
public function getMenuName()
{
    return $this->menuName;
}

/**
 * @param int $menuName
 */
public function setMenuName($menuName)
{
    $this->menuName = $menuName;
}

/**
 * @return int
 */
public function getFullName()
{
    return $this->fullName;
}

/**
 * @param int $fullName
 */
public function setFullName($fullName)
{
    $this->fullName = $fullName;
}

/**
 * @return int
 */
public function getDescription()
{
    return $this->description;
}

/**
 * @param int $description
 */
public function setDescription($description)
{
    $this->description = $description;
}

/**
 * @return int
 */
public function getAlias()
{
    return $this->alias;
}

/**
 * @param int $alias
 */
public function setAlias($alias)
{
    $this->alias = $alias;
}

/**
 * @return \DateTime
 */
public function getPublishedAt()
{
    return $this->publishedAt;
}

/**
 * @param \DateTime $publishedAt
 */
public function setPublishedAt($publishedAt)
{
    $this->publishedAt = $publishedAt;
}

/**
 * @return \DateTime
 */
public function getUnpublishedAt()
{
    return $this->unpublishedAt;
}

/**
 * @param \DateTime $unpublishedAt
 */
public function setUnpublishedAt($unpublishedAt)
{
    $this->unpublishedAt = $unpublishedAt;
}
}


namespace Sunduk\AdminBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 *     @ORM\Entity(repositoryClass="Sunduk\AdminBundle\Repository\ProductRepository")
 * @ORM\Table(name="product")
 */
class Product
{
/**
 * @ORM\Id()
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\ManyToOne(targetEntity="Category")
 * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
 */
protected $category;

/**
 * @var Currency
 *
 * @ORM\ManyToOne(targetEntity="Currency")
 * @ORM\JoinColumn(name="currency_id", referencedColumnName="id")
 */
protected $currency;

/**
 * @var Brand
 *
 * @ORM\ManyToOne(targetEntity="Brand")
 * @ORM\JoinColumn(name="brand_id", referencedColumnName="id")
 */
protected $brand;

/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", nullable=false)
 */
protected $name;

/**
 * @var string
 *
 * @ORM\Column(name="article_number", type="string", nullable=true)
 */
protected $articleNumber;

/**
 * @var int
 *
 * @ORM\Column(name="base_price", type="integer", nullable=true)
 */
protected $basePrice;

/**
 * @var string
 *
 * @ORM\Column(name="description", type="text", nullable=true)
 */
protected $description;

/**
 * @var int
 *
 * @ORM\Column(name="count_per_unit", type="integer", nullable=false)
 */
protected $countPerUnit = 1;

/**
 * @var int
 *
 * @ORM\Column(name="weight", type="float", nullable=true)
 */
protected $weight;

/**
 * @var string
 *
 * @ORM\Column(name="alias", type="string", nullable=true)
 */
protected $alias;

/**
 * @var bool
 *
 * @ORM\Column(name="new", type="boolean", nullable=false)
 */
protected $new = false;

/**
 * @var bool
 *
 * @ORM\Column(name="popular", type="boolean", nullable=false)
 */
protected $popular = false;

/**
 * @return mixed
 */
public function getId()
{
    return $this->id;
}

/**
 * @param mixed $id
 */
public function setId($id)
{
    $this->id = $id;
}

/**
 * @return string
 */
public function getName()
{
    return $this->name;
}

/**
 * @param string $name
 */
public function setName($name)
{
    $this->name = $name;
}

/**
 * @return string
 */
public function getArticleNumber()
{
    return $this->articleNumber;
}

/**
 * @param string $articleNumber
 */
public function setArticleNumber($articleNumber)
{
    $this->articleNumber = $articleNumber;
}

/**
 * @return int
 */
public function getBasePrice()
{
    return $this->basePrice;
}

/**
 * @param int $basePrice
 */
public function setBasePrice($basePrice)
{
    $this->basePrice = $basePrice;
}

/**
 * @return string
 */
public function getDescription()
{
    return $this->description;
}

/**
 * @param string $description
 */
public function setDescription($description)
{
    $this->description = $description;
}

/**
 * @return int
 */
public function getCountPerUnit()
{
    return $this->countPerUnit;
}

/**
 * @param int $countPerUnit
 */
public function setCountPerUnit($countPerUnit)
{
    $this->countPerUnit = $countPerUnit;
}

/**
 * @return int
 */
public function getWeight()
{
    return $this->weight;
}

/**
 * @param int $weight
 */
public function setWeight($weight)
{
    $this->weight = $weight;
}

/**
 * @return string
 */
public function getAlias()
{
    return $this->alias;
}

/**
 * @param string $alias
 */
public function setAlias($alias)
{
    $this->alias = $alias;
}

/**
 * @return boolean
 */
public function isNew()
{
    return $this->new;
}

/**
 * @param boolean $new
 */
public function setNew($new)
{
    $this->new = $new;
}

/**
 * @return boolean
 */
public function isPopular()
{
    return $this->popular;
}

/**
 * @param boolean $popular
 */
public function setPopular($popular)
{
    $this->popular = $popular;
}

/**
 * @return Currency
 */
public function getCurrency()
{
    return $this->currency;
}

/**
 * @param Currency $currency
 */
public function setCurrency($currency)
{
    $this->currency = $currency;
}

/**
 * @return mixed
 */
public function getBrand()
{
    return $this->brand;
}

/**
 * @param mixed $brand
 */
public function setBrand($brand)
{
    $this->brand = $brand;
}

/**
 * @return mixed
 */
public function getCategory()
{
    return $this->category;
}

/**
 * @param mixed $category
 */
public function setCategory($category)
{
    $this->category = $category;
}
}


namespace Sunduk\AdminBundle\Repository;

use Sunduk\AdminBundle\Entity\Category;

class CategoryRepository extends AbstractRepository
{
public function getTree() {
    return $this
        ->createQueryBuilder('category')
        ->orderBy('category.left', 'ASC')
        ->getQuery()
        ->getResult();
}

public function getTreeRoot() {
    return $this
        ->createQueryBuilder('category')
        ->orderBy('category.left', 'ASC')
        ->where('category.parent IS NULL')
        ->setMaxResults(1)
        ->getQuery()
        ->getResult();
}

public function getChildrenByCategory(Category $category) {
    return $this
        ->createQueryBuilder('category')
        ->where('category.left > :left_key AND category.right < :right_key ')
        ->setParameter('left_key', $category->getLeft())
        ->setParameter('right_key', $category->getRight())
        ->getQuery()
        ->getResult();
}
}

1 个答案:

答案 0 :(得分:0)

您需要在抽象实体类

上添加Mapped超类注释
/**
* @ORM\MappedSuperclass
*
*/
abstract class Node
{
//...
}