简体   繁体   中英

Doctrine Association Mapping OneToMany Bidirectional do not work

i use doctrine ORM for generate a association one-to-many bidirectional, but when i execute the orm:validation-scheme command, this shows me the mesaje below:

"C:\\xampp\\htdocs\\Gestor\\vendor\\bin>doctrine-module orm:validate-schema [Mapping] FAIL - The entity-class 'Empleados\\Entity\\TipoDocumento' mapping is i nvalid: * The association Empleados\\Entity\\TipoDocumento#empleados refers to the owning side field Empleados\\Entity\\Empleado#tipodocumento which does not exist.

[Database] FAIL - The database schema is not in sync with the current mapping fi le."

The code: Empleado class (many to one side)

<?php
namespace Empleados\Entity;

use Doctrine\Common\Collections\ArrayCollection as Collection;
use Empresas\Entity\Empresa;
use Empleados\Entity\TipoDocumento;
use Doctrine\ORM\Mapping as ORM;
use Documentos\Entity\Documentacion_Empleado;

/**
 * @ORM\Entity
 * @ORM\Table(name="empleado")
 * 
 */

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

     /**
      * @ORM\Column(type="string",length=30,nullable=false,unique=true)
      */
     private $nro_documento;

    /*
     * @ORM\ManyToOne(targetEntity="Empleados\Entity\TipoDocumento",inversedBy="empleados")
     * @ORM\JoinColumn(name="tipodocumento_id", referencedColumnName="id")
     */
    private $tipodocumento;

 //...

 }

The TipoDocumento class (one to many side):

<?php
// yes, the class are in the same namespace "Empleados"
namespace Empleados\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Empleados\Entity\Empleado;

/**
 * @ORM\Entity
 * @ORM\Table(name="tipo_documento")
 */

class TipoDocumento
{

    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\OneToMany(targetEntity="Empleados\Entity\Empleado", mappedBy="tipodocumento"))
     */
    private $empleados;

 //.....
    public function __construct()
    {
        $this->empleados = new ArrayCollection();
    }

 }

I'm based on the Doctrine documentation example in http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html

In class TipoDocumento , private $empleados; should be private $empleado; .

Edit Sorry that's right, I was looking at the wrong place in the documentation.

The Many-to-one has the plural there. It also contains something like:

public function __construct() {
    $this->empleados = new ArrayCollection();
}

I can't tell if your class contains this function.

jmarkmurphy thanks for your help. The problem was in the camelcase of the "TipoDocumento" class, for some reason Doctrine does not like the camelcase ... What I did was rename the class to Tipo_documento and with that change everything started to work fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM