accessing User object in route
I have the following route:
work:
class: acWorkObjectRouteCollection
options:
prefix_path: /work
module: work
model: Work
type: object
column: workname
model_methods:
object: findBySlug
As you can see, this route pulls a Work object from the db by the findBySlug
method and the slug column is workname
. It al开发者_StackOverflow中文版l works fine, but i want it to pull the Work of the connected User, so if 2 users have the same work name, it will choose the right one.
something like: ->andWhere('w.user_id = ?', $this->getUser()->getId()
if you like..
of course, i can change the route url to :username/work/:workname
but it looks less professional and it's not really necessary, since each user will only be able to edit his own Work.
Any ideas how to get this done? maybe if i could access the User object in the route i could do it, but i couldn't find out how..
Thanks in advance!
Although it's generally bad practice, I believe this is one of the rare cases when sfContext::getInstance()
cannot be avoided. Use sfContext::getInstance()->getUser()
in your findBySlug
method.
Before using it please read Why sfContext::getInstance() Is Bad so you're aware of it's problems.
Can't you just edit proper file and append proper WHERE statement [using andWhere() of course] in code? This would be much easier.
AFAIR you can't access user data in symfony routes. Not automatically in YAML.
Edit:
I've haven't tried this, but have a look:
http://oldforum.symfony-project.org/index.php/m/85116/
If you'll come to something usable, please notify, I would like also to know [and change this answer as well]. ;]
精彩评论