这对我来说很奇怪。 我正在使用symfony 3并尝试使用2个不同的实体创建2个表。两个名字都与ManyToOne联系在一起。清除缓存和更新原则后,两个字段都没有链接在一起。
所以我做了一个简单的OneToOne和2个类似的实体
用户:
'/**
* @ORM\Entity
* @ORM\Table(name="md_user")
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
*/
class User
{
/**
* @ORM\Id
* @ORM\Column(type="integer", unique=true)
* @Assert\NotBlank()
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type = "integer")
* @ORM\OneToOne(targetEntity="Userdata")
* @ORM\JoinColumn(name="userdata_id", referencedColumnName="id")
*/
protected $cid;'
使用用户数据:
'/**
* MdUser
*
* @ORM\Table(name="md_userdata")
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserdataRepository")
*/
class Userdata
{
/**
* @var id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Id
* @ORM\OneToOne(targetEntity="AppBundle\Entity\User", inversedBy="cid")
*/
protected $id;
/**
* @var string
* @ORM\Column(name="last_name", type="string", length=255)
*/
protected $last_name;
'
在symfony中更新Doctrine之后没有任何改变。有人能告诉我,我做错了什么?
添加了config.yml:
'imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
locale: de
framework:
cache:
pools:
app.cache.doctrine_orm:
adapter: cache.system
esi: ~
translator:
fallbacks: ['%locale%']
paths: ['%kernel.project_dir%/app/Resources/translations']
session:
handler_id: Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler
secret: '%secret%'
router:
resource: '%kernel.project_dir%/app/config/routing.yml'
strict_requirements: ~
form: ~
csrf_protection: ~
validation:
enable_annotations: true
enabled: true
#serializer: { enable_annotations: true }
templating:
engines: ['twig']
default_locale: '%locale%'
trusted_hosts: ~
fragments: ~
http_method_override: true
assets: ~
# base_path: ./../Resources/views/Frontend/
php_errors:
log: true
# Twig Configuration
twig:
debug: '%kernel.debug%'
strict_variables: '%kernel.debug%'
# globals:
# fos_csrf_provider: "@form.csrf_provider"
fos_user:
db_driver: orm
firewall_name: main
user_class: AppBundle\Entity\User
from_email:
address: "%mailer_user%"
sender_name: "%mailer_user%"
knp_menu:
# if true, enables the helper for PHP templates
templating: true
# the renderer to use, list is also available by default
default_renderer: twig
# Doctrine Configuration
doctrine:
dbal:
driver: pdo_mysql
host: '%database_host%'
port: '%database_port%'
dbname: '%database_name%'
user: '%database_user%'
password: '%database_password%'
charset: UTF8
orm:
auto_generate_proxy_classes: '%kernel.debug%'
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
metadata_cache_driver:
type: service
id: app.doctrine_orm.cache
result_cache_driver:
type: service
id: app.doctrine_orm.cache
query_cache_driver:
type: service
id: app.doctrine_orm.cache
second_level_cache:
enabled: true
# Swiftmailer Configuration
swiftmailer:
transport: '%mailer_transport%'
host: '%mailer_host%'
username: '%mailer_user%'
password: '%mailer_password%'
spool: { type: memory }
# ...
assetic:
debug: '%kernel.debug%'
use_controller: '%kernel.debug%'
filters:
cssrewrite: ~
'
services.yml
'# Learn more about services, parameters and containers at
# https://symfony.com/doc/current/service_container.html
parameters:
#parameter_name: value
services:
Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler:
public: false
arguments:
- 'mysql:host=%database_host%;port=%database_port%;dbname=%database_name%'
- { db_table: md_sessions, db_username: '%database_user%', db_password: '%database_password%' }
# default configuration for services in *this* file
_defaults:
# automatically injects dependencies in your services
autowire: true
# automatically registers your services as commands, event subscribers, etc.
autoconfigure: true
# this means you cannot fetch services directly from the container via $container->get()
# if you need to do this, you can override this setting on individual services
public: false
# makes classes in src/AppBundle available to be used as services
# this creates a service per class whose id is the fully-qualified class name
AppBundle\:
resource: '../../src/AppBundle/*'
# you can exclude directories or files
# but if a service is unused, it's removed anyway
exclude: '../../src/AppBundle/{Entity,Repository,Tests}'
# controllers are imported separately to make sure they're public
# and have a tag that allows actions to type-hint services
AppBundle\Controller\:
resource: '../../src/AppBundle/Controller'
public: true
tags: ['controller.service_arguments']
app.session_handler:
class: 'AppBundle\EventListener\MandantListener'
arguments: ["@doctrine.orm.entity_manager"]
tags:
- { name: kernel.event_listener, event: kernel.request }
app.template_listener:
class: 'AppBundle\EventListener\TemplateListener'
arguments: ["@twig"]
tags:
- { name: kernel.event_listener, event: kernel.controller, method: onKernelController }
app.doctrine_orm.cache:
class: Symfony\Component\Cache\DoctrineProvider
arguments: ["@app.cache.doctrine_orm"]
app.locale_listener:
class: 'AppBundle\EventListener\LocaleSubscriber'
arguments: ["%kernel.default_locale%"]
tags: [kernel.event_subscriber]
'
答案 0 :(得分:1)
您似乎有一个冲突的声明:无法自动生成列以及同时与另一个实体建立关系。
因此,请更改您的UserData
$id
以反映:删除自动生成,以及这些内容:
class Userdata
{
/**
* @var id
* @ORM\Column(type="integer")
* @ORM\OneToOne(targetEntity="AppBundle\Entity\User", inversedBy="cid")
*/
protected $id;
}
...但这并不是一个好习惯,因为无论如何你需要一个唯一的ID。 所以我会采用不同的路线并创建一个新字段来保存该特定关系。原始ID会自动生成,并为cid
的关系创建一个新字段。
PS:同样,您在@ORM\Entity
上声明User
两次。没什么大不了的,但是你知道