• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 13724522058

07 Mar 2025 04:04PM UTC coverage: 8.175% (-0.3%) from 8.518%
13724522058

Pull #7005

github

web-flow
Merge 322407532 into 1e0bc9dc8
Pull Request #7005: fix(validation): deprecate string message for ValidationException con…

4 of 6 new or added lines in 1 file covered. (66.67%)

159 existing lines in 24 files now uncovered.

12839 of 157045 relevant lines covered (8.18%)

13.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/docs/guides/doctrine-entity-as-resource.php
1
<?php
2
// ---
3
// slug: doctrine-entity-as-resource
4
// name: Doctrine entity as API resource
5
// position: 4
6
// tags: doctrine
7
// executable: true
8
// ---
9
//
10
// API Platform is compatible with [Doctrine ORM](https://www.doctrine-project.org), all we need is to declare an
11

12
namespace App\Entity {
13
    use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
14
    use ApiPlatform\Metadata\ApiFilter;
15
    use ApiPlatform\Metadata\ApiResource;
16
    use Doctrine\ORM\Mapping as ORM;
17

18
    // When an ApiResource is declared on an `\ORM\Entity` we have access to [Doctrine filters](/docs/core/filters/).
19
    #[ApiResource]
20
    #[ApiFilter(OrderFilter::class)]
21
    #[ORM\Entity]
22
    class Book
23
    {
24
        #[ORM\Id, ORM\Column, ORM\GeneratedValue]
25
        private ?int $id = null;
26

27
        #[ORM\Column]
28
        public ?string $title = null;
29

30
        public function getId(): ?int
31
        {
32
            return $this->id;
×
33
        }
34
    }
35
}
36

37
namespace App\Playground {
38
    use Symfony\Component\HttpFoundation\Request;
39

40
    function request(): Request
41
    {
42
        // Persistence is automatic, you can try to create or read data:
43
        return Request::create('/books?order[id]=desc', 'GET');
×
44

45
        return Request::create('/books/1', 'GET');
×
46

47
        return Request::create(uri: '/books', method: 'POST', server: ['CONTENT_TYPE' => 'application/ld+json'], content: json_encode(['id' => 1, 'title' => 'API Platform rocks.']));
×
48
    }
49
}
50

51
namespace DoctrineMigrations {
52
    use Doctrine\DBAL\Schema\Schema;
53
    use Doctrine\Migrations\AbstractMigration;
54

55
    final class Migration extends AbstractMigration
56
    {
57
        public function up(Schema $schema): void
58
        {
59
            $this->addSql('CREATE TABLE book (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, title VARCHAR(255) NOT NULL)');
×
60
        }
61
    }
62
}
63

64
namespace App\Fixtures {
65
    use App\Entity\Book;
66
    use Doctrine\Bundle\FixturesBundle\Fixture;
67
    use Doctrine\Persistence\ObjectManager;
68

69
    use function Zenstruck\Foundry\anonymous;
70
    use function Zenstruck\Foundry\faker;
71
    use function Zenstruck\Foundry\repository;
72

73
    final class BookFixtures extends Fixture
74
    {
75
        public function load(ObjectManager $manager): void
76
        {
77
            $bookFactory = anonymous(Book::class);
×
78
            if (repository(Book::class)->count()) {
×
79
                return;
×
80
            }
81

82
            $bookFactory->many(10)->create(fn () => [
×
83
                'title' => faker()->name(),
×
84
            ]
×
85
            );
×
86
        }
87
    }
88
}
89

90
namespace App\Tests {
91
    use ApiPlatform\Playground\Test\TestGuideTrait;
92
    use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
93

94
    final class BookTest extends ApiTestCase
95
    {
96
        use TestGuideTrait;
97

98
        public function testGet(): void
99
        {
100
            static::createClient()->request('GET', '/books.jsonld');
×
101
            $this->assertResponseIsSuccessful();
×
102
        }
103

104
        public function testGetOne(): void
105
        {
106
            static::createClient()->request('GET', '/books/1.jsonld');
×
107
            $this->assertResponseIsSuccessful();
×
108
        }
109
    }
110
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc