【问题标题】:Symfony Admin generator - show Doctrine CollectionSymfony 管理生成器 - 显示 Doctrine Collection
【发布时间】:2012-06-11 10:21:31
【问题描述】:

在我的模型中,我有:

 * @method Doctrine_Collection getComments() Returns the current record's "Comments" collection

默认如果我生成了管理员,那么这不会显示在列表中。 如果在 generator.yml 中:

config:
  actions: ~
  fields:  ~
  list:    
    display: [id, title, comments]
  filter:  ~
  form:    ~
  edit:    ~
  new:     ~

那就给我看看

<pre> Doctrine_Collection data : Array( ) </pre>

而不是 cmets 列表。

我知道 - 我可以从缓存中获取文件并显示这一点,但也许只有 generator.yml 才有可能? 例如,如果我有一对多的关系,那么这会显示我这个名字。

我不想为此使用缓存! 谢谢!

【问题讨论】:

    标签: php symfony1 symfony-1.4


    【解决方案1】:

    你可以使用函数来解决你的问题。

    例如在我的generator.yml

      list:    
        display: [id, description, public, nbQuestions]
    

    nbQuestions 是Object.class.php中的一个函数

    public function getNbQuestions() {
        return $this->getQuestion()->count();
    }
    

    管理生成器会自动调用对象类中的“getYouField”方法。所以你可以描述一个为你的学说集合返回一个长字符串的函数。

    【讨论】:

      【解决方案2】:

      除了显示计数之外,还有其他方法。

      你可以在你的 generator.yml 中添加一个部分:

        list:
          display: [id, description, public, _comments]
      

      然后在您的部分 (_comments.php) 中,您可以调用关系并显示您想要的任何内容(添加样式、其他信息等):

      <?php
        // note that you will need to change the $object
        echo $object->getComments()->count();
      ?>
      

      以另一种方式,将所有 cmets 都列在编辑视图中可能会很有用。在您的 generator.yml 中:

        form:
          # don't forget to add others fields
          display: [_comments]
      

      然后在你的部分:

      <ul>
        <?php foreach($form->getObject()->getComments() as $comment): ?>
          <li><?php echo $comment->getBody() ?></li>
        <?php endforeach; ?>
      </ul>
      

      如果你想将两者合并到同一个部分中(不要忘记重命名$object):

      <?php if(isset($form)): ?>
      
        <ul>
          <?php foreach($form->getObject()->getComments() as $comment): ?>
            <li><?php echo $comment->getBody() ?></li>
          <?php endforeach; ?>
        </ul>
      
      <?php elseif(isset($object)): ?>
      
        <?php
          // note that you will need to change the $object
          echo $object->getComments()->count();
        ?>
      
      <?php endif; ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-21
        • 2011-09-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多