Postgresql 一对一原则,自我参照不起作用
我正在处理一个遗留应用程序,现在需要更改一个现有表(称为categories),以便它有一个parent_id字段,该字段是它自己的表(categories表)主键的外键 我遇到的问题是,我无法在数据库中设置父字段 我有以下实体orm:Postgresql 一对一原则,自我参照不起作用,postgresql,doctrine-orm,php-7.1,symfony-3.2,Postgresql,Doctrine Orm,Php 7.1,Symfony 3.2,我正在处理一个遗留应用程序,现在需要更改一个现有表(称为categories),以便它有一个parent_id字段,该字段是它自己的表(categories表)主键的外键 我遇到的问题是,我无法在数据库中设置父字段 我有以下实体orm: ModelBundle\Entity\Category: type: entity table: category indexes: fki_parent_to_id: columns:
ModelBundle\Entity\Category:
type: entity
table: category
indexes:
fki_parent_to_id:
columns:
- parent_id
uniqueConstraints:
uniq_64c19c1989d9b62:
columns:
- slug
uniq_name:
columns:
- name
id:
id:
type: integer
nullable: false
options:
unsigned: false
id: true
generator:
strategy: SEQUENCE
fields:
name:
type: string
nullable: false
length: 255
options:
fixed: false
slug:
type: string
nullable: false
length: 255
options:
fixed: false
description:
type: string
nullable: true
length: 255
options:
fixed: false
isActive:
type: boolean
nullable: false
options:
default: false
column: is_active
displayOrder:
type: integer
nullable: true
options:
unsigned: false
column: display_order
oneToOne:
parent:
targetEntity: Category
joinColumns:
parent_id:
referencedColumnName: id
lifecycleCallbacks: { }
以下是实体类:
<?php
namespace ModelBundle\Entity;
use Doctrine\Common\Collections\Collection;
use JMS\Serializer\Annotation\Expose;
use Symfony\Cmf\Bundle\SeoBundle\Extractor\TitleReadInterface;
use Symfony\Cmf\Bundle\SeoBundle\Extractor\DescriptionReadInterface;
use Symfony\Cmf\Bundle\SeoBundle\Extractor\ExtrasReadInterface;
class Category implements ExtrasReadInterface, TitleReadInterface, DescriptionReadInterface
{
/** @var int */
private $id;
/** @var string */
private $slug;
/**
* @Expose
*
* @var string
*/
private $name;
/** @var bool */
protected $isActive;
/** @var string */
private $description;
/** @var Collection */
private $products;
/** @var int */
private $displayOrder;
/**
* @var Category
*/
private $parent;
public function getId(): int
{
return $this->id;
}
public function setName(string $name): Category
{
$this->name = $name;
return $this;
}
public function getName(): string
{
return $this->name;
}
public function setIsActive(bool $isActive): Category
{
$this->isActive = $isActive;
return $this;
}
public function getIsActive(): bool
{
return $this->isActive;
}
public function setDescription(string $description): Category
{
$this->description = $description;
return $this;
}
public function getDescription(): string
{
return $this->description ?: '';
}
public function __construct()
{
$this->products = new \Doctrine\Common\Collections\ArrayCollection();
}
public function addProduct(Product $products): Category
{
$this->products[] = $products;
return $this;
}
public function removeProduct(Product $products)
{
$this->products->removeElement($products);
}
public function getProducts(): Collection
{
return $this->products;
}
public function setSlug(string $slug): Category
{
$this->slug = $slug;
return $this;
}
public function getSlug(): string
{
return $this->slug ?: '';
}
public function getSeoTitle(): string
{
return $this->getName();
}
public function getSeoDescription(): string
{
return $this->getDescription();
}
public function getSeoExtras(): array
{
return [
'property' => [
'og:title' => $this->name,
'og:description' => $this->description,
'og:type' => 'product:category'
],
];
}
function getDisplayOrder(): ?int
{
return $this->displayOrder;
}
public function setDisplayOrder($displayOrder): Category
{
$this->displayOrder = $displayOrder;
return $this;
}
public function getParent() : Category
{
return $this->parent;
}
public function setParent(Category $parent): void
{
$this->parent = $parent;
}
}
此代码在数据库中创建一个新类别,但父字段设置为NULL
我做错了什么?我需要做什么才能将父对象保存到数据库中。
注:我不想要一个家庭,我只想要一个类别有一个家长,仅此而已
版本:
条令/注释v1.3.1
条令/缓存v1.6.1
条令/集合v1.4.0
条令/通用v2.7.2
条令/数据装置v1.2.2
条令/dbal v2.5.12
条令/条令包1.6.7
条令/条令缓存包1.3.0
条令/条令固定装置包v2.4.0
条令/条令迁移包v1.2.1
条令/拐点v1.1.0
条令/实例化器1.0.5
条令/词汇1.0.1
条令/迁移v1.5.0
条令/orm v2.5.6
破产管理署/条令扩展1.2.0
编辑:
抱歉,伙计们,经过两天的尝试,我注意到条令有以下缓存功能(在symfony配置中):
我一移除缓存,一切都开始工作了。
我正在手动删除缓存目录,但没有看到doctrine也在使用apcu进行缓存。这里的信息太多,无法进行后续操作,您是否可以尝试只保留相关信息并删除额外信息?如果您从YML中删除
索引
和连接列
部分并清除缓存,会发生什么情况?通常,您不需要明确地声明它们,甚至可能会遇到问题。
Example code:
$em = $this->get('doctrine')->getManager();
$repo = $em->getRepository(Category::class);
$newCat = new Category();
$newCat->setName('Test11');
$newCat->setSlug('slug1');
$newCat->setDescription('desc1');
$newCat->setIsActive(true);
$newCat->setDisplayOrder(1);
$newCat->setParent($repo->find(11)); //<-- This does not get saved in the DB
var_dump($newCat);
$em->persist($newCat);
$em->flush();
object(ModelBundle\Entity\Category)[421]
private 'id' => null
private 'slug' => string 'slug1' (length=42)
private 'name' => string 'Teste11' (length=33)
protected 'isActive' => boolean true
private 'description' => string 'desc1' (length=5)
private 'products' =>
object(Doctrine\Common\Collections\ArrayCollection)[422]
private 'elements' =>
array (size=0)
empty
private 'displayOrder' => int 1
private 'parent' =>
object(ModelBundle\Entity\Category)[435]
private 'id' => int 11
private 'slug' => string 'entertaining' (length=12)
private 'name' => string 'Browse Entertaining' (length=19)
protected 'isActive' => boolean false
private 'description' => string 'Selection of great food for any occasion.' (length=41)
private 'products' =>
object(Doctrine\ORM\PersistentCollection)[458]
private 'snapshot' =>
array (size=0)
protected 'initialized' => boolean false
private 'displayOrder' => null
private 'parent' => null
orm:
auto_generate_proxy_classes: "%kernel.debug%"
entity_managers:
default:
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
metadata_cache_driver: apcu
query_cache_driver: apcu