Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOCSP-39180: Symfony Quick Start #971

Merged
merged 27 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions source/includes/php-frameworks/symfony/Restaurant.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace App\Document;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

#[ODM\Document(collection: 'restaurants')]
class Restaurant
{
#[ODM\Id]
public ?string $id = null;

#[ODM\Field]
public string $name;

#[ODM\Field]
public string $borough;

#[ODM\Field]
public string $cuisine;
}
47 changes: 47 additions & 0 deletions source/includes/php-frameworks/symfony/RestaurantController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace App\Controller;

use App\Document\Restaurant;
use Doctrine\ODM\MongoDB\DocumentManager;
use MongoDB\BSON\Regex;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class RestaurantController extends AbstractController
{
private DocumentManager $dm;
private LoggerInterface $logger;
jmikola marked this conversation as resolved.
Show resolved Hide resolved

public function __construct(DocumentManager $dm, LoggerInterface $logger)
{
$this->dm = $dm;
$this->logger = $logger;
}

#[Route('/', name: 'restaurant_index', methods: ['GET'])]
public function index(Request $request): Response
{
return $this->render('restaurant/index.html.twig');
}

#[Route('/restaurant/browse', name: 'restaurant_browse', methods: ['GET'])]
public function browse(Request $request): Response
{
$restaurantRepository = $this->dm->getRepository(Restaurant::class);
$queryBuilder = $restaurantRepository->createQueryBuilder();

$restaurants = $queryBuilder
->field('borough')->equals('Queens')
->field('name')->equals(new Regex('Moon', 'i'))
->getQuery()
->execute();

return $this->render('restaurant/browse.html.twig', ['restaurants' => $restaurants]);
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions source/includes/php-frameworks/symfony/browse.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{# templates/restaurant/browse.html.twig #}

{% extends 'base.html.twig' %}

{% block title %}
Search Restaurants
{% endblock %}

{% block body %}
<h1>Search Restaurants</h1>
{% for restaurant in restaurants %}
<p>
<span style="color:deeppink;"><b>Name: </b>{{ restaurant.name }}</span><br>
<b>Borough:</b> {{ restaurant.borough }}<br>
<b>Cuisine:</b> {{ restaurant.cuisine }}<br>
</p>
{% endfor %}
{% endblock %}
18 changes: 18 additions & 0 deletions source/includes/php-frameworks/symfony/doctrine_mongodb.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
doctrine_mongodb:
auto_generate_proxy_classes: true
auto_generate_hydrator_classes: true
connections:
default:
server: "%env(resolve:MONGODB_URL)%"
default_database: "%env(resolve:MONGODB_DB)%"
document_managers:
default:
auto_mapping: true
mappings:
App:
dir: "%kernel.project_dir%/src/Document"
mapping: true
type: attribute
prefix: 'App\Document'
is_bundle: false
alias: App
7 changes: 7 additions & 0 deletions source/includes/php-frameworks/symfony/index.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{# templates/restaurant/index.html.twig #}

{% extends 'base.html.twig' %}

{% block body %}
<h1>Welcome to the Symfony MongoDB Quickstart!</h1>
{% endblock %}
3 changes: 2 additions & 1 deletion source/motor.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ or set up a runnable project by following our tutorials.
.. tip::

If you do not need to access MongoDB in a non-blocking manner or from
co-routines, we recommend that you use the :doc:`PyMongo </pymongo>`
co-routines, we recommend that you use the `PyMongo
<https://www.mongodb.com/docs/languages/python/pymongo-driver/current/>`__
bisht2050 marked this conversation as resolved.
Show resolved Hide resolved
driver instead.

- `Tutorial on using Motor with Tornado <http://motor.readthedocs.org/en/stable/tutorial-tornado.html>`__
Expand Down
1 change: 1 addition & 0 deletions source/php-drivers.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ MongoDB PHP Driver
:titlesonly:

Laravel MongoDB <https://www.mongodb.com/docs/drivers/php/laravel-mongodb/current>
/php-frameworks/symfony
/php-libraries

.. contents:: On this page
Expand Down
Loading
Loading