2013-03-22 133 views
0

我有兩個表新聞和news_category。爲此,我使用symfony命令'doctrine:mapping:convert'創建了兩個映射類。這兩個文件如下。Symfony2自動生成實體錯誤。

  1. news.orm.yml。

    News: 
        type: entity 
        table: news 
        fields: 
         newsId: 
          id: true 
          type: integer 
          unsigned: false 
          nullable: false 
          column: news_id 
          generator: 
           strategy: IDENTITY 
        newsTitle: 
         type: string 
         length: 255 
         fixed: false 
         nullable: false 
         column: news_title 
        newsDescription: 
         type: text 
         nullable: false 
         column: news_description 
        newsStatus: 
         type: string 
         length: 255 
         fixed: false 
         nullable: false 
         column: news_status 
        createdAt: 
         type: date 
         nullable: false 
         column: created_at 
        manyToOne: 
        category: 
         targetEntity: NewsCategory 
         cascade: { } 
         mappedBy: null 
         inversedBy: null 
         joinColumns: 
          category_id: 
           referencedColumnName: category_id 
         orphanRemoval: false 
    lifecycleCallbacks: { } 
    

2)。 NewCategory.orm.yml

NewsCategory: 
type: entity 
table: news_category 
fields: 
    categoryId: 
     id: true 
     type: integer 
     unsigned: false 
     nullable: false 
     column: category_id 
     generator: 
      strategy: IDENTITY 
    categoryTitle: 
     type: string 
     length: 255 
     fixed: false 
     nullable: false 
     column: category_title 
    categoryDescription: 
     type: text 
     nullable: false 
     column: category_description 
lifecycleCallbacks: { } 

後,我已經使用了另一個symfony的命令「的信條:映射:導入」使用這個我再次生成兩個文件中的實體目錄News.php和NewsCategory.php

這是如下。

1)news.php

<?php 

    namespace Admin\NewsBundle\Entity; 

    use Doctrine\ORM\Mapping as ORM; 

/** 
    * News 
    * 
    * @ORM\Table(name="news") 
    * @ORM\Entity 
    */ 
    class News 
{ 
/** 
* @var integer 
* 
* @ORM\Column(name="news_id", type="integer", nullable=false) 
* @ORM\Id 
* @ORM\GeneratedValue(strategy="IDENTITY") 
*/ 
private $newsId; 

/** 
* @var string 
* 
* @ORM\Column(name="news_title", type="string", length=255, nullable=false) 
*/ 
private $newsTitle; 

/** 
* @var string 
* 
* @ORM\Column(name="news_description", type="text", nullable=false) 
*/ 
private $newsDescription; 

/** 
* @var string 
* 
* @ORM\Column(name="news_status", type="string", length=255, nullable=false) 
*/ 
private $newsStatus; 

/** 
* @var \DateTime 
* 
* @ORM\Column(name="created_at", type="date", nullable=false) 
*/ 
private $createdAt; 

/** 
* @var \NewsCategory 
* 
* @ORM\ManyToOne(targetEntity="NewsCategory") 
* @ORM\JoinColumns({ 
* @ORM\JoinColumn(name="category_id", referencedColumnName="category_id") 
* }) 
*/ 
private $category; 

} 

和2)NewCategory.php

namespace Admin\NewsBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* NewsCategory 
* 
* @ORM\Table(name="news_category") 
* @ORM\Entity 
*/ 
class NewsCategory 
{ 
/** 
* @var integer 
* 
* @ORM\Column(name="category_id", type="integer", nullable=false) 
* @ORM\Id 
* @ORM\GeneratedValue(strategy="IDENTITY") 
*/ 
private $categoryId; 

/** 
* @var string 
* 
* @ORM\Column(name="category_title", type="string", length=255, nullable=false) 
*/ 
private $categoryTitle; 

/** 
* @var string 
* 
* @ORM\Column(name="category_description", type="text", nullable=false) 
*/ 
private $categoryDescription; 

} 

問題是現在當我創建使用的實體 「學說:生成:實體」 它給了我以下錯誤。

D:\wamp\www\Symfony>php app/console doctrine:generate:entities AdminNewsBundle 
Generating entities for bundle "AdminNewsBundle" 

    [Doctrine\Common\Persistence\Mapping\MappingException] 
    Invalid mapping file 'Admin.NewsBundle.Entity.News.orm.yml' for class 'Admi 
    n\NewsBundle\Entity\News'. 

doctrine:generate:entities [--path="..."] [--no-backup] name 

遺憾的英文不好,請幫我從這個問題來,因爲我是新來的Symfony2

+0

如果您使用'yml'映射,則不需要生成'Annotations'。嘗試刪除您的實體並再次運行該命令。另外,請確保您的YML文件正確縮進。你提供的是不正確的。 *(請參閱'news.orm.yml'中的manyToOne)* – Touki 2013-03-22 09:26:03

+0

是的,我的yml文件正確縮進becoz它生成dinhem我刪除了我的實體,但仍然沒有創建實體。感謝您的重播。 – 2013-03-22 09:55:53

回答

3

嘗試:

1) php app/console doctrine:mapping:convert yml ./src/Admin/NewsBundle/Resources/config/doctrine/metadata/orm --from-database --force --namespace="Admin\\NewsBundle\\Entity\\" 

爲Linux 命名空間=「管理員\\ \\ NewsBundle Entity \\「,對於Win可能是namespace =」Admin \ NewsBundle \ Entity \\「

Watch tha t映射是正確的,有正確的名稱和正確的語法。

2) php app/console doctrine:mapping:import AdminNewsBundle annotation 

3) php app/console doctrine:generate:entities AdminNewsBundle 
+0

它仍然給出相同的錯誤 – 2013-03-22 10:45:24

+0

我不知道這是如何「。」以文件名Admin.NewsBundle.Entity.News.orm.yml – 2013-03-22 10:46:18

+0

嘗試刪除此'。',然後生成。 – sean662 2013-03-22 11:34:26

0

嘗試與該實體名稱與naspace

Admin\NewsBundle\Entity\News: 

更換陽明的第一線爲例。