• 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-search-filter.php
1
<?php
2
// ---
3
// position: 7
4
// slug: doctrine-search-filter
5
// name: Doctrine ORM SearchFilter
6
// executable: true
7
// tags: doctrine
8
// ---
9

10
// API Platform provides a generic system to apply filters and sort criteria on collections. Useful filters for Doctrine ORM, MongoDB ODM and ElasticSearch are provided with the library.
11
//
12
// By default, all filters are disabled. They must be enabled explicitly.
13

14
namespace App\Entity {
15
    use ApiPlatform\Metadata\GetCollection;
16
    use ApiPlatform\Metadata\QueryParameter;
17
    use Doctrine\ORM\Mapping as ORM;
18

19
    #[GetCollection(
20
        uriTemplate: 'books{._format}',
×
21
        parameters: [
×
22
            // Declare a QueryParameter with the :property pattern that matches the properties declared on the Filter.
23
            // The filter is a service declared in the next class.
24
            ':property' => new QueryParameter(filter: 'app.search_filter'),
×
25
        ]
×
26
    )]
×
27
    #[ORM\Entity]
28
    class Book
29
    {
30
        #[ORM\Id, ORM\Column, ORM\GeneratedValue]
31
        public ?int $id = null;
32

33
        #[ORM\Column]
34
        public ?string $title = null;
35

36
        #[ORM\Column]
37
        public ?string $author = null;
38
    }
39
}
40

41
namespace App\DependencyInjection {
42
    use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
43

44
    function configure(ContainerConfigurator $configurator): void
45
    {
46
        // This is the custom search filter we declare, if you prefer to use decoration, suffix the parent service with `.instance`. They implement the `PropertyAwareFilterInterface` that allows you to override a filter's property.
47
        $services = $configurator->services();
×
48
        $services->set('app.search_filter')
×
49
                 ->parent('api_platform.doctrine.orm.search_filter')
×
50
                // Search strategies may be defined here per properties, [read more](https://api-platform.com/docs/core/filters/) on the filter documentation.
×
51
                 ->args([['author' => 'partial', 'title' => 'partial']])
×
52
                 ->tag('api_platform.filter');
×
53
    }
54
}
55

56
namespace App\Playground {
57
    use Symfony\Component\HttpFoundation\Request;
58

59
    function request(): Request
60
    {
61
        // Try changing the search value [in the interactive Playground](/playground/doctrine-search-filter).
62
        return Request::create('/books.jsonld?author=a', 'GET');
×
63
    }
64
}
65

66
namespace DoctrineMigrations {
67
    use Doctrine\DBAL\Schema\Schema;
68
    use Doctrine\Migrations\AbstractMigration;
69

70
    final class Migration extends AbstractMigration
71
    {
72
        public function up(Schema $schema): void
73
        {
74
            $this->addSql('CREATE TABLE book (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, title VARCHAR(255) NOT NULL, author VARCHAR(255) NOT NULL)');
×
75
        }
76
    }
77
}
78

79
namespace App\Fixtures {
80
    use App\Entity\Book;
81
    use Doctrine\Bundle\FixturesBundle\Fixture;
82
    use Doctrine\Persistence\ObjectManager;
83

84
    use function Zenstruck\Foundry\anonymous;
85
    use function Zenstruck\Foundry\faker;
86
    use function Zenstruck\Foundry\repository;
87

88
    final class BookFixtures extends Fixture
89
    {
90
        public function load(ObjectManager $manager): void
91
        {
92
            $bookFactory = anonymous(Book::class);
×
93
            if (repository(Book::class)->count()) {
×
94
                return;
×
95
            }
96

97
            $bookFactory->many(10)->create(fn () => [
×
98
                'title' => faker()->name(),
×
99
                'author' => faker()->firstName(),
×
100
            ]);
×
101
        }
102
    }
103
}
104

105
namespace App\Tests {
106
    use ApiPlatform\Playground\Test\TestGuideTrait;
107
    use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
108
    use App\Entity\Book;
109

110
    final class BookTest extends ApiTestCase
111
    {
112
        use TestGuideTrait;
113

114
        public function testGetDocumentation(): void
115
        {
116
            static::createClient()->request('GET', '/books.jsonld');
×
117

118
            $this->assertResponseIsSuccessful();
×
119
            $this->assertMatchesResourceCollectionJsonSchema(Book::class, '_api_books{._format}_get_collection', 'jsonld');
×
120
            $this->assertJsonContains([
×
121
                'search' => [
×
122
                    '@type' => 'IriTemplate',
×
123
                    'template' => '/books.jsonld{?title,author}',
×
124
                    'variableRepresentation' => 'BasicRepresentation',
×
125
                    'mapping' => [
×
126
                        [
×
127
                            '@type' => 'IriTemplateMapping',
×
128
                            'variable' => 'title',
×
129
                            'property' => 'title',
×
130
                            'required' => false,
×
131
                        ],
×
132
                        [
×
133
                            '@type' => 'IriTemplateMapping',
×
134
                            'variable' => 'author',
×
135
                            'property' => 'author',
×
136
                            'required' => false,
×
137
                        ],
×
138
                    ],
×
139
                ],
×
140
            ]);
×
141
        }
142
    }
143
}
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