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 21 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
26 changes: 26 additions & 0 deletions source/includes/php-frameworks/symfony/Restaurant.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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;
use Doctrine\ODM\MongoDB\Types\Type;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class alias is no longer used.

Suggested change
use Doctrine\ODM\MongoDB\Types\Type;


#[ODM\Document(collection: 'restaurants')]
class Restaurant
{
#[ODM\Field(type: Type::STRING)]
jmikola marked this conversation as resolved.
Show resolved Hide resolved
public ?string $id = null;

#[ODM\Field(type: Type::STRING)]
public string $name;

#[ODM\Field(type: Type::STRING)]
public string $borough;

#[ODM\Field(type: Type::STRING)]
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 $_documentManager, LoggerInterface $_logger)
{
$this->_documentManager = $_documentManager;
$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->_documentManager->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:green;"><b>Name: </b>{{ restaurant.name }}</span><br>
<b>Borough:</b> {{ restaurant.borough }}<br>
<b>Cuisine:</b> {{ restaurant.cuisine }}<br>
</p>
{% endfor %}
{% endblock %}
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 %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{% endblock %}
{% 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