dongsheng8664 2009-12-13 17:35
浏览 32

too long

I am using Symfony 1.2.9 (with Propel ORM) to create a website. I have started using the admin generator to implement the admin functionality.

I have come accross a slight 'problem' however. My models are related (e.g. one table may have several 1:N relations and N:N relations). I have not found a way to address this satisfactorily yet. As a tactical solution (for list views), I have decided to simply show the parent object, and then add interactions to show the related objects.

I'll use a Blog model to illustrate this.

Here are the relationships for a blog model:

N:M relationship with Blogroll (models a blog roll) 1:N relationship with Blogpost (models a post submitted to a blog)

I had originally intended on displaying the (paged) blogpost list for a blog,, when it was selected, using AJAX, but I am struggling enough with the admin generator as it is, so I have shelved that idea - unless someone is kind enough to shed some light on how to do this.

Instead, what I am now doing (as a tactical/interim soln), is I have added interactions to the list view which allow a user to:

  1. View a list of the blog roll for the blog on that row

  2. View a list of the posts for the blog on that row

  3. Add a post for the blog on tha row

In all of the above, I have written actions that will basically forward the request to the approriate action (admin generated). However, I need to pass some parameters (like the blog id etc), so that the correct blog roll or blog post list etc is returned.

I am sure there is a better way of doing what I want to do, but in case there isn't here are my questions:

  1. How may I obtain the object that relates to a specific row (of the
    clicked link) in the list view (e.g. the blog object in this example)

  2. Once I have the object, I may choose to extract various fields: id etc. How can I pass these arguments to the admin generated action ?

Regarding the second question, my guess is that this may be the way to do it (I may be wrong)

public function executeMyAddedBlogRollInteractionLink(sfWebRequest $request)
{
     // get the object *somehow* (I'm guessing this may work)
     $object = $this->getRoute()->getObject();

     // retrieve the required parameters from the object, and build a query string
     $query_str=$object->getId();

     //forward the request to the generated code (action to display blogroll list in this case)
     $this->forward('backendmodulename',"getblogrolllistaction?params=$query_string");
}

This feels like a bit of a hack, but I'm not sure how else to go about it. I'm also not to keen on sending params (which may include user_id etc via a GET, even a POST is not that much safer, since it is fairly sraightforward to see what requests a browser is making). if there is a better way than what I suggest above to implement this kind of administration that is required for objects with 1 or more M:N relationships, I will be very glad to hear the "recommended" way of going about it.

I remember reading about marking certain actions as internal. i.e. callable from only within the app. I wonder if that would be useful in this instance?

  • 写回答

1条回答 默认 最新

  • dounue6984 2009-12-14 09:06
    关注

    I'm assuming your application is called backend. Suppose there are two models, BlogPost and BlogPostComment. These are managed using admin generated modules called blog_post and blog_post_comment.

    I believe you want a link against each BlogPost displayed on the list page at backend.php/blog_post. The links take you to backend.php/blog_post_comment, which should only show comments related to the relevant BlogPost.

    Under apps/backend/blog_post/templates, create a file called _commentslink.php and put this in it:

    <a href="<?php echo url_for('blog_post_comment_collection', array('action' => 'filter', 'blog_post_comment_filters[blogpost_id]'=>$blog_post->getId())) ?>">View Comments</a>
    

    Then in apps/backend/blog_post/config/generator.yml, you need to include this partial in the fields for the list view:

    ....
    param:
      config:
        list:
          display: [ id, title, _commentslink ]
    

    Note the _commentslink - the _ tells it to use a partial instead of looking for the field in the model. Your object is available in this partial as $<name of model> - $blog_post in this case.

    Essentially, all this method does is links to the same action as the filter on the comments list normally goes to, passing the relevant condition to it to make it filter by blogpost_id.

    If you've got CSRF protection enabled in the backend, you'll need to disable it, or this method won't work. This is set in apps/backend/config/settings.yml. There will be a setting in there called csrf_secret - it should be set to false to disable csrf.

    You should try symfony 1.3/1.4 out if you need support for 1:N relationships in forms. 1.3 is in my experience a relatively hassle free upgrade from 1.2.x - 1.4 is the same, but with deprecated features removed.

    评论

报告相同问题?

悬赏问题

  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单