Symphony:

Setup & Entities explained

Installation

  • 1. Install PHP
  • 2. Install a database server
  • 3. Install Composer
  • 4. Start installing symphony

Installation of Symphony

  • Version 3.3:
composer create-project symfony/framework-standard-edition my_project "3.3.*"
  • Version 3.4:
composer create-project symfony/framework-standard-edition my_project "3.4.*"
  • Version 4.0
  • :
composer create-project symfony/website-skeleton my-project

Setup

  • 1. Generate bundle
  • 2. Edit composer.json

Generate bundle

 

$ php bin/console generate:bundle


  Welcome to the Symfony bundle generator!


Are you planning on sharing this bundle across multiple applications? [no]:

Your application code must be written in bundles. This command helps
you generate them easily.

Give your bundle a descriptive name, like BlogBundle.
Bundle name: GarageBundle

Bundles are usually generated into the src/ directory. Unless you're
doing something custom, hit enter to keep this default!

Target Directory [src/]:

What format do you want to use for your generated configuration?

Configuration format (annotation, yml, xml, php) [annotation]:



  Bundle generation


> Generating a sample bundle skeleton into C:\Users\fernando\Documents\Webdesign\garage_3.3\app/../src/GarageBundle
  created .\app/../src/GarageBundle/
  created .\app/../src/GarageBundle/GarageBundle.php
  created .\app/../src/GarageBundle/Controller/
  created .\app/../src/GarageBundle/Controller/DefaultController.php
  created .\app/../tests/GarageBundle/Controller/
  created .\app/../tests/GarageBundle/Controller/DefaultControllerTest.php
  created .\app/../src/GarageBundle/Resources/views/Default/
  created .\app/../src/GarageBundle/Resources/views/Default/index.html.twig
  created .\app/../src/GarageBundle/Resources/config/
  created .\app/../src/GarageBundle/Resources/config/services.yml
> Checking that the bundle is autoloaded
FAILED
> Enabling the bundle inside C:\Users\fernando\Documents\Webdesign\garage_3.3\app\AppKernel.php
  updated .\app\AppKernel.php
OK
> Importing the bundle's routes from the C:\Users\fernando\Documents\Webdesign\garage_3.3\app\config\routing.yml file
  updated .\app/config/routing.yml
OK
> Importing the bundle's services.yml from the C:\Users\fernando\Documents\Webdesign\garage_3.3\app\config\config.yml file
  updated .\app/config/config.yml
OK


  The command was not able to configure everything automatically.
  You'll need to make the following changes manually.


- Edit the composer.json file and register the bundle
  namespace in the "autoload" section:

Edit composer.json

 

    "autoload": {
        "psr-4": {
            "AppBundle\\": "src/AppBundle",
            "GarageBundle\\": "src/GarageBundle"
        },
        "classmap": [ "app/AppKernel.php", "app/AppCache.php" ]
    },

Entities explained

 

Creating Entities

# Generate models
php bin/console doctrine:generate:entity GarageBundle:Customer
php bin/console doctrine:generate:entity GarageBundle:Car
php bin/console doctrine:generate:entity GarageBundle:GarageOrder
php bin/console doctrine:generate:entity GarageBundle:Product
php bin/console doctrine:generate:entity GarageBundle:Order_Product

Basic Entity

namespace GarageBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity(repositoryClass="GarageBundle\Repository\CustomerRepository")
 */
class Customer {

	/**
	 * @ORM\Id
	 * @ORM\GeneratedValue
	 * @ORM\Column(type="integer")
	 */
	private $id;

	/**
	 *
	 * @ORM\Column(type="string")
	 */
	private $name;

	/**
	 *
	 * @ORM\Column(type="string")
	 */
	private $address;
}

Foreign keys

# Entity/Customer.php


	/**
	 * @ORM\OneToMany(targetEntity="GarageBundle\Entity\Car", mappedBy="customer")
	 */
	private $cars;


# Entity/Car.php


	/**
	 * @ORM\ManyToOne(targetEntity="GarageBundle\Entity\Customer", inversedBy="cars")
	 * @ORM\JoinColumn()
	 */
	private $customer;

Advanced Foreign keys

# Entity/Order.php


	/**
	 * @ORM\OneToMany(targetEntity="GarageBundle\Entity\OrderProduct", mappedBy="order")
	 */
	private $products;


# Entity/OrderProduct.php


	/**
	 * @ORM\Id
	 * @ORM\ManyToOne(targetEntity="GarageBundle\Entity\Product", inversedBy="orders")
	 * @ORM\JoinColumn()
	 */
	private $product;

	/**
	 * @ORM\Id
	 * @ORM\ManyToOne(targetEntity="GarageBundle\Entity\Order", inversedBy="products")
	 * @ORM\JoinColumn()
	 */
	private $order;

# Entity/Product.php

	/**
	 * @ORM\OneToMany(targetEntity="GarageBundle\Entity\OrderProduct", mappedBy="product")
	 */
	private $orders;

Updating the database

$ php bin/console doctrine:schema:validate
[Mapping]  OK - The mapping files are correct.
[Database] FAIL - The database schema is not in sync with the current mapping file.


$ php bin/console doctrine:schema:update --force 
Updating database schema...
Database schema updated successfully! "9" queries were executed

Links

 

Questions?

Setting up a SYmphony project

By Fernando van Loenhout

Setting up a SYmphony project

  • 43