gpt4 book ai didi

symfony - 尝试生成 Doctrine2 CRUD 时遇到问题

转载 作者:行者123 更新时间:2023-12-02 16:49:20 25 4
gpt4 key购买 nike

我尝试通过运行任务 php app/console Doctrine:generate:crud 来生成一个简单的 Doctrine2 CRUD,但在结束之前出现此错误:

[Doctrine\ORM\Mapping\MappingException] The target-entity ProductBundle\Entity\KList cannot be found in 'ProductBundle\Entity\ListHasProduct#list'.

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

<?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] Single id is not allowed on composite primary key in entity ShoppingBundle\Entity\BillDetail

这是我的 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;
}

}

最佳答案

[Doctrine\ORM\Mapping\MappingException] Single id is not allowed on composite primary key in entity ShoppingBundle\Entity\BillDetail

BillDetail 有一个复合主键:

  • 身份证
  • 用户
  • 交易
  • 目录

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

  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
    ));

关于symfony - 尝试生成 Doctrine2 CRUD 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19683811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com