php - 使用 yml 配置向现有供应商包实体添加新字段

标签 php symfony doctrine-orm

我正在尝试将多对一关联添加到供应商包中的现有实体。

这是我在供应商包中的实体:-

class Post
{
    private $id;

    private $title;

    private $accroche;

    private $article;

    private $categories;

    private $comments;

    private $created;

    private $updated;

    private $publied;
}

而且,这是供应商包中 Post 实体的 orm 映射文件:-

Mv\BlogBundle\Entity\AdminBlog\Post:
  type:                 entity
  table:                null
  repositoryClass:      Mv\BlogBundle\Entity\AdminBlog\PostRepository
  id:
    id:
      type:             integer
      generator:
        strategy:       AUTO

  fields:
    title:
      type:             string
      length:           150
    accroche:
      type:             text
    article:
      type:             text
    created:
      type:             datetime
      gedmo:
        timestampable:
          on: create
    updated:
      type:             datetime
      gedmo:
        timestampable:
          on: update
    publied:
      type:             datetime

  manyToMany:
    categories:
      targetEntity:   Mv\BlogBundle\Entity\AdminBlog\Category
      inversedBy:     posts
      joinTable:
        name:         post_category
        joinColumns:
          post_id:
            referencedColumnName: id
            onDelete:             CASCADE
        inverseJoinColumns:
          category_id:
            referencedColumnName: id
            onDelete:             RESTRICT

  oneToMany:
    comments:
      targetEntity:   Mv\BlogBundle\Entity\AdminBlog\Comment
      mappedBy:       post

我创建了供应商包的子包。我研究了 mappedSuperclass 并尝试了其他继承方法。但是他们都没有为只是将关联添加到“Post”实体而工作。

我只想拥有如下内容(尝试将其添加到 orm 映射文件中但不起作用):-

manyToOne:
  user:
    targetEntity:   TP\Bundle\MainBundle\Entity\User
    joinColumns:
        user_id:
          referencedColumnName: id

最佳答案

作为解决方案,我可以建议 SonataEasyExtendsBundle .作为docs说,它解决的问题之一是:

If a developer wants to add a new property into a VB entity, the developer needs to create a new child entity with a custom mapping.

但它有一些requirements对于供应商 bundle 。您可以阅读短文 explanation并决定它是否适合您。

关于php - 使用 yml 配置向现有供应商包实体添加新字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25780556/

相关文章:

php - 分页在 Wordpress 的静态博客页面中不起作用

php - 如何获取 func_get_args 和默认值以与 call_user_func_array 一起使用

sql - Doctrine 查询 - 忽略空格

php - Symfony2/Doctrine2 : Issues managing multiple entity manager inside a listener

php - 我收到此错误代码 : Invalid argument supplied for foreach()

mysql - Doctrine 数据导入 - 进行后备的常用方法?

symfony 验证器错误名称常量

php - 带有 Symfony 和 PHP-FPM 的 NGINX SSI

mysql - 原则 2 DQL 安全

javascript - 如何在删除/添加内容时使内容滑动?