5

我试图通过运行任务来生成一个简单的 Doctrine2 CRUD,php app/console doctrine:generate:crud但在它结束之前我收到了这个错误:

[Doctrine\ORM\Mapping\MappingException] 在“ProductBundle\Entity\ListHasProduct#list”中找不到目标实体 ProductBundle\Entity\KList。

如果在我的实体中我与该实体没有关系,它为什么要寻找它?这是我的实体代码:

<?php

namespace Wuelto\BankRulesBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * @ORM\Table(name="bank_rules")
 * @ORM\Entity
 * @Gedmo\SoftDeleteable(fieldName="deletedAt")
 */
class BankRules {

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

    /**
     * 
     * @ORM\ManyToOne(targetEntity="BankBundle\Entity\NBank" )      
     * @ORM\JoinColumn(name="n_bank", referencedColumnName="id")   
     */
    protected $n_bank;

    /**
     * @ORM\Column(type="string", length=255)      
     */
    protected $regex;

    /**
     * @ORM\Column(type="text")      
     */
    protected $action;

    /**
     * @Gedmo\Timestampable(on="create")
     * @ORM\Column(name="created", type="datetime")
     */
    protected $created;

    /**
     * @Gedmo\Timestampable(on="update")
     * @ORM\Column(name="modified", type="datetime")
     */
    protected $modified;

    /**
     * @ORM\Column(name="deletedAt", type="datetime", nullable=true)
     */
    protected $deletedAt;

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

    public function setRegex($regex) {
        $this->regex = $regex;
        return true;
    }

    public function getRegex() {
        return $this->regex;
    }

    public function setAction($action) {
        $this->action = $action;
    }

    public function getAction() {
        return $this->action;
    }

    public function setCreated($created) {
        $this->created = $created;
    }

    public function getCreated() {
        return $this->created;
    }

    public function setModified($modified) {
        $this->modified = $modified;
    }

    public function getModified() {
        return $this->modified;
    }

    public function getDeletedAt() {
        return $this->deletedAt;
    }

    public function setDeletedAt($deletedAt) {
        $this->deletedAt = $deletedAt;
    }

}

我该如何解决这个问题?

更新 1

ListHastProduct我通过在实体上修复它来修复第一个错误:

/**
   * @ORM\ManyToOne(targetEntity="UserBundle\Entity\KList", inversedBy="products" )
   * @ORM\JoinColumns(@ORM\JoinColumn(name="kuser", referencedColumnName="kuser"),
   *                  @ORM\JoinColumn(name="klist", referencedColumnName="name"))   
   */
  protected $list;

但现在我得到了另一个错误:

[Doctrine\ORM\Mapping\MappingException] 实体 ShoppingBundle\Entity\BillDetail 中的复合主键不允许使用单个 id

这是我的BillDetail定义:

<?php

namespace ShoppingBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * @ORM\Entity
 * @ORM\Table(name="bill_detail")
 */
class BillDetail {

    /**
     * @ORM\Id
     * @ORM\Column(type="int",length=11)
     * @ORM\GeneratedValue
     */
    protected $id;

    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="ShoppingBundle\Entity\Transaction")
     * @ORM\JoinColumn(name="ktransaction", referencedColumnName="id")
     */
    protected $transaction;

    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="UserBundle\Entity\User")
     * @ORM\JoinColumn(name="kuser", referencedColumnName="id")
     */
    protected $user;

    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="CatalogBundle\Entity\KCatalog")
     * @ORM\JoinColumn(name="kcatalog", referencedColumnName="id")
     */
    protected $catalog;

    /**
     * @ORM\Column(type="decimal")
     */
    protected $amount;

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

    /**
     * @ORM\Column(name="created", type="datetime")
     * @Gedmo\Timestampable(on="create")
     */
    protected $created;

    /**
     * @ORM\Column(name="modified", type="datetime")
     * @Gedmo\Timestampable(on="update")
     */
    protected $modified;

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

    public function setUser(\UserBundle\Entity\User $user) {
        $this->user = $user;
    }

    public function getUser() {
        return $this->user;
    }

    public function setCatalog(\CatalogBundle\Entity\KCatalog $catalog) {
        $this->catalog = $catalog;
    }

    public function getCatalog() {
        return $this->catalog;
    }

    public function setTransaction(\ShoppingBundle\Entity\Transaction $transaction) {
        $this->transaction = $transaction;
    }

    public function getTransaction() {
        return $this->transaction;
    }

    public function setAmount($amount) {
        $this->amount = $amount;
    }

    public function getAmount() {
        return $this->amount;
    }

    public function setStatus($status) {
        $this->status = $status;
    }

    public function getStatus() {
        return $this->status;
    }

    public function setCreated($created) {
        $this->created = $created;
    }

    public function getCreated() {
        return $this->created;
    }

    public function setModified($modified) {
        $this->modified = $modified;
    }

    public function getModified() {
        return $this->modified;
    }

}
4

1 回答 1

8

[Doctrine\ORM\Mapping\MappingException] 实体 ShoppingBundle\Entity\BillDetail 中的复合主键不允许使用单个 id

BillDetail一个复合主键:

  • ID
  • 用户
  • 交易
  • 目录

因此,有两种方法可以避免此错误:

  1. 从其他属性中删除@ORM\Id注释,以便将 ID 作为唯一 ID 字段
  2. 使用复合键触发 CRUD 查询,例如,您通常可以这样做:

     // find by id
    $bill = em->find('ShoppingBundle\Entity\BillDetail', 3);
    

    根据您的映射,您必须通过以下方式进行:

    // find by composite key
    $bill = em->find('ShoppingBundle\Entity\BillDetail', array(
        'id'=> $idRequested, 
        'user' => $userRequested, 
        'transaction' => $transactionRequested, 
        'catalog' => $catalogRequested 
    ));
    
于 2013-10-30T14:51:04.657 回答