pátek 7. září 2018

jednoducha CRUD aplikace v symfony 4. Entity Asociace... make:crud

Zdravím !!!!!!
Nebudu řešit základní věci, jak nainstalovat composer a etc.

Pojedme na to uděláme jsi nejdříve svoji první aplikaci a to instalací symfony/skeletonu  v projektu z nazvem CRUD.


cd ~            
composer create-project symfony/skeleton CRUD
composer req easycorp/easyadmin-bundle
composer req --dev maker-bundle migrations
composer req doctrine
composer req symfony/web-server-bundle --dev

zeditujeme si .env soubor aby jsme mohli vytvořit databázi.

mcedit ./.env


DATABASE_URL=mysql://rimmer:heslisko@127.0.0.1:3306/crud

Vytvoříme databázi pomocí doctrine.

bin/console doctrine:database:create

Dále budu pokračovat jako je vidět na výpisu. Cílem je vytvořit  a ukázat si vztah mezi Entitama, tak jak k němu přistupuje symfony 4, respektive Doctrine.
Uděláme si Větve na kterých bude ovoce.

bin/console make:entity ovoce

 created: src/Entity/Ovoce.php
 created: src/Repository/OvoceRepository.php
 
 Entity generated! Now let's add some fields!
 You can always add more fields later manually or by re-running this command.

 New property name (press <return> to stop adding fields):
 > title

 Field type (enter ? to see all types) [string]:
 > 

 Field length [255]:
 > 500

 Can this field be null in the database (nullable) (yes/no) [no]:
 > 

 updated: src/Entity/Ovoce.php

 Add another property? Enter the property name (or press <return> to stop adding fields):
 > description

 Field type (enter ? to see all types) [string]:
 > text

 Can this field be null in the database (nullable) (yes/no) [no]:
 > no

 updated: src/Entity/Ovoce.php

 Add another property? Enter the property name (or press <return> to stop adding fields):
 > 


           
  Success! 
           

 Next: When you're ready, create a migration with make:migration
 
bohous@bohous:~/sandbox (master)$ bin/console make:entity vetev

 created: src/Entity/Vetev.php
 created: src/Repository/VetevRepository.php
 
 Entity generated! Now let's add some fields!
 You can always add more fields later manually or by re-running this command.

 New property name (press <return> to stop adding fields):
 > name

 Field type (enter ? to see all types) [string]:
 > 

 Field length [255]:
 > 

 Can this field be null in the database (nullable) (yes/no) [no]:
 > 

 updated: src/Entity/Vetev.php

 Add another property? Enter the property name (or press <return> to stop adding fields):
 > ovoce

 Field type (enter ? to see all types) [string]:
 > ?



 Field type (enter ? to see all types) [string]:
 > relation

 What class should this entity be related to?:
 > Ovoce

What type of relationship is this?
 ------------ ------------------------------------------------------------- 
  Type         Description                                                  
 ------------ ------------------------------------------------------------- 
  ManyToOne    Each Vetev relates to (has) one Ovoce.                       
               Each Ovoce can relate/has to (have) many Vetev objects       
                                                                            
  OneToMany    Each Vetev relates can relate to (have) many Ovoce objects.  
               Each Ovoce relates to (has) one Vetev                        
                                                                            
  ManyToMany   Each Vetev relates can relate to (have) many Ovoce objects.  
               Each Ovoce can also relate to (have) many Vetev objects      
                                                                            
  OneToOne     Each Vetev relates to (has) exactly one Ovoce.               
               Each Ovoce also relates to (has) exactly one Vetev.          
 ------------ ------------------------------------------------------------- 

 Relation type? [ManyToOne, OneToMany, ManyToMany, OneToOne]:
 > OneToMany

 A new property will also be added to the Ovoce class so that you can access and set the related Vetev object from it.

 New field name inside Ovoce [vetev]:
 > 

 Is the Ovoce.vetev property allowed to be null (nullable)? (yes/no) [yes]:
 > 

 updated: src/Entity/Vetev.php
 updated: src/Entity/Ovoce.php

 Add another property? Enter the property name (or press <return> to stop adding fields):
 > 


           
  Success! 
           

 Next: When you're ready, create a migration with make:migration
 
bohous@bohous:~/sandbox (master)$ bin/console make:migration

           
  Success! 
           

 Next: Review the new migration "src/Migrations/Version20180907195459.php"
 Then: Run the migration with php bin/console doctrine:migrations:migrate
 See https://symfony.com/doc/current/bundles/DoctrineMigrationsBundle/index.html


bohous@bohous:~/sandbox (master)$ bin/console doctrine:schema:update --force

 Updating database schema...

     3 queries were executed

                                                                                                                        
 [OK] Database schema updated successfully!                                                                             
                                                                                                                        

                                                                                                 

bohous@bohous:~/sandbox (master)$ bin/console make:crud Vetev

 created: src/Controller/VetevController.php
 created: src/Form/VetevType.php
 created: templates/vetev/_delete_form.html.twig
 created: templates/vetev/_form.html.twig
 created: templates/vetev/edit.html.twig
 created: templates/vetev/index.html.twig
 created: templates/vetev/new.html.twig
 created: templates/vetev/show.html.twig

           
  Success! 
           

 Next: Check your new CRUD by going to /vetev/
bohous@bohous:~/sandbox (master)$ bin/console make:crud Ovoce

 created: src/Controller/OvoceController.php
 created: src/Form/OvoceType.php
 created: templates/ovoce/_delete_form.html.twig
 created: templates/ovoce/_form.html.twig
 created: templates/ovoce/edit.html.twig
 created: templates/ovoce/index.html.twig
 created: templates/ovoce/new.html.twig
 created: templates/ovoce/show.html.twig

           
  Success! 
           

 Next: Check your new CRUD by going to /ovoce/

SUPER

A máme to. A teď  můžeme spustit server a mrknout se na stránky

bin/console server:run

 Na našem webu  http://127.0.0.1:8000/vetev/ uvidíme formuláře ale i kompletně controllery, formy, twig náhledy pro view,delete,list.Ale taky Entity a Entityrepository. Inu pěkná ukážka toho co symfony umí!!!!!!
Mne nejvíce zajímalo právě, ta práce s doctrinou a asosiacemi, takže poupravíme vysledný kód v šabloně takto
{# vetev/show.html.twig #}
<table class="table">
        <tbody>

            
            <tr><th>ovoce</th>
               {% for ovocicko in vetev.getOvoce %}
                 <td> <a href="{{ path('ovoce_show', {'id': ovocicko.id}) }}">  {{ ovocicko.title }} </a>
                   {% endfor %}
                   

                </td></tr>
        </tbody>
    </table>

A je pěkně vidět jak můžeme přistupovat k větvy s ovocem, přes kolekci popsanou v Vetev.php Entitě.
Nezapomeneme přidat do Ovoce.php a Vetev.php na konec souboru metodu __toString aby se nám vše správně zobrazovalo

public function __toString()
     {
         return $this->name;
     }


To pro dnešek stačí.... pokračování příště.


Žádné komentáře:

Okomentovat