tags:

views:

91

answers:

1

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?

A: 

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.

benlumley
Hi ben - apologies for the delay in responding. Regarding your suggestion above, there is a small problem (at least AFAI am aware). Blogs and Blogposts are different models, so belong in different modules. It would seem that in your solution, the partial template needs to be in the same module ... ? (*Sigh*)
Stick it to THE MAN
Basically, I need to find a way of passing parameters from one module to another, whilst using 'added interactions' to the list view. This is where I am coming unstuck ...
Stick it to THE MAN
the _postlist partial goes in the blog model (i should have called it commentlist). It is right to put it there, its a link to a list of comments relating to a particular blog post.
benlumley
Ben, I am finding what you have written quite confusing (I accidentally marked up your last comment - as I was still working out how to use this website). First, I cannot relate to the names you have used in the example, (e.g. 'comment_filters[post_id]' etc), maybe you can repost using something like 'model_name_filters[id_of_object]' etc. Its all getting quite confusing. Additionally, your comment: "If you've got CSRF protection enabled in the backend, you'll need to enable it, or this method won't work" is obviously a typo - I cant figure out which bit is wrong though - please clarify
Stick it to THE MAN
I have posted an abridged version of my schema here. Hopefully it will help clarify the problem I am having (maybe I am not explaining the problem clearly enough - but the schema should clarify what I'm trying to do).A blog has 0 to 1 blog rolls, 0 to N blog posts attached to it. Each blog post has 0 to M comments attached to it. Currently, I can view a list of blogs. But I want to add actions (or links) that can make me view the blogroll (which is a list of blogs attached to the blog) and the list of blogposts.When blogpost list is shown, I want link to show comments for a post. Anyone?
Stick it to THE MAN
Sorry link to schema is here: http://pastebin.com/m2d7fc594
Stick it to THE MAN
I've updated the answer, there were a few bits that weren't very clear. Have also made it match your schema. I've outlined for the BlogPost/BlogPostComments relationship. But it should apply to any others too by changing field names.
benlumley