【问题标题】:Generator.yml using DB value [Symfony]Generator.yml 使用 DB 值 [Symfony]
【发布时间】:2011-12-06 00:40:59
【问题描述】:

我是使用 symfony 的新手,我似乎找不到合适的解决方案来解决我的问题。

问题是,我有一个包含网站所有配置的后端模块(站点名称、每页项目、默认样式)。我想配置 generator.yml 文件,以便在列表字段中我可以将 max_per_page 的值设置为不是静态数字,而是从数据库中获取。

我知道我可以在该文件中使用 php 代码,但由于它是缓存的,我认为这不是最好的解决方案。我的 generator.yml 文件如下所示:

generator:
class: sfDoctrineGenerator
param:
model_class:           PodcastUsers
theme:                 admin
non_verbose_templates: true
with_show:             false
singular:              ~
plural:                ~
route_prefix:          podcast_users
with_doctrine_route:   true
actions_base_class:    sfActions

config:
  actions: ~
  fields:  ~
  list:
    max_per_page: 10;
  filter:  ~
  form:    ~
  edit:    ~
  new:     ~

有什么方法可以从某处的代码中初始化 max_per_page 值,所以如果我在数据库中更改该数字,它会立即刷新?

提前致谢!

【问题讨论】:

    标签: php symfony1 backend


    【解决方案1】:

    幸运的是,.yml 文件可以解析为 PHP,因此您可以插入 PHP 代码。不知道如何存储此 max_per_page 值,但您可以创建或编辑现有模型文件并添加检索该值的方法,然后将其包含在您的 yaml 文件中。以下仅为示范:

    class MyModelTable
    {
        public static function getMaxPerPage($myId)
        {
            // Assuming doctrine ORM
            $q = Doctrine_Query::create()
                ->from('MyTable mt')
                ->where('mt.id = ?', array($myId));
    
            $results = $q->fetchOne();
    
            return $results['max_per_page'];
        }
    }
    
    // Generator.yml
    config:
      ...
      list:
        max_per_page: <?php echo MyModelTable::getMaxPerPage($someId) . PHP_EOL ?>
      filter: ~
      ...
    

    【讨论】:

    • 感谢您的回答。我已经尝试过了,但问题是 max_per_page 的值被缓存了......所以它不会在生产服务器中立即改变。不过我找到了这个链接:finalconcept.com.au/article/view/… 它显示了如何覆盖 moduleGeneratorConfiguration.class.php 中的函数 getPagerMaxPerPage() ,这足够公平。谢谢!
    猜你喜欢
    • 2011-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多