Symfony Doctrine Row Count

Posted Under: Doctrine, PHP, Programming, Symfony, Tutorials on Jan 6, 2011
Symfony Doctrine Row Count
Doctrine is a very powerful ORM for PHP application, this tutorial demonstrate how you can get the number of rows returned by Doctrine. Suppose we want to check if a user is valid or not, for that we need user name and password, here we are using symfony.
public function executeValidateUser(sfWebRequest $request){

	$username = $request -> getParameter("username");
	$password = $request -> getParameter("password");

	$query = Doctrine_Query::create()

	$rowcount= $query -> count();

	if( $rowcount == 1 ){

	//do something useful, User Authenticated


	// do something useful here too even thoug User is Not Authenticated :)

here we first get the $username and $passowrd from the form submitted using symfony $request->getParamenter() method then we make a Doctrine query and then we check how many results are returned by the Doctrine Query using Doctrine count() method and lastly we check if it is exactly 1 which mean user name and password is correct and user is a valid user if not then the else part work.