chrisperk
chrisperk

Reputation: 47

Symfony2. Doctrine Query for check

How to check for author with Doctrine Query?

class UserController extends Controller
{     ...
  /**
   * @Route("/join/{id}", name="join_event")
   */
  public function joinAct(Request $request, $id)
  {

What Query for check? Condition: IF $id (field 'content_id') AND UserID (field 'user_id') exist in table, THEN message: 'You are an author!', ELSE do some code.

      $authorcheck = $this->getDoctrine()
              ->getRepository('MyBundle:User')
              ->find($id AND $this->getUser()->getId());

End Query

          if ($authorcheck) {
              $message = ['text' => 'You are an author!', 'type' => 'success'];
          } 
          else {
              DoSomeCode...
          }
  }
}

Any ideas?

Upvotes: 0

Views: 127

Answers (1)

DonCallisto
DonCallisto

Reputation: 29912

$authorcheck = $this
    ->getDoctrine()
    ->getRepository('MyBundle:User')
    ->findBy(array(
        'id' => $id,
        'user' => $this->getUser()
));

Should be fine for what you need.

Disclaimer

As I don't know fields name and so on, you'll probably need to arrange this in order to work

Upvotes: 2

Related Questions