php bin/console make:entity Vn - создаем entity

 

открываем файл  www/src/Entity/Vn.php

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\VnRepository")
 */
class Vn
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $Article;

    public function getId()
    {
        return $this->id;
    }

    public function getArticle(): ?string
    {
        return $this->Article;
    }

    public function setArticle(string $Article): self
    {
        $this->Article = $Article;

        return $this;
    }
}

 

 


next >

 

php bin/console doctrine:migrations:diff - создаем миграционый файл

php bin/console   doctrine:migrations:migrate - выполняем миграционый файл sql

 

 

 

в контролере добавляем

use App\Entity\Vn;

class LuckyController extends Controller
{

    /**
     * @Route("/")
     */
    public function main()
    {

 
        $entityManager = $this->getDoctrine()->getManager();
        $product = new Vn();
        $product->setArticle('Keyboard');
        $entityManager->persist($product);
        // actually executes the queries (i.e. the INSERT query)
        $entityManager->flush();





        return $this->render('articles/main.html.twig', array('number' => 1));
    }

 

 


Service

/src/Service/MessageGenerator.php

<?php
// src/Service/MessageGenerator.php
namespace App\Service;

class MessageGenerator
{
    public function getHappyMessage()
    {
        $messages = [
            'You did it! You updated the system! Amazing!',
            'That was one of the coolest updates I\'ve seen all day!',
            'Great work! Keep going!',
        ];

        $index = array_rand($messages);

        return $messages[$index];
    }
}

 

 

www/src/Controller/LuckyController.php

<?php
// src/Controller/LuckyController.php
namespace App\Controller;
 
use App\Service\MessageGenerator;// <!----- добавили


class LuckyController extends Controller
{

    /**
     * @Route("/")
     */
    public function main()
    {
        $m = new MessageGenerator();
        $message = $m->getHappyMessage();
        var_dump( $message );
        exit; 
    }

 

}