I trying to generate a simple Doctrine2 CRUD by running the task php app/console doctrine:generate:crud
but I get this error just before it ends:
[Doctrine\ORM\Mapping\MappingException] The target-entity ProductBundle\Entity\KList cannot be found in 'ProductBundle\Entity\ListHasProduct#list'.
Why it's looking for that if in my entity I have not relation to that entity? This is my entity code:
<?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;
}
}
How I can solve this?
UPDATE 1
I fix the first error by fixing this at ListHastProduct
entity:
/**
* @ORM\ManyToOne(targetEntity="UserBundle\Entity\KList", inversedBy="products" )
* @ORM\JoinColumns(@ORM\JoinColumn(name="kuser", referencedColumnName="kuser"),
* @ORM\JoinColumn(name="klist", referencedColumnName="name"))
*/
protected $list;
but now I get this other error:
[Doctrine\ORM\Mapping\MappingException] Single id is not allowed on composite primary key in entity ShoppingBundle\Entity\BillDetail
Here is my BillDetail
definition:
<?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;
}
}
Some of the code will not work with lower versions. Doctrine 2 is an object-relational mapper (ORM) for PHP 5.4+ that provides transparent persistence for PHP objects. It uses the Data Mapper pattern at the heart, aiming for a complete separation of your domain/business logic from the persistence in a relational database management system.
Some developers don’t start using doctrine in their application, just because, they find it a little difficult to get started (I listened so from some of my own friends too). And unluckily, I also faced some similar issue as well.
Remember, thanks to the Doctrine DBAL it doesn't matter too much which database we are using. This is one of the nice parts of Doctrine - it hides the complexity of the underlying database connectivity from us.
It uses the Data Mapper pattern at the heart, aiming for a complete separation of your domain/business logic from the persistence in a relational database management system. The benefit of Doctrine for the programmer is the ability to focus on the object-oriented business logic and worry about persistence only as a secondary problem.
[Doctrine\ORM\Mapping\MappingException] Single id is not allowed on composite primary key in entity ShoppingBundle\Entity\BillDetail
The BillDetail
has a composite primary key by:
So, there are 2 ways to avoid this error:
@ORM\Id
annotation from the other attributes in order to take the ID as unique ID fieldTrigger the CRUD queries with a composite key, for example, you generally could do this:
// find by id
$bill = em->find('ShoppingBundle\Entity\BillDetail', 3);
According to your mapping, you must do it by:
// find by composite key
$bill = em->find('ShoppingBundle\Entity\BillDetail', array(
'id'=> $idRequested,
'user' => $userRequested,
'transaction' => $transactionRequested,
'catalog' => $catalogRequested
));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With