坚持doctrine2教程错误“没有为实体指定的标识符/主键”产品“。每个实体必须有一个标识符/主键。”

时间:2015-12-14 15:09:15

标签: php symfony doctrine-orm

page上复制粘贴示例后,我收到此错误:

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

我搜索了一下,found out代码中缺少实体注释,所以我最终得到了这段代码:

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

产品创建者也取自教程:     

// 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";

产品定义如下:

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

我也尝试了here指令,尽管他们没有给我任何结果。 我对学说很新,所以你对下一步该尝试有什么想法吗?

1 个答案:

答案 0 :(得分:0)

找到我的答案here

添加后似乎php映射不正确:

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

而不是:

* @var integer $id

到它工作的Product类。