· 6 years ago · Mar 07, 2019, 02:14 PM
1public function getBrandsWithArticles() {
2
3 $query = $this->_em->createQuery('SELECT b, a FROM EntitiesBrand b JOIN b.articles a');
4
5 return $query->getResult();
6
7}
8
9<?php
10
11namespace Entities;
12
13use DoctrineCommonCollectionsArrayCollection;
14
15/**
16 * @Entity (repositoryClass="RepositoriesArticle")
17 * @Table(name="articles")
18 * @HasLifecycleCallbacks
19 */
20class Article {
21
22 /**
23 * @Id @Column(type="integer")
24 * @GeneratedValue(strategy="AUTO")
25 */
26 private $id;
27
28 /** @Column(type="string", length=255) */
29 private $name;
30
31 /** @Column(type="string", length=255) */
32 private $thumb;
33
34 /** @Column(type="string", length=255) */
35 private $big;
36
37 /** @Column(type="text",nullable=true) */
38 private $description;
39
40 /** @Column(type="datetime") */
41 private $created;
42
43 /** @Column(type="datetime") */
44 private $updated;
45
46 /**
47 * @ManyToOne(targetEntity="Category", inversedBy="articles", cascade={"persist"})
48 * @JoinColumn(name="category_id", referencedColumnName="id")
49 */
50 private $category;
51 /**
52 * @ManyToOne(targetEntity="Brand", inversedBy="articles", cascade={"persist"})
53 * @JoinColumn(name="brand_id", referencedColumnName="id")
54 */
55 private $brand;
56
57 /**
58 * @OneToMany(targetEntity="ArticlesQuoteItems", mappedBy="articles")
59 */
60 private $items;
61
62 /** @Column(type="string", length=1) */
63 private $status;
64
65 /** @Column(type="text",nullable=true) */
66 private $tips;
67
68 /** @Column(type="text",nullable=true) */
69 private $features;
70
71 /** @Column(type="text",nullable=true) */
72 private $media;
73
74 public function __construct() {
75 $this->created = $this->updated = new DateTime("now");
76 $this->details = new ArrayCollection();
77 }
78
79 /**
80 * @PreUpdate
81 */
82 public function updated()
83 {
84 $this->updated = new DateTime("now");
85 }
86
87 public function created()
88 {
89 $this->created = new DateTime("now");
90 }
91
92 public function setUpdated()
93 {
94 $this->updated = new DateTime("now");
95 }
96 public function setCreated()
97 {
98 $this->created = new DateTime("now");
99 }
100?>
101
102CREATE TABLE IF NOT EXISTS `articles` (
103`id` int(11) NOT NULL AUTO_INCREMENT,
104`category_id` int(11) DEFAULT NULL,
105`name` varchar(255) COLLATE utf8_spanish2_ci NOT NULL,
106`description` longtext COLLATE utf8_spanish2_ci,
107`features` longtext COLLATE utf8_spanish2_ci,
108`big` varchar(255) COLLATE utf8_spanish2_ci NOT NULL,
109`thumb` varchar(255) COLLATE utf8_spanish2_ci NOT NULL,
110`tips` longtext COLLATE utf8_spanish2_ci,
111`media` longtext COLLATE utf8_spanish2_ci,
112`created` datetime NOT NULL,
113`updated` datetime NOT NULL,
114`status` varchar(1) COLLATE utf8_spanish2_ci NOT NULL,
115`brand_id` int(11) DEFAULT NULL,
116PRIMARY KEY (`id`),
117KEY `IDX_BFDD316812469DE2` (`category_id`),
118KEY `IDX_BFDD316844F5D008` (`brand_id`)
119) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_spanish2_ci AUTO_INCREMENT=24 ;
120
121use DoctrineORMMapping as ORM;
122
123class Brand {
124
125 /**
126 * @ORMId @ORMColumn(type="integer")
127 * @ORMGeneratedValue(strategy="AUTO")
128 */
129 private $id;
130
131 /**
132 * @ORMOneToMany(targetEntity="Article", mappedBy="brand")
133 */
134 private $articles;
135
136}