简体   繁体   English

Doctrine 和 Symfony2:关联是指不存在错误的反面

[英]Doctrine and Symfony2 : Association refers to inverse side that doesn't exist error

I have a page address.html.twig , the user can add many addresses in the table UserAddress.我有一个页面 address.html.twig ,用户可以在表 UserAddress 中添加许多地址。 when he added his address in the database , the address should be render in the same page that he added his address then he can choose which one he would like to use.当他在数据库中添加他的地址时,地址应该在他添加他的地址的同一页面中呈现,然后他可以选择他想要使用的那个。 Unfortunately the address is not render.不幸的是,该地址未呈现。

First i thought that i have a problem in my controller action or in my twig page.首先,我认为我的控制器操作或树枝页面有问题。 I even asked a question here about it => here我什至在这里问了一个问题 => 在这里

I verified all my tables in phpmyadmin and all of them are well link but if i'm doing this: php app/console doctrine:schema:validate i have this error :我在 phpmyadmin 中验证了我的所有表,并且所有表都链接良好,但是如果我这样做:php app/console police:schema:validate 我有这个错误:

[Mapping] FAIL - The entity-class 'FLY\\BookingsBundle\\Entity\\Commandes' mapping is invalid: * The association FLY\\BookingsBundle\\Entity\\Commandes#user refers to the inverse side field Application\\Sonata\\UserBundle\\Entity\\User#commandes which does not exist. [映射] FAIL - 实体类 'FLY\\BookingsBundle\\Entity\\Commandes' 映射无效: * 关联 FLY\\BookingsBundle\\Entity\\Commandes#user 指的是反向侧字段 Application\\Sonata\\UserBundle\\Entity\\User#不存在的命令。

[Mapping] FAIL - The entity-class 'FLY\\BookingsBundle\\Entity\\UserAddress' mapping is invalid: * The association FLY\\BookingsBundle\\Entity\\UserAddress#user refers to the inverse side field Application\\Sonata\\UserBundle\\Entity\\User#address which does not exist. [映射] FAIL - 实体类 'FLY\\BookingsBundle\\Entity\\UserAddress' 映射无效: * 关联 FLY\\BookingsBundle\\Entity\\UserAddress#user 指的是反向侧字段 Application\\Sonata\\UserBundle\\Entity\\User#不存在的地址。

Have a look at this picture:看看这张照片:

在此处输入图片说明

This is my UserAddress.php这是我的 UserAddress.php

/**
 * @ORM\ManyToOne(targetEntity="Application\Sonata\UserBundle\Entity\User", inversedBy="address")
 * @ORM\JoinColumn(nullable=true)
 */
private $user;

Commandes.php命令文件

/**
 * @ORM\ManyToOne(targetEntity="Application\Sonata\UserBundle\Entity\User", inversedBy="commandes")
 * @ORM\JoinColumn(nullable=true)
 */
private $user;

User.php用户名

/**
 * @ORM\Entity(repositoryClass="FLY\UserBundle\Repository\UserRepository")
 * @ORM\Table(name="fos_user_user")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */


    protected $id;

    public function __construct()
    {
        parent::__construct();
        $this->commandes = new \Doctrine\Common\Collections\ArrayCollection();
        $this->address = new \Doctrine\Common\Collections\ArrayCollection();
    }


    /**
     * @ORM\OneToMany(targetEntity="FLY\BookingsBundle\Entity\Commandes", mappedBy="user", cascade={"remove"})
     * @ORM\JoinColumn(nullable=true)
     */
    private $commandes;



    /**
     * @ORM\OneToMany(targetEntity="FLY\BookingsBundle\Entity\UserAddress", mappedBy="user", cascade={"remove"})
     * @ORM\JoinColumn(nullable=true)
     */
    private $address;

Here you can see my var dump:在这里你可以看到我的 var dump:

User {#124 ▼
  #id: 21
  -commandes: null
  -address: null
}

I've had an issue which has popped up 2-3 times in the last few years, where the mappings were incorrect but the schema update was successful.我遇到了一个问题,在过去几年中出现了 2-3 次,其中映射不正确但架构更新成功。 After the mappings were fixed this wasn't reflected in the schema and symfony assumed it was already up-to-date.映射被修复后,这并没有反映在模式中,symfony 假设它已经是最新的。

I recommend you try removing the relevent relationships manually from your user, commande and address tables and then run:我建议您尝试从用户、命令和地址表中手动删除相关关系,然后运行:

php app/console doctrine:schema:update --force

- it may fix your issue. - 它可能会解决您的问题。

Heres an example from one of my apps - I've done this for your commandes entity.这是我的一个应用程序中的一个示例 - 我已经为您的命令实体完成了此操作。 You'll be able to piece together your UserAddress Entity from this example yourself!您将能够自己从这个示例中拼凑出您的 UserAddress 实体!

Here goes:开始:

User.php用户名

 /**
 * @ORM\OneToMany(targetEntity="FLY\BookingsBundle\Entity\Commandes", mappedBy="commandesUser")
 */
protected $commandes;

User.php - Getters and Setters User.php - Getter 和 Setter

/**
 * Add commandes
 *
 * @param FLY\BookingsBundle\Entity\Commandes $commandes
 */
public function addCommandes(\FLY\BookingsBundle\Entity\Commandes $commandes)
{
    $this->commandes[] = $commandes;
}

/**
 * Get commandes
 *
 * @return Doctrine\Common\Collections\Collection
 */
public function getCommandes()
{
    return $this->commandes;
}

Commandes.php命令文件

/**
* @ORM\ManyToOne(targetEntity="Application\Sonata\UserBundle\Entity\User", inversedBy="commandes")
* @ORM\JoinColumn(name="user", referencedColumnName="id")
*/
private $commandesUser;

Commandes.php - Getters and Setters Commandes.php - Getter 和 Setter

/**
 * Set commandesUser
 *
 * @param Application\Sonata\UserBundle\Entity\User $commandesUser
 */
public function setCommandesUser(\Application\Sonata\UserBundle\Entity\User $commandesUser = null)
{
    $this->commandesUser = $commandesUser;
}

/**
 * Get $commandesUser
 *
 * @return Application\Sonata\UserBundle\Entity\User
 */
public function getCommandesUser()
{
    return $this->commandesUser;
}

It's quite likely this doesn't happen to anyone else, but there's a chance.这很可能不会发生在其他任何人身上,但有机会。

In my case, this error appeared because there was a duplicate.就我而言,出现此错误是因为存在重复项。 My entity had 2 fields, which are ManyToOne relationships.我的实体有 2 个字段,它们是ManyToOne关系。 And they both had the same inversed names, which gave this error.他们都有相同的反名,这导致了这个错误。

So this is the relevant bit of code:所以这是相关的代码:

class TaskIngredient
{

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Ingredient", inversedBy="taskIngredients")
     * @ORM\JoinColumn(nullable=false)
     */
    private $ingredient;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Task", inversedBy="taskIngredients")
     * @ORM\JoinColumn(nullable=false)
     */
    private $task;
}

The solution was relatively easy.解决方案相对简单。 I tried changing the inversedBy name, manually.我尝试手动更改inversedBy名称。 However this didn't fix it (even after applying php app/console doctrine:schema:update --force and removing the var/cache folder).然而这并没有解决它(即使在应用了php app/console doctrine:schema:update --force并删除了var/cache文件夹之后)。

So I just:所以我只是:

  1. Removed one of the problematic entities (and it's setter/getter)删除了有问题的实体之一(它是 setter/getter)
  2. Ran the php bin/console make:entity tool and readded the field with a different name运行php bin/console make:entity工具并使用不同的名称读取该字段
  3. Voilà!瞧! Issue fixed.问题已修复。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 主义和Symfony2 ManyToMany:关联指不存在错误的反面 - Doctrine and Symfony2 ManyToMany: Association refers to inverse side that doesn't exist error Symfony映射错误:“映射彼此不一致”和“关联是指不存在的反侧字段” - Symfony Mapping Error: “The mappings are inconsistent with each other” & “The association refers to the inverse side field which does not exist” Symfony2-关联是指不存在的拥有方 - Symfony2 - Association refers to the owning side which does not exist 指不存在的反侧字段。 教义2 - refers to the inverse side field which does not exist. Doctrine2 Symfony2-映射无效是指不存在的拥有方 - Symfony2 - Mapping is invalid refers to the owning side which does not exist Doctrine & Symfony3 OneToOne 关系,反面不存在 - Doctrine & Symfony3 OneToOne relations, inverse side does not exist Symfony2-教义:setMaxResults()不起作用 - Symfony2 - Doctrine : setMaxResults() doesn't work 关联X指的是未被定义为关联的逆侧字段Y. - The association X refers to the inverse side field Y which is not defined as association Symfony / Doctrine“指的是不存在的拥有方字段” - 但属性存在于课堂中 - Symfony / Doctrine “refers to the owning side field which does not exist” - but property is present in class “关联指的是反面域”&“映射与自身不一致” - “association refers to the inverse side field” & “mappings are inconsistent with self”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM