dragon19720808 2011-12-19 12:31
浏览 17

具有学说的Symfony2用户

I'm looking for best practices to fetch users created content. I have $user object form 'security.context' and I need to get single record created by this user by some $record_id,

so what I should do?

$this->getDoctrine()->getRepository('AcmeRecordBundle:Record')
->findOneBy(array( 'id' => $record_id, 'user' => $user->getId() ));

This doesn't look good to me, because I have lot's of information that needs to be fetch looking for user too(to don't let other users try get it by some id). And for any content( personal photo, some other private content) I have to pass 'user' => $user->getId() ?

Or it's better to create UserRepository with all these functions? getRecordById($id), getPhotoById($id), getPrivateInformationById($id), etc.

I was working with Rails a little, and there I was able to define current_user method

def current_user
  return @current_user if defined?(@current_user)
  # ....
end

and then just use it as

current_account.records.find(params[:id])

is there any possibility to make it work like this with Doctrine2 and Symfony2? Like

$user->getRecords()->find($recordId)
  • 写回答

4条回答 默认 最新

  • dspows0637 2011-12-22 10:43
    关注

    Of course you have to pass the user's id for the "WHERE" sql clause, just because ROR did it magically behind the scenes (which is a very bad practice imo), doesn't mean it didn't do it at all.

    As for the other matter, both solutions are ok:

    1. Fetch data from the particular repository, and pass the object id + the user's id, or:
    2. Create methods which internally get user's id and put them in queries

    And remember that user's id is fetched only once during the request, so don't worry about getting it from the security context too much.

    评论

报告相同问题?

悬赏问题

  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题