【问题标题】:Mapping FAIL - The entity-class 'PI\UnidadBundle\Entity\HorasPorUnidad' mapping is invalid映射失败 - 实体类 'PI\UnidadBundle\Entity\HorasPorUnidad' 映射无效
【发布时间】:2013-12-03 04:50:09
【问题描述】:

我在这里遇到了问题,我不知道如何解决它。看看我有这两个实体:

<?php

namespace PI\ProyectoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Event\LifecycleEventArgs;

/**
 * @ORM\Entity
 * @ORM\Table(name="proyectos")
 * @ORM\Entity(repositoryClass="PI\ProyectoBundle\Entity\Repository\ProyectosRepository")
 * @ORM\HasLifecycleCallbacks
 */
class Proyectos {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=45, unique=true, nullable=false)
     */
    protected $nombre;

    /**
     * @ORM\Column(type="boolean", nullable=true)
     */
    protected $estado;

    /**
     * @ORM\Column(type="string", length=45, nullable=false)
     */
    protected $pais;

    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="PI\ClienteBundle\Entity\Clientes", cascade={"all"})
     * @ORM\JoinColumn(name="cliente", referencedColumnName="id")
     */
    protected $clientes;

    /**
     * @ORM\ManyToMany(targetEntity="PI\CentroBundle\Entity\Centros", inversedBy="proyectos", cascade={"persist"})
     * @ORM\JoinTable(name="proyectos_has_centros",
     *      joinColumns={@ORM\JoinColumn(name="proyectos_id", referencedColumnName="id"),
     *                   @ORM\JoinColumn(name="proyectos_cliente", referencedColumnName="cliente")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="centros_id", referencedColumnName="id")}
     *      )
     */
    protected $centros;

    /**
     * @ORM\ManyToMany(targetEntity="Application\Sonata\UserBundle\Entity\User", cascade={"persist"})
     * @ORM\JoinTable(name="proyectos_has_system_user",
     *      joinColumns={@ORM\JoinColumn(name="proyectos_id", referencedColumnName="id"),
     *                   @ORM\JoinColumn(name="proyectos_cliente", referencedColumnName="cliente")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="system_user_id", referencedColumnName="id")}
     *      )
     */
    protected $ingenieros;

    public function __construct() {
        $this->centros = new \Doctrine\Common\Collections\ArrayCollection();
        $this->ingenieros = new \Doctrine\Common\Collections\ArrayCollection();
    }

    public function getId() {
        return $this->id;
    }

    public function setNombre($nombre) {
        $this->nombre = $nombre;
    }

    public function getNombre() {
        return $this->nombre;
    }

    public function setEstado($estado) {
        $this->estado = $estado;
    }

    public function getEstado() {
        return $this->estado;
    }

    public function setPais($pais) {
        $this->pais = $pais;
    }

    public function getPais() {
        return $this->pais;
    }

    public function setClientes(\PI\ClienteBundle\Entity\Clientes $clientes) {
        $this->clientes = $clientes;
    }

    public function getClientes() {
        return $this->clientes;
    }

    public function setCentros(\PI\CentroBundle\Entity\Centros $centros) {
        $this->centros[] = $centros;
    }

    public function getCentros() {
        return $this->centros;
    }

    public function setIngenieros(\BIT\UserBundle\Entity\User $ingenieros) {
        $this->ingenieros[] = $ingenieros;
    }

    public function getIngenieros() {
        return $this->ingenieros;
    }

    /**
     * @ORM\PrePersist
     */
    public function prePersist(LifecycleEventArgs $eventArgs) {
        $em = $eventArgs->getEntityManager();
        $q = $em->createQuery('SELECT MAX(p.id) FROM PIProyectoBundle:Proyectos p');
        $id = $q->getSingleResult(\Doctrine\ORM\Query::HYDRATE_SINGLE_SCALAR);
        $this->id = $id + 1;
    }

}

还有这个:

<?php

namespace PI\UnidadBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="horas_por_proyectos")
 */
class HorasPorUnidad {

    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="Application\Sonata\UserBundle\Entity\User", cascade={"all"})
     * @ORM\JoinColumn(name="fos_user_user_id", referencedColumnName="id")
     */
    protected $fos_user_user_id;

    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="PI\UnidadBundle\Entity\Unidades", cascade={"all"})
     * @ORM\JoinColumn(name="fos_user_user_id", referencedColumnName="id")
     */
    protected $unidades_id;

    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="PI\CentroBundle\Entity\Centros", cascade={"all"})
     * @ORM\JoinColumn(name="centros_id", referencedColumnName="id")
     */
    protected $centros_id;

    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="PI\ProyectoBundle\Entity\Proyectos", cascade={"all"})
     * @ORM\JoinColumn(name="proyectos_id", referencedColumnName="id")
     */
    protected $proyectos_id;
   
    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="PI\ClienteBundle\Entity\Clientes", cascade={"all"})
     * @ORM\JoinColumn(name="proyectos_cliente", referencedColumnName="id")
     */
    protected $proyectos_cliente;

    /**
     * @ORM\Column(type="date")
     */
    protected $fecha;

    /**
     * @ORM\Column(type="integer")
     */
    protected $horas;

    public function setUserId(\Application\Sonata\UserBundle\Entity\User $user) {
        $this->fos_user_user_id = $user;
    }
   
    public function getUserId() {
        return $this->fos_user_user_id;
    }
   
    public function setUnidadesId(\PI\UnidadBundle\Entity\Unidades $unidad) {
        $this->unidades_id = $unidad;
    }
   
    public function getUnidadesId() {
        return $this->unidades_id;
    }
   
    public function setCentrosId(\PI\CentroBundle\Entity\Centros $centro) {
        $this->centros_id = $centro;
    }
   
    public function getCentrosId() {
        return $this->centros_id;
    }

    public function setHoras($cantidad_horas) {
        $this->horas = $cantidad_horas;
    }

    public function getHoras() {
        return $this->horas;
    }

    public function setFecha($fecha) {
        $this->fecha = $fecha;
    }

    public function getFecha() {
        return $this->fecha;
    }

}

但是当我运行任务 php app/console doctrine:schema:validate 时,我得到了这个错误:

[映射] FAIL - 实体类 'PI\UnidadBundle\Entity\HorasPorUnidad' 映射无效:

  • 无法将关联“PI\UnidadBundle\Entity\HorasPorUnidad#proyectos_id”映射为标识符,因为目标实体“PI\ProyectoBundle\Entity\Proyectos”也将关联映射为标识符。
  • 关联“proyectos_id”的连接列必须与目标实体“PI\UnidadBundle\Entity\HorasPorUnidad”的所有标识符列匹配,但是缺少“id, cliente”。

而且我不知道如何修复它们,请问专家有什么帮助吗?我需要在这里解决什么问题?

【问题讨论】:

  • 你最后做了什么?

标签: php symfony orm doctrine-orm symfony-2.3


【解决方案1】:

您在 ManyToOne 关联中获取 ma​​ppedByinversedBy 属性,并检查关联的另一面:

 /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="PI\ClienteBundle\Entity\Clientes",inversedBy="?" cascade={"all"})
     * @ORM\JoinColumn(name="cliente", referencedColumnName="id")
     */
    protected $clientes;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-13
    • 2012-05-25
    • 2018-01-15
    • 2011-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多