【问题标题】:Symfony CMS, command doctrine:generate:entities does not workSymfony CMS,命令教义:生成:实体不起作用
【发布时间】:2015-10-13 08:23:56
【问题描述】:

问题? 如何在 Symfony CMS 中为文档生成 getter 和 setter? 为什么命名空间 TaskBundle 或 Acme\TaskBundle 不被命令教义识别:实体生成?

我已经安装了标准版的CMS,并尝试从官方文档中做例子: http://symfony.com/doc/master/cmf/book/database_layer.html

我需要为 Document Task.php 生成 setter 和 getter

为此,我决定使用命令“doctrine:generate:entities”。因此创建了一个新文件夹 Entity 并从 Document 文件夹中复制了 Task.php 文件: C:\Bitnami\wampstack-5.4.38-0\sym_prog\standard-edition\src\Acme\TaskBundle\Entity\Task.php 我希望生成 setter 和 getter,然后将它们复制回 Document\Task.php

命令教义:生成:实体不存在。 因此我更新了 composer.json C:\Bitnami\wampstack-5.4.38-0\sym_prog\standard-edition\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\standard-edition\src\Acme\TaskBundle\Entity\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?

【问题讨论】:

    标签: php symfony doctrine


    【解决方案1】:

    另一种方法是编写自己的脚本来生成 setter 和 getter。

    文件示例(com.php)如下。 在这种情况下,必须将字段名称输入自定义数组 $avar 并将正确的路径写入将生成 setter 和 getter 的文件。 将文件保存到控制台指向的目录(例如 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. 
    

    【讨论】:

      【解决方案2】:

      试试这个:

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

      【讨论】:

      • 谢谢。使用您的命令我仍然收到错误:[RuntimeException] Bundle "AcmeTaskBundle" 不包含任何映射实体。
      • 您是否在任务实体中添加了命名空间?
      • 我已经编辑了问题并粘贴了整个 Task.php 文件。它有命名空间 Acme\TaskBundle\Entity;
      • 我的意思是它有命名空间 Acme\TaskBundle\Entity 。但我收到错误:[RuntimeException] Bundle "AcmeTaskBundle" 不包含任何映射实体。
      • 是的,因为您不使用学说实体,并且在您的情况下,您不能使用 generate:doctrine:entities。如果你想生成 setter 和 getter,你可以直接使用你的 IDE。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多