简体   繁体   中英

stuck on doctrine2 tutorial error “No identifier/primary key specified for Entity ”Product“. Every Entity must have an identifier/primary key. ”

After copy-pasting the example as is on the page I am getting this error:

[Doctrine\ORM\Mapping\MappingException]                                                                                
    No identifier/primary key specified for Entity "Product". Every Entity must have an identifier/primary key. 

I searched a bit and found out that an entity annotation was missing from the code so I ended up with this code:

<?php
// bootstrap.php

/**
* @Entity 
* @Table(name="Product")
* property int $id
* property string $name
*/

use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;

require_once "vendor/autoload.php";

// Create a simple "default" Doctrine ORM configuration for Annotations
$isDevMode = true;
$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/src"), $isDevMode);
// or if you prefer yaml or XML
//$config = Setup::createXMLMetadataConfiguration(array(__DIR__."/config/xml"), $isDevMode);
//$config = Setup::createYAMLMetadataConfiguration(array(__DIR__."/config/yaml"), $isDevMode);

// database configuration parameters
$conn = array(
    'driver' => 'pdo_sqlite',
    'path' => __DIR__ . '/db.sqlite',
);

// obtaining the entity manager
$entityManager = EntityManager::create($conn, $config);

The product creator is also taken from the tutorial:

// create_product.php
require_once "bootstrap.php";


$newProductName = $argv[1];

$product = new Product();
$product->setName($newProductName);

$entityManager->persist($product);
$entityManager->flush();

echo "Created Product with ID " . $product->getId() . "\n";

The Product is defined here:

<?php
/**
* @Entity 
* @Table(name="Product")
* property int $id
* property string $name
*/

// src/Product.php
class Product
{
    /**
     * @var integer $id
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
    /**
     * @var string
     */
    protected $name;

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

    public function getName()
    {
        return $this->name;
    }

    public function setName($name)
    {
        $this->name = $name;
    }
}

I also tried the instructions here though they did not give me any result. I am very new to doctrine so do you have any ideas on what to try next?

Found my answer here

It seems that the php mapping was not correct after adding:

   /**
     * @Id @Column(type="integer")
     * @GeneratedValue
     */

instead of:

* @var integer $id

to the Product class it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM