Tema Symfony
Tema Symfony
Tema Symfony
Crear un proyecto:
symfony new NombreProyecto --full
Arrancar symfony
cd “NombreProyecto”
symfony server:start
Crear controladores (dentro del proyecto)
php bin/console make:controller NombreControlador
Gestionar en el proyecto .env
Cambiar db_user:db_password y db_name
Creamos la base de datos desde la carpeta del proyecto:
php bin/console doctrine:database:create
Crear las entidades(tablas) para Doctrine:
Ingeniería Inversa, sacar de una base de datos todas sus entidades ( la base de
datos que te selecciona es la del archivo .env) y las introduce en tu proyecto de
NetBeans
o php bin/console doctrine:mapping:import "App\Entity" annotation
--path=src/Entity
Relacionar tablas:
/**
* @ORM\OneToMany(targetEntity="App\Entity\Animal",
mappedBy="id_cliente")
*/
private $id_animal;
/**
* @var \Cliente
*
* @ORM\ManyToOne(targetEntity="Cliente")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_Cliente",
referencedColumnName="id_cliente")
* })
*/
Private $Cliente;
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use App\Entity\Cliente;
/**
* @Route("/controlador1/{idCliente}", name="controlador1")
*/
public function index($idCliente) {
$entityManager = $this->getDoctrine()->getManager();
// Nombre de la tabla
$cliente = $entityManager->getRepository(Cliente::class)->find($idCliente);
if (!$cliente) {
throw $this->createNotFoundException(
'No product found for nombre ' . $idCliente
);
}
$cliente->setPassword("hola");
$entityManager->flush();
return new Response('Saved new product with id ');
}
}
En el buscador ponemos: http://127.0.0.1:8000/controlador1/1
Insertar datos en una tabla
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use App\Entity\Cliente;
// tell Doctrine you want to (eventually) save the Product (no queries yet)
$entityManager->persist($cliente);
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use App\Entity\Cliente;
/**
* @Route("/ControladorBorrar/{idCliente}", name="index")
*/
public function index($idCliente){
$entityManager = $this->getDoctrine()->getManager();
$cliente = $entityManager->getRepository(Cliente::class)->find($idCliente);
if (!$cliente) {
throw $this->createNotFoundException(
'No product found for id ' . $idCliente
);
}
$entityManager->remove($cliente);
$entityManager->flush();
return new Response('Remove new product with id ');
}
}
Meter en el buscador:
http://127.0.0.1:8000/ControladorBorrar/2
Doctrine, DQL
Dentro del proyecto debes crear:
composer.json que dentro lleva
{
"require": {
"doctrine/orm": "*“
},
"autoload": {
"psr-0": {
"": "src/"
}
}