我尝试通过运行任务php app/console doctrine:generate:crud
来生成一个简单的Doctrine2 CRUD,但是我在它结束之前就得到了这个错误:
[Doctrine \ ORM \ Mapping \ MappingException]目标实体 无法找到ProductBundle \ Entity \ KList 'ProductBundle \实体\ ListHasProduct#列表'。
为什么它在寻找,如果在我的实体中我与该实体无关?这是我的实体代码:
<?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]不允许使用单个ID 实体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;
}
}
答案 0 :(得分:8)
[Doctrine \ ORM \ Mapping \ MappingException]不允许使用单个ID 实体ShoppingBundle \ Entity \ BillDetail中的复合主键
BillDetail
具有复合主键:
因此,有两种方法可以避免此错误:
@ORM\Id
注释,以便将ID作为唯一ID字段使用复合键触发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
));