Sortie du snippet Doctrine "Exécuter des requêtes SQL brutes avec Doctrine"

Environement :

  • Strangebuzz Snippet VM : 2.2.1
  • Symfony : 5.1.3

Sortie du code exécuté :

SELECT type_id, count(*) AS count FROM article WHERE id > ? GROUP BY type_id

array(2) {
  [0]=>
  array(2) {
    ["type_id"]=>
    string(1) "1"
    ["count"]=>
    string(2) "17"
  }
  [1]=>
  array(2) {
    ["type_id"]=>
    string(1) "2"
    ["count"]=>
    string(2) "70"
  }
}
SELECT type_id, count(*) AS count FROM article GROUP BY type_id

array(2) {
  [0]=>
  array(2) {
    ["type_id"]=>
    string(1) "1"
    ["count"]=>
    string(2) "20"
  }
  [1]=>
  array(2) {
    ["type_id"]=>
    string(1) "2"
    ["count"]=>
    string(2) "87"
  }
}

 Retour au code  ≪ this.showUnitTest ? this.trans.hide_unit_test : this.trans.show_unit_test ≫  Plus sur Stackoverflow   Lire la doc   Lire la doc  Snippet aléatoire

<?php declare(strict_types=1);

namespace App\Tests\Controller\Snippets;

use Doctrine\DBAL\ParameterType;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

/**
 * @covers Snippet70Trait
 */
final class Snippet70Test extends KernelTestCase
{
    private $connection;

    protected function setUp(): void
    {
        self::bootKernel();
        $this->connection = self::$kernel->getContainer()->get('doctrine.dbal.default_connection');
    }

    /**
     * @covers Snippet70Trait::snippet70
     */
    public function testSnippet70(): void
    {
        $statement = $this->connection->prepare('SELECT type_id, count(*) AS count FROM article WHERE id > ? GROUP BY type_id');
        $statement->bindValue(1, 20,  ParameterType::INTEGER);
        $statement->execute();
        $res = $statement->fetchAll();
        self::assertIsArray($res);
        self::assertSame(['type_id', 'count'], array_keys($res[0] ?? []));

        $res2 = $this->connection->fetchAll('SELECT type_id, count(*) AS count FROM article GROUP BY type_id');
        self::assertIsArray($res2);
        self::assertSame(['type_id', 'count'], array_keys($res2[0] ?? []));
    }
}