Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting issues while trying to generate Doctrine2 CRUD

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

}
like image 743
Reynier Avatar asked Oct 30 '13 13:10

Reynier


People also ask

Does doctrine 2 work with lower versions of PHP?

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.

Why some developers don’t use doctrine in their application?

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.

Does it matter which database we are using in doctrine?

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.

What is doctrine and how does it work?

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.


1 Answers

[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:

  • ID
  • User
  • Transaction
  • Catalog

So, there are 2 ways to avoid this error:

  1. Remove the @ORM\Id annotation from the other attributes in order to take the ID as unique ID field
  2. Trigger 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 
    ));
    
like image 78
manix Avatar answered Nov 15 '22 09:11

manix