dongyue7796 2015-12-14 15:09
浏览 53

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

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() . "
";

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?

  • 写回答

1条回答 默认 最新

  • douyi7055 2015-12-14 15:18
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题