2016-10-12 5 views
1

私は投票者を使って、ログインしたユーザーが特定のオブジェクトを編集できるかどうかを判断しています。基準の1つは別のオブジェクトとの比較が必要ですが、これをどのように投票者に渡すかはわかりません。私はそれがあらかじめ定義された値ではないので、コンストラクタの引数を使用することはできません。Symfony2:投票者に2番目のオブジェクトを渡す

基本的に私はこのような何かをしたいと思います:

protected function voteOnAttribute($attribute, $subject, TokenInterface $token, $comparedObject) 
      { if ($subject->getProperty1 == $comparedObject) 
    {return true;} 
    } 

任意の助けいただければ幸いです。

答えて

0

「比較対象」を配置できる「件名」の追加プロパティを作成することをお勧めします。

// Inside action. 
public function myBestAction(Request $request) 
{ 
    // My super code... e.g. we have received from ORM a $post. 

    // Create property on the fly to put $comparedObject. 
    // Perhaps creating property dynamically is not good practice, therefore you can create permanent with getter and setter. 
    $post->comparedObject = $comparedObject; 
    $this->isGranted('can_edit', $post); 
} 

// Now inside voter. 
private function canEdit($subject) 
{ 
    $comparedObject = $subject->comparedObject; 

    // Compare $subject(post) with $comparedObject and return true or false... 
} 
関連する問題