Doctrine Join Mapeados | Fórum | School of Net

Deseja poder participar de nosso fórum e tirar todas as suas dúvidas?
Clique aqui e assine nosso plano de acesso ilimitado. Saiba mais.

por Elvis

2 semanas, 2 dias atrás Elvis

Doctrine Join Mapeados

Estou estou tentando fazer um relacionamento **join** com a tabela o ORM **ProductRepository** com `@Table(name="produtos")` porem os campos possuiem mapeamentos `@Column(type="integer", name="id_produto")` com `@Column(type="integer", name="fk_produto")` SQL GERADO SELECT COUNT(*) AS dctrn_count FROM (SELECT DISTINCT id_produto_0 FROM (SELECT p0_.id_produto AS id_produto_0, p0_.produto AS produto_1 FROM produtos p0_ LEFT JOIN estoques e1_ ON `p0_.product_id = e1_.id`) dctrn_result) dctrn_table deveria gerar `ON p0_.id_produto = e1_.fk_produto` ``` $qb = $this->em->createQueryBuilder(); $result = $qb->select('p') ->from(ProductRepository::class, 'p') ->leftJoin('p.stocks', 'e') ->setMaxResults(10) ->setFirstResult(0); ``` ``` <?php /** * Created by PhpStorm. * User: elvis * Date: 03/02/2019 * Time: 15:15 */ namespace App\Domain; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping\Entity; use Doctrine\ORM\Mapping\Table; use Doctrine\ORM\Mapping\Id; use Doctrine\ORM\Mapping\Column; use Doctrine\ORM\Mapping\GeneratedValue; use Doctrine\ORM\Mapping\ManyToOne; use Doctrine\ORM\Mapping\OneToOne; use Doctrine\ORM\Mapping\JoinColumn; /** * @Entity * @Table(name="produtos") */ class ProductRepository { /** * @Id * @GeneratedValue * @Column(type="integer", name="id_produto") */ private $id; /** * @Column(type="string", name="produto") */ private $name; /** * @ManyToOne(targetEntity="App\Domain\StockRepository") * @JoinColumn(name="product_id", referencedColumnName="id") */ private $stocks; public function __construct() { $this->stocks = new ArrayCollection(); $this->prices = new ArrayCollection(); } /** * @return int */ public function getId() { return $this->id; } /** * @param int $id * @return ProductRepository */ public function setId($id) { $this->id = $id; return $this; } /** * @return string */ public function getName() { return $this->name; } /** * @param string $name * @return ProductRepository */ public function setName($name) { $this->name = $name; return $this; } /** * @return Collection */ public function getStocks(): Collection { return $this->stocks; } /** * @return Collection */ public function getPrices(): Collection { return $this->prices; } } ``` ``` <?php /** * Created by PhpStorm. * User: elvis * Date: 05/02/2019 * Time: 20:14 */ namespace App\Domain; use Doctrine\ORM\Mapping\Entity; use Doctrine\ORM\Mapping\Table; use Doctrine\ORM\Mapping\Id; use Doctrine\ORM\Mapping\Column; use Doctrine\ORM\Mapping\GeneratedValue; use Doctrine\ORM\Mapping\ManyToOne; use Doctrine\ORM\Mapping\JoinColumn; /** * @Entity * @Table(name="precos") */ class PriceRepository { /** * @Column(type="integer", name="fk_produto") */ private $product_id; /** * @ManyToOne(targetEntity="ProductRepository", inversedBy="prices") * @JoinColumn(name="product_id", referencedColumnName="id") */ private $product; /** * @Id * @GeneratedValue * @Column(type="decimal", precision=10, scale=2, name="valor_compra") */ private $cost; /** * @Id * @GeneratedValue * @Column(type="decimal", precision=10, scale=2, name="valor_venda") */ private $list; /** * @Id * @GeneratedValue * @Column(type="decimal", precision=10, scale=2, name="promocao_valor") */ private $sale; /** * @return float */ public function getCost() { return $this->cost; } /** * @param float $cost * @return PriceRepository */ public function setCost($cost) { $this->cost = $cost; return $this; } /** * @return float */ public function getList() { return $this->list; } /** * @param float $list * @return PriceRepository */ public function setList($list) { $this->list = $list; return $this; } /** * @return float */ public function getSale() { return $this->sale; } /** * @param float $sale * @return PriceRepository */ public function setSale($sale) { $this->sale = $sale; return $this; } } ``` ``` <?php /** * Created by PhpStorm. * User: elvis * Date: 05/02/2019 * Time: 20:13 */ namespace App\Domain; use Doctrine\ORM\Mapping\Entity; use Doctrine\ORM\Mapping\Table; use Doctrine\ORM\Mapping\Id; use Doctrine\ORM\Mapping\Column; use Doctrine\ORM\Mapping\GeneratedValue; use Doctrine\ORM\Mapping\ManyToOne; use Doctrine\ORM\Mapping\JoinColumn; /** * @Entity * @Table(name="estoques") */ class StockRepository { /** * @Column(type="integer", name="fk_produto") */ private $product_id; /** * @Id * @GeneratedValue * @Column(type="integer", name="estoque") */ private $quantity; /** * @return integer */ public function getProductId() { return $this->product_id; } /** * @param integer $product_id * @return StockRepository */ public function setProductId($product_id) { $this->product_id = $product_id; return $this; } /** * @return integer */ public function getQuantity() { return $this->quantity; } /** * @param integer $quantity * @return StockRepository */ public function setQuantity($quantity) { $this->quantity = $quantity; return $this; } } ```

7 Respostas