Skip to content

Instantly share code, notes, and snippets.

@Luitame
Last active February 7, 2018 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Luitame/9ab3a9503aca01bf020dc512c0a60e49 to your computer and use it in GitHub Desktop.
Save Luitame/9ab3a9503aca01bf020dc512c0a60e49 to your computer and use it in GitHub Desktop.
Symfony 4 - repository problem
parameters:
# Adds a fallback DATABASE_URL if the env var is not set.
# This allows you to run cache:warmup even if your
# environment variables are not available yet.
# You should not need to change this value.
env(DATABASE_URL): ''
doctrine:
dbal:
# configure these for your database server
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
# With Symfony 3.3, remove the `resolve:` prefix
url: '%env(resolve:DATABASE_URL)%'
orm:
auto_generate_proxy_classes: '%kernel.debug%'
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\ProdutoRepository")
*/
class Produto
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=100)
* @Assert\NotBlank()
*/
private $nome;
/**
* @ORM\Column(type="decimal", scale=2)
* @Assert\NotBlank()
*/
private $preco;
/**
* @ORM\Column(type="text")
* @Assert\NotBlank()
*/
private $descricao;
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @return mixed
*/
public function getNome()
{
return $this->nome;
}
/**
* @param mixed $nome
*
* @return Produto
*/
public function setNome($nome)
{
$this->nome = $nome;
return $this;
}
/**
* @return mixed
*/
public function getPreco()
{
return $this->preco;
}
/**
* @param mixed $preco
*
* @return Produto
*/
public function setPreco($preco)
{
$this->preco = $preco;
return $this;
}
/**
* @return mixed
*/
public function getDescricao()
{
return $this->descricao;
}
/**
* @param mixed $descricao
*
* @return Produto
*/
public function setDescricao($descricao)
{
$this->descricao = $descricao;
return $this;
}
}
<?php
namespace App\Controller;
use App\Repository\ProdutoRepository;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class ProdutoController extends Controller
{
/**
* @Route("/produto", name="produto")
*/
public function index()
{
$em = $this
->getDoctrine()
->getManager();
$produtos = $em->getRepository(ProdutoRepository::class)->findAll();
return $this->render("produtos/index.html.twig", ['produtos' => $produtos]);
}
}
<?php
namespace App\Repository;
use App\Entity\Produto;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Symfony\Bridge\Doctrine\RegistryInterface;
class ProdutoRepository extends ServiceEntityRepository
{
public function __construct(RegistryInterface $registry)
{
parent::__construct($registry, Produto::class);
}
/*
public function findBySomething($value)
{
return $this->createQueryBuilder('p')
->where('p.something = :value')->setParameter('value', $value)
->orderBy('p.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment