0% found this document useful (0 votes)
210 views7 pages

Ourcodeworld How To Properly Count All The Rows From A Table With Doctrine in Symfony 4 PDF

The document discusses how to count the number of rows in a database table using Doctrine in Symfony 4. It explains that you first get the entity manager and repository, then use the repository to build a query and call getSingleScalarResult() to retrieve a count of the rows that match the query. In the example, it counts all rows in the Articles table by selecting the count of the id field. The response returns just the number, not all the records from the table.

Uploaded by

Ilan Tomašević
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
210 views7 pages

Ourcodeworld How To Properly Count All The Rows From A Table With Doctrine in Symfony 4 PDF

The document discusses how to count the number of rows in a database table using Doctrine in Symfony 4. It explains that you first get the entity manager and repository, then use the repository to build a query and call getSingleScalarResult() to retrieve a count of the rows that match the query. In the example, it counts all rows in the Articles table by selecting the count of the id field. The response returns just the number, not all the records from the table.

Uploaded by

Ilan Tomašević
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

HOME CONTENT INQUIRIES LANGUAGE: EN LOGIN 

Enter your search here... 

How to properly count all the rows from a table with Doctrine in Symfony 4 RELATED ARTICLES - SYMFONY
MAY 5TH 2019  14.4K  2 COMMENTS

Which PHP Framework to Choose: Laravel vs


In this short article, we will explain you how to count how many record are CodeIgniter vs Symfony
there in a table with a primary key with Doctrine in Symfony 4. JULY 7TH 2020
PHP

Count all rows from a table (repository) How to use the url_for (Url Helpers) of Symfony
1.4 in the controllers
In this example, we'll assume that you already have tables in your database JUNE 7TH 2020
SYMFONY
and you already created the models for them. Here, we will use an Articles
model available in the Entity\Articles.php le:
Porting a fully functional legacy Symfony 1.4
application to PHP 7.2
MAY 18TH 2020
SYMFONY

How to solve Symfony 5 exception: Cannot use


the "format" option of
"Symfony\Component\Form\Extension\Core\Typ
e\DateType" when the "html5" option is enabled
APRIL 13TH 2020
SYMFONY

How to order a Doctrine 2 query result by a


specific order of an array using MySQL in
Symfony 5
MARCH 4TH 2020
SYMFONY

ADVERTISE IN OUR CODE WORLD


<?php

// src/Entity/Articles.php
namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* Articles
*
* @ORM\Table(name="articles")
* @ORM\Entity(repositoryClass="App\Repository\articles
*/
class Articles
{
/**
* @var int
*
* @ORM\Column(name="id", type="bigint", nullable=f
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;

/**
* @var string
* Simplify cloud
* @ORM\Column(name="name", type="string", length=2 complexity
Dynatrace
*/
private $name; Think all-in-one. Think Dynatrace.

/**
* @var string|null
SIGN UP
*
* @ORM\Column(name="video", type="text", length=65
*/
private $video;
FOLLOW US ON YOUR FAVORITE SOCIAL
NETWORK
/** BE SURE TO JOIN OUR GIVEAWAYS TO WIN
SOMETHING AWESOME OCASIONALLY 
* @var string
*
* @ORM\Column(name="content", type="text", length=
  
*/
LIKE US FOLLOW US STAR US ON
private $content; GITHUB

  
public function getId(): ?int SUBSCRIBE SUBSCRIBE FOLLOW US

{
return $this->id;
}

public function getName(): ?string


{
return $this->name;
}

public function setName(string $name): self


{
$this->name = $name;

return $this;
}

public function getContent(): ?string


{
return $this->content;
}

public function setContent(string $content): self


{
$this->content = $content;

return $this;
}
}

The table contains a primary key with the id identi er as the primary. In our
controller, we will count how many rows are in the table with the following
query:
<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractC
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

// Include class of the entity (table) that you want to


use App\Entity\Articles;

class AdminController extends AbstractController


{
public function index()
{
// 1. Obtain doctrine manager
$em = $this->getDoctrine()->getManager();

// 2. Setup repository of some entity


$repoArticles = $em->getRepository(Articles::cl

// 3. Query how many rows are there in the Arti


$totalArticles = $repoArticles->createQueryBuil
// Filter by some parameter if you want
// ->where('a.published = 1')
->select('count(a.id)')
->getQuery()
->getSingleScalarResult();

// 4. Return a number as response


// e.g 972
return new Response($totalArticles);
}
}

Note that we are not calling absolutely all the information from the table, but
just the count instruction that will count how many elds do have the id eld.
The getSingleScalarResult function retrieves a single scalar value from the
result returned by the dbms. If the result contains more than a single scalar
value, an exception is thrown. The pure/mixed distinction does not apply.

Happy coding !

 E-mail  Tweet  Like  Share  Pin it

 WhatsApp
Carlos Delgado

 

Interested in programming since he was 14 years old, Carlos is a self-taught


programmer and founder and author of most of the articles at Our Code World.

THIS COULD INTEREST YOU

BECOME A MORE SOCIAL PERSON


ALSO ON OUR CODE WORLD

6 months ago • 1 comment 5 months ago • 4 comments 5 months ago • 1 comment

How to access the entity How to enable WebP How to order a Doctrine
manager (Doctrine) … image format preview … 2 query result by a …

What do you think about this article?


2 Responses

Upvote Funny Love Surprised Angry

Sad

Our Code World Comment Policy


Our Comments Section is open to every developer, so you can contribute (even
code) to the main idea of the Article.
Please read our Comment Policy before commenting.

2 Comments Our Code World 🔒 Disqus' Privacy Policy 


1 Login

 Recommend t Tweet f Share Sort by Best

Join the discussion…

LOG IN WITH
OR SIGN UP WITH DISQUS ?

Name

elbidani • 10 months ago


public function index()
{
$em = $this->getDoctrine()->getManager();
$repoArticles = $em->getRepository(Articles::class);

$totalArticles = $repoArticles->createQueryBuilder('a')

OUR CODE WORLD LEGAL ABOUT US

Our Code World is a free blog about programming, where you will nd Privacy Policy About
solutions to simple and complex tasks of your daily life as a developer. Comments Policy Advertise with us

Authors

Contact

Write for us
ADVERTISING
 

Advertise Here

Advertise Here

    

You might also like