2015-10-13 64 views
0

有問題? 如何在Symfony CMS中爲文檔生成getter和setter? 爲什麼命名空間TaskBundle或Acme \ TaskBundle不被命令doctrine識別:實體生成?Symfony CMS,命令學說:生成:實體不起作用

我已經安裝了CMS的標準版,並設法使從官方文檔的例子: http://symfony.com/doc/master/cmf/book/database_layer.html

我需要生成文檔Task.php

爲此,我決定getter和setter方法使用命令「doctrine:generate:entities」。因此,從創建Document文件夾的新文件夾實體和複製Task.php文件: C:\ Bitnami \ wampstack-5.4.38-0 \ sym_prog \標準版的\ src \ Acme的\ TaskBundle \實體\ Task.php 我希望生成getter和setter方法,比複製回他們的文檔\ Task.php

命令主義:生成:實體是不存在的。 因此我更新comsposer.json C:\ Bitnami \ wampstack-5.4.38-0 \ sym_prog \標準版\ composer.json

"require": { 
... 
     "doctrine/orm": "^2.4.8", 
     "doctrine/doctrine-bundle": "~1.4", 

但現在我得到有關命名空間中的錯誤:

c:\Bitnami\wampstack-5.4.38-0\sym_prog\standard-edition>php app/console generate:doctrine:entities TaskBundle:Task 
    [Doctrine\ORM\ORMException] 
    Unknown Entity namespace alias 'TaskBundle'. 

c:\Bitnami\wampstack-5.4.38-0\sym_prog\standard-edition>php app/console generate:doctrine:entity 
The Entity shortcut name: Acme/TaskBundle:Task 
Bundle "Acme\TaskBundle" does not exist. 
The Entity shortcut name: TaskBundle:Task 
Bundle "TaskBundle" does not exist. 

C:\ Bitnami \ wampstack-5.4.38-0 \ sym_prog \標準版的\ src \ Acme的\ TaskBundle \實體\ Task.php

<?php 

    /* If you use annotations, you'll need 
    * to prepend all annotations with @PHPCR\, 
    * which is the name of the imported namespace 
    * (e.g. @PHPCR\Document(..)), 
    * this is not shown in Doctrine's documentation. 
    * You'll also need to include the 
    * use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCR; 
    * statement to import the PHPCR annotations prefix. */ 


    // src/Acme/TaskBundle/Entity/Task.php 
    namespace Acme\TaskBundle\Entity; 

    class Task 
    { 
     /** 
     * @PHPCR\Id() 
     */ 
     protected $id; 

     /** 
     * @PHPCR\String() 
     */ 
     protected $description; 

     /** 
     * @PHPCR\Boolean() 
     */ 
     protected $done = false; 

     /** 
     * @PHPCR\ParentDocument() 
     */ 
     protected $parentDocument; 
    } 



Questions? 
How to generate getters and setters in Symfony CMS for documents? 
Why namespace TaskBundle or Acme\TaskBundle is not recognised by the command doctrine:entities generate? 

回答

0

試試這個:

php app/console generate:doctrine:entities AcmeTaskBundle:Task 
+0

謝謝。使用你的命令我仍然收到錯誤:[RuntimeException] Bundle「AcmeTaskBundle」不包含任何映射實體。 – olga

+0

您是否在任務實體中添加了名稱空間? – Soufiene

+0

我編輯了問題並粘貼了整個Task.php文件。它具有命名空間Acme \ TaskBundle \ Entity; – olga

1

另一種方式是寫一個getter和setter方法生成自己的腳本。

文件示例(com.php)在下面。 在這種情況下,必須將字段名稱輸入到自定義數組$ avar,並將正確的路徑寫入將生成setters和getters的文件。 比起文件保存在控制檯定向的目錄(例如C:\ Bitnami \ wampstack-5.4.38-0 \的symfony \ PROJECT1 \ com.php)和運行命令行: C:\ Bitnami \ wampstack-5.4 .38-0 \ sym_prog \ standard-edition> php com.php

<?php 
//c:\Bitnami\wampstack-5.4.38-0\symfony\project1\com.php 

//write here own field names, for which you need setters and getters 
$avar=[ "description", "done", "parentDocument" ]; 
$sstr="<?php "; 

foreach($avar as $item){ 
    $uitem=ucfirst($item); 
     echo($item); 

    $sstr=$sstr." 

    /** 
    * Set $item 
    * 
    * @param string \$$item 
    * 
    * @return **ADD** 
    */ 
    public function set$uitem(\$$item) 
    { 
     \$this->$item = \$$item; 

     return \$this; 
    } 

    /** 
    * Get $item 
    * 
    * @return string 
    */ 
    public function get$uitem() 
    { 
     return \$this->$item; 
    } 
    "; 
} 

//write here the path and filenae where setters and getters will be generated 
// or you can append the file, where you need setters and getters 
    $fcom=fopen("C:\Bitnami\wampstack-5.4.38-0symfony\project1\a.php","w"); 
    fwrite($fcom,$sstr); 
    fclose($fcom); 

//copy setter and getters to file, where you need them. 
相關問題