Make

Maker Command

Symfony

$ composer require symfony/maker-bundle --dev
make:command            Creates a new console command class
make:controller         Creates a new controller class
make:entity             Creates a new Doctrine entity class

[...]

make:validator          Creates a new validator and constraint class
make:voter              Creates a new security voter class
make:command            Creates a new console command class
make:controller         Creates a new controller class
make:entity             Creates a new Doctrine entity class

[...]

make:validator          Creates a new validator and constraint class
make:voter              Creates a new security voter class

 $ ./bin/console make:entity                                                                                                                           2397ms  Wed 21 Aug 2019 02:47:55 PM CDT

 Class name of the entity to create or update (e.g. DeliciousJellybean):
 > Data

 created: src/Entity/Data.php
 created: src/Repository/DataRepository.php
 
 ...

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

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

 Field length [255]:
 > 50

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

 updated: src/Entity/Data.php

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

$ ./bin/console make:entity                                                                                                                            1.7m  Wed 21 Aug 2019 02:49:42 PM CDT

 Class name of the entity to create or update (e.g. TinyPuppy):
 > Data

 Your entity already exists! So let's add some new fields!

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

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

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

...

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

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

 

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

 Do you want to add a new property to User so that you can 
 access/update Data objects from it - e.g. $user->getData()? (yes/no) [yes]:
 > 

 A new property will also be added to the User class so that you can access 
 the related Data objects from it.

 New field name inside User [data]:
 > 

 updated: src/Entity/Data.php
 updated: src/Entity/User.php
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

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

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

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="data")
     */
    private $rel;
...
<?php

namespace App\Repository;

use App\Entity\Data;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Symfony\Bridge\Doctrine\RegistryInterface;

/**
 * @method Data|null find($id, $lockMode = null, $lockVersion = null)
 * @method Data|null findOneBy(array $criteria, array $orderBy = null)
 * @method Data[]    findAll()
 * @method Data[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class DataRepository extends ServiceEntityRepository
{
    public function __construct(RegistryInterface $registry)
    {
        parent::__construct($registry, Data::class);
    }

...

}
<?php

namespace App\Controller;

use ...;

class IndexController extends AbstractController
{
    protected $dataRepo;

    protected $userRepo;

    public function __construct(DataRepository $data)
    {
        $this->dataRepo = $data;
    }

    /**
     * @Route("/", name="index")
     */
    public function index(UserRepository $userRepo)
    {
        
        $data = $this->dataRepo->findOneBy([...]);
       	
        $use user = $userRepo->find(10);
    }
    
...
}

Symfony Make Command

By David Flores

Symfony Make Command

Explicación de como aprovechar el comando Make de Symfony 4

  • 811