Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 停留在doctrine2教程错误“上”;没有为实体“指定标识符/主键”;“产品”;。每个实体必须有一个标识符/主键&引用;_Php_Symfony_Doctrine Orm - Fatal编程技术网

Php 停留在doctrine2教程错误“上”;没有为实体“指定标识符/主键”;“产品”;。每个实体必须有一个标识符/主键&引用;

Php 停留在doctrine2教程错误“上”;没有为实体“指定标识符/主键”;“产品”;。每个实体必须有一个标识符/主键&引用;,php,symfony,doctrine-orm,Php,Symfony,Doctrine Orm,复制粘贴示例后,如上所示,我收到以下错误: [Doctrine\ORM\Mapping\MappingException] No identifier/primary key specified for Entity "Product". Every Entity must have an identifier/primary k

复制粘贴示例后,如上所示,我收到以下错误:

[Doctrine\ORM\Mapping\MappingException]                                                                                
    No identifier/primary key specified for Entity "Product". Every Entity must have an identifier/primary key. 
我搜索了一下,发现代码中缺少一个实体注释,因此我最终得到了以下代码:

<?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);
产品定义如下:

<?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;
    }
}
找到了我的答案

添加以下内容后,php映射似乎不正确:

   /**
     * @Id @Column(type="integer")
     * @GeneratedValue
     */
而不是:

* @var integer $id

请记住,您的问题不是由
@var
注释引起的。您的开场白中的注释是以Symfony样式定义的,使用别名
ORM
作为导入的名称空间
doctor\ORM\Mapping作为ORM。由于您使用的是作为独立供应商的条令,因此不需要
@ORM
前缀。非常感谢!我发现很难绕过条令文件实际上似乎没有一个地方可以开始。我计划将它与php和mysql一起使用。您有什么可以推荐给我阅读的链接吗?我相信现在的文档已经足够让您入门了。你需要知道的关于教义的一切,都应该在那里找到。你可以在这里再看一眼,看看你的问题。
* @var integer $id