You can find out how to install the part of the manual you need in the description of the repository by the link . (For example, if you want to start with this lesson without going through the previous one)
The note
If you are not familiar with ORM, we will reveal the basic principles. The definition from Wikipedia reads:
ORM (English Object-Relational Mapping, Russian object-relational mapping) is a programming technology that links databases with the concepts of object-oriented programming languages, creating a "virtual object database."
ORM facilitates the transfer of data from a relational database, such as MySQL to PHP objects, which we can manipulate. This allows us to encapsulate the necessary functionality that we need in the table through a class. Provide a user table, it probably has fields: username , password , first_name , last_name , email and job . With ORM, this becomes a class with username , password , first_name , etc. properties, which allows us to call methods such as getUsername () and setPassword () . ORM went much further, it also allows you to get related tables, while we retrieve the user object or later. Now suppose that our user has several friends associated with him. This will be the friends table, with the primary key for the user table. Now, using ORM, we can call the $ user-> getFriends () method to return objects from the friends table. ORM is also involved in saving data, so that we can create objects in PHP, call the save () method and ORM will save the data to the database. Since we use the Doctrine 2 ORM library, you will learn much more about its capabilities as you go through the tutorial.
Note
While this guide will use the Doctrine 2 ORM library, you can select the Doctrine 2 ODM library. There are several variations of this library, including implementations for MongoDB and CouchDB . See the Doctrine Projects page for more information.
You can also see this article , which explains how to install ODM with symfony 2.
app/config/parameters.yml
$ php app/console doctrine:database:create
src/Blogger/BlogBundle/Entity/Blog.php
and paste in the following: <?php // src/Blogger/BlogBundle/Entity/Blog.php namespace Blogger\BlogBundle\Entity; class Blog { protected $title; protected $author; protected $blog; protected $image; protected $tags; protected $comments; protected $created; protected $updated; }
src/Blogger/BlogBundle/Entity/Blog.php
<?php // src/Blogger/BlogBundle/Entity/Blog.php namespace Blogger\BlogBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity * @ORM\Table(name="blog") */ class Blog { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @ORM\Column(type="string") */ protected $title; /** * @ORM\Column(type="string", length=100) */ protected $author; /** * @ORM\Column(type="text") */ protected $blog; /** * @ORM\Column(type="string", length=20) */ protected $image; /** * @ORM\Column(type="text") */ protected $tags; protected $comments; /** * @ORM\Column(type="datetime") */ protected $created; /** * @ORM\Column(type="datetime") */ protected $updated; }
The note
We used only a small fraction of the 2 mapping types provided by Doctrine. A complete list of mapping types can be found on the Doctrine 2 website . Other types of comparisons will be discussed later in this guide.
// Create a blog object. $blog = new Blog(); $blog->setTitle("symblog - A Symfony2 Tutorial"); $blog->setAuthor("dsyph3r"); $blog->setBlog("symblog is a fully featured blogging website ..."); // Create a comment and add it to our blog $comment = new Comment(); $comment->setComment("Symfony2 rocks!"); $blog->addComment($comment);
class Blog { protected $comments = array(); public function addComment(Comment $comment) { $this->comments[] = $comment; } }
class Blog { protected $comments = array(); public function getComments() { return $this->comments; } }
$ php app/console doctrine:generate:entities Blogger
The note
While we use annotations in our entity, you can convert the mapping information to other supported mapping formats using the doctrine: mapping: convert command. For example, the following command converts the entity mappings above to the yml format.
$ php app/console doctrine:mapping:convert --namespace="Blogger\BlogBundle\Entity\Blog" yaml src/Blogger/BlogBundle/Resources/config/doctrine
And it will create a file insrc/Blogger/BlogBundle/Resources/config/doctrine/Blogger.BlogBundle.Entity.Blog.orm.yml
which will contain the blog entity mappings in the yml format.
$ php app/console doctrine:schema:create
The board
We used a number of Symfony2 console commands. You can get help on any command by entering the option --help. For example, to see help for the doctrine: schema: create command type:
$ php app/console doctrine:schema:create --help
Background information will display usage methods as well as available options. Most commands are executed with several options that can extend it.
src/Blogger/BlogBundle/Resources/config/routing.yml
following: # src/Blogger/BlogBundle/Resources/config/routing.yml BloggerBlogBundle_blog_show: path: /{id} defaults: { _controller: "BloggerBlogBundle:Blog:show" } requirements: methods: GET id: \d+
src/Blogger/BlogBundle/Controller/BlogController.php
and paste the following. <?php // src/Blogger/BlogBundle/Controller/BlogController.php namespace Blogger\BlogBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; /** * Blog controller. */ class BlogController extends Controller { /** * Show a blog entry */ public function showAction($id) { $em = $this->getDoctrine()->getManager(); $blog = $em->getRepository('BloggerBlogBundle:Blog')->find($id); if (!$blog) { throw $this->createNotFoundException('Unable to find Blog post.'); } return $this->render('BloggerBlogBundle:Blog:show.html.twig', array( 'blog' => $blog, )); } }
The note
The repository object provides access to a number of useful helper methods, including
// Return entities where 'author' matches 'dsyph3r' $em->getRepository('BloggerBlogBundle:Blog')->findBy(array('author' => 'dsyph3r')); // Return one entity where 'slug' matches 'symblog-tutorial' $em->getRepository('BloggerBlogBundle:Blog')->findOneBySlug('symblog-tutorial');
src/Blogger/BlogBundle/Resouces/views/Blog/show.html.twig
and insert the following. {# src/Blogger/BlogBundle/Resouces/views/Blog/show.html.twig #} {% extends 'BloggerBlogBundle::layout.html.twig' %} {% block title %}{{ blog.title }}{% endblock %} {% block body %} <article class="blog"> <header> <div class="date"><time datetime="{{ blog.created|date('c') }}">{{ blog.created|date('l, F j, Y') }}</time></div> <h2>{{ blog.title }}</h2> </header> <img src="{{ asset(['images/', blog.image]|join) }}" alt="{{ blog.title }} image not found" class="large" /> <div> <p>{{ blog.blog }}</p> </div> </article> {% endblock %}
src/Blogger/BlogBundle/Resouces/public/css/blog.css.
src/Blogger/BlogBundle/Resouces/public/css/blog.css.
.date { margin-bottom: 20px; border-bottom: 1px solid #ccc; font-size: 24px; color: #666; line-height: 30px } .blog { margin-bottom: 20px; } .blog img { width: 190px; float: left; padding: 5px; border: 1px solid #ccc; margin: 0 10px 10px 0; } .blog .meta { clear: left; margin-bottom: 20px; } .blog .snippet p.continue { margin-bottom: 0; text-align: right; } .blog .meta { font-style: italic; font-size: 12px; color: #666; } .blog .meta p { margin-bottom: 5px; line-height: 1.2em; } .blog img.large { width: 300px; min-height: 165px; }
The note
If you do not use the symbolic link method to access the bundle’s assets in the web folder, you must re-run the assets installation command.$ php app/console assets:install web
"require": { // ... "doctrine/doctrine-fixtures-bundle": "dev-master", "doctrine/data-fixtures" : "dev-master" }
$ composer update
app/AppKernel.php
// app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle(), // ... ); // ... }
src/Blogger/BlogBundle/DataFixtures/ORM/BlogFixtures.php
and paste <?php // src/Blogger/BlogBundle/DataFixtures/ORM/BlogFixtures.php namespace Blogger\BlogBundle\DataFixtures\ORM; use Doctrine\Common\DataFixtures\FixtureInterface; use Doctrine\Common\Persistence\ObjectManager; use Blogger\BlogBundle\Entity\Blog; class BlogFixtures implements FixtureInterface { public function load(ObjectManager $manager) { $blog1 = new Blog(); $blog1->setTitle('A day with Symfony2'); $blog1->setBlog('Lorem ipsum dolor sit amet, consectetur adipiscing eletra electrify denim vel ports.\nLorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi ut velocity magna. Etiam vehicula nunc non leo hendrerit commodo. Vestibulum vulputate mauris eget erat congue dapibus imperdiet justo scelerisque. Nulla consectetur tempus nisl vitae viverra. Cras el mauris eget erat congue dapibus imperdiet justo scelerisque. Nulla consectetur tempus nisl vitae viverra. Cras elementum molestie vestibulum. Morbi id quam nisl. Praesent hendrerit, orci sed elementum lobortis, justo mauris lacinia libero, non facilisis purus ipsum non mi. Aliquam sollicitudin, augue id vestibulum iaculis, sem lectus convallis nunc, vel scelerisque lorem tortor ac nunc. Donec pharetra eleifend enim vel porta.'); $blog1->setImage('beach.jpg'); $blog1->setAuthor('dsyph3r'); $blog1->setTags('symfony2, php, paradise, symblog'); $blog1->setCreated(new \DateTime()); $blog1->setUpdated($blog1->getCreated()); $manager->persist($blog1); $blog2 = new Blog(); $blog2->setTitle('The pool on the roof must have a leak'); $blog2->setBlog('Vestibulum vulputate mauris eget erat congue dapibus imperdiet justo scelerisque. Na. Cras elementum molestie vestibulum. Morbi id quam nisl. Praesent hendrerit, orci sed elementum lobortis.'); $blog2->setImage('pool_leak.jpg'); $blog2->setAuthor('Zero Cool'); $blog2->setTags('pool, leaky, hacked, movie, hacking, symblog'); $blog2->setCreated(new \DateTime("2011-07-23 06:12:33")); $blog2->setUpdated($blog2->getCreated()); $manager->persist($blog2); $blog3 = new Blog(); $blog3->setTitle('Misdirection. What the eyes see and the ears hear, the mind believes'); $blog3->setBlog('Lorem ipsumvehicula nunc non leo hendrerit commodo. Vestibulum vulputate mauris eget erat congue dapibus imperdiet justo scelerisque.'); $blog3->setImage('misdirection.jpg'); $blog3->setAuthor('Gabriel'); $blog3->setTags('misdirection, magic, movie, hacking, symblog'); $blog3->setCreated(new \DateTime("2011-07-16 16:14:06")); $blog3->setUpdated($blog3->getCreated()); $manager->persist($blog3); $blog4 = new Blog(); $blog4->setTitle('The grid - A digital frontier'); $blog4->setBlog('Lorem commodo. Vestibulum vulputate mauris eget erat congue dapibus imperdiet justo scelerisque. Nulla consectetur tempus nisl vitae viverra.'); $blog4->setImage('the_grid.jpg'); $blog4->setAuthor('Kevin Flynn'); $blog4->setTags('grid, daftpunk, movie, symblog'); $blog4->setCreated(new \DateTime("2011-06-02 18:54:12")); $blog4->setUpdated($blog4->getCreated()); $manager->persist($blog4); $blog5 = new Blog(); $blog5->setTitle('You\'re either a one or a zero. Alive or dead'); $blog5->setBlog('Lorem ipsum dolor sit amet, consectetur adipiscing elittibulum vulputate mauris eget erat congue dapibus imperdiet justo scelerisque.'); $blog5->setImage('one_or_zero.jpg'); $blog5->setAuthor('Gary Winston'); $blog5->setTags('binary, one, zero, alive, dead, !trusting, movie, symblog'); $blog5->setCreated(new \DateTime("2011-04-25 15:34:18")); $blog5->setUpdated($blog5->getCreated()); $manager->persist($blog5); $manager->flush(); } }
$blog1 = new Blog(); $blog1->setTitle('A day in paradise - A day with Symfony2'); $blog1->setBlog('Lorem ipsum dolor sit d us imperdiet justo scelerisque. Nulla consectetur...'); $blog1->setImage('beach.jpg'); $blog1->setAuthor('dsyph3r'); $blog1->setTags('symfony2, php, paradise, symblog'); $blog1->setCreated(new \DateTime()); $blog1->setUpdated($this->getCreated()); $manager->persist($blog1); // .. $manager->flush();
$ php app/console doctrine:fixtures:load
src/Blogger/BlogBundle/Entity/Blog.php
<?php // src/Blogger/BlogBundle/Entity/Blog.php // .. /** * @ORM\Entity * @ORM\Table(name="blog") * @ORM\HasLifecycleCallbacks */ class Blog { // .. }
<?php // src/Blogger/BlogBundle/Entity/Blog.php // .. /** * @ORM\Entity * @ORM\Table(name="blog") * @ORM\HasLifecycleCallbacks */ class Blog { // .. public function __construct() { $this->setCreated(new \DateTime()); $this->setUpdated(new \DateTime()); } /** * @ORM\PreUpdate */ public function setUpdatedValue() { $this->setUpdated(new \DateTime()); } // .. }
setCreated(); setUpdated();
src/Blogger/BlogBundle/DataFixtures/ORM/BlogFixtures.php
The note
timestampable , . StofDoctrineExtensionsBundle Doctrine 2 , Timestampable, Sluggable Sortable.
. .
Thank you all for the attention and comments made on the project, if you have any difficulties or questions, unsubscribe in the comments or private messages, add to friends.
Part 1 - Configuring Symfony2 and Templates
Part 2 - Contact page: validators, forms, and email
Part 4 - Comment Model, Repository and Migrations of Doctrine 2
Part 5 - Twig Extensions, Sidebar (sidebar) and Assetic
Part 6 - Modular and Functional Testing
Source: https://habr.com/ru/post/302438/
All Articles