I have an Article and Subcategory entities, I want, using Doctrine DQL, select all the subcategories that have 1 or more articles, I don't want to select empty Subcategories .. How can I do that in one query
Here are my objects:
Article
<?php
namespace Evr\ArticleBundle\Entity;
use Evr\HomeBundle\Entity\ImageThumbnail;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
/**
* Article
*
* #ORM\Table(name="ev_article")
* #ORM\Entity
* #ORM\HasLifecycleCallbacks
*/
class Article{
/**
* #var integer
*
* #ORM\Column(name="article_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
*
* #ORM\ManyToOne(targetEntity="Evr\HomeBundle\Entity\Subcategory",inversedBy="articles")
* #ORM\JoinColumn(name="subcategory_id",referencedColumnName="subcategory_id")
*/
private $subcategory;
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* #var integer
*
* #ORM\Column(name="type", type="integer")
*/
private $type;
/**
* #var text
*
* #ORM\Column(name="content", type="text")
*/
private $content;
/**
* #var text
*
* #ORM\Column(name="exclusive_content", type="text")
*/
private $exclusive_content;
/**
* #var \DateTime
*
* #ORM\Column(name="creation_date", type="date")
*/
private $creation_date;
/**
* #var integer
*
* #ORM\Column(name="views", type="integer" , nullable=true)
*/
private $views;
/**
* #var integer
*
* #ORM\Column(name="votes", type="integer", nullable=true)
*/
private $votes;
/**
* #var string
*
* #ORM\Column(name="photo", type="string", length=255, nullable=true)
*/
protected $photo;
/**
* Image file
*
* #var File
*
* #Assert\File(
* maxSize = "5M",
* mimeTypes = {"image/jpeg", "image/gif", "image/png", "image/tiff"},
* maxSizeMessage = "The maxmimum allowed file size is 5MB.",
* mimeTypesMessage = "Only the filetypes image are allowed."
* )
*/
protected $file;
private $temp;
public function getAbsolutePath() {
return (null === $this->photo) ? null : $this->getUploadRootDir() . '/' . $this->photo;
}
public function getWebPath() {
return (null === $this->photo) ? null : $this->getUploadDir() . '/' . $this->photo;
}
protected function getUploadRootDir() {
return __DIR__ . '/../../../../web/' . $this->getUploadDir();
}
public function getUploadDir() {
return 'uploads/documents/';
}
public function getThumbPath() {
return 'uploads/documents/thumbs/';
}
/**
* Get id
*
* #return integer
*/
public function getId() {
return $this->id;
}
/**
* Set subcategory
*
* #param integer $subcategory
* #return Article
*/
public function setSubcategory($subcategory) {
$this->subcategory = $subcategory;
return $this;
}
/**
* Get subcategory
*
* #return integer
*/
public function getSubcategory() {
return $this->subcategory;
}
/**
* Set title
*
* #param string $title
* #return Article
*/
public function setTitle($title) {
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle() {
return $this->title;
}
/**
* Set type
*
* #param integer $type
* #return Article
*/
public function setType($type) {
$this->type = $type;
return $this;
}
/**
* Get type
*
* #return integer
*/
public function getType() {
return $this->type;
}
/**
* Set content
*
* #param text $content
* #return Article
*/
public function setContent($content) {
$this->content = $content;
return $this;
}
/**
* Get content
*
* #return text
*/
public function getContent() {
return $this->content;
}
/**
* Set exclusive_content
*
* #param text $exclusiveContent
* #return Article
*/
public function setExclusiveContent($exclusiveContent) {
$this->exclusive_content = $exclusiveContent;
return $this;
}
/**
* Get exclusive_content
*
* #return text
*/
public function getExclusiveContent() {
return $this->exclusive_content;
}
/**
* Set creation_date
*
* #param DateTime $creationDate
* #return Article
*/
public function setCreationDate($creation_date) {
$this->creation_date = $creation_date;
return $this;
}
/**
* Get creation_date
*
* #return DateTime
*/
public function getCreationDate() {
return $this->creation_date;
}
/**
* Set views
*
* #param integer $views
* #return Article
*/
public function setViews($views = 0) {
$this->views = $views;
return $this;
}
/**
* Get views
*
* #return integer
*/
public function getViews() {
return $this->views;
}
/**
* Set votes
*
* #param integer $votes
* #return Article
*/
public function setVotes($votes) {
$this->votes = $votes;
return $this;
}
/**
* Get votes
*
* #return integer
*/
public function getVotes() {
return $this->votes;
}
/**
* Set photo
*
* #param string $photo
* #return Article
*/
public function setPhoto($photo) {
$this->photo = $photo;
return $this;
}
/**
* Get photo
*
* #return string
*/
public function getPhoto() {
return $this->photo;
}
/**
* Set file
*
* #param UploadedFile $file
* #return Article
*/
public function setFile(UploadedFile $file = null) {
$this->file = $file;
return $this;
}
/**
* Get file
*
* #return UploadedFile
*/
public function getFile() {
return $this->file;
}
/**
* Called before saving the entity
*
* #ORM\PrePersist()
* #ORM\PreUpdate()
*/
public function preUpload() {
if (null !== $this->file) {
$filename = sha1(uniqid(mt_rand(), true));
$this->photo = $filename . '.' . $this->file->guessExtension();
}
}
/**
* Called after entity persistence
*
* #ORM\PostPersist()
* #ORM\PostUpdate()
*/
public function upload() {
if (null === $this->file) {
return;
}
$this->file->move(
$this->getUploadRootDir(), $this->photo
);
$this->file = null;
}
/**
* Called before entity removal
*
* #ORM\PostRemove()
*/
public function removeUpload() {
if ($file = $this->getAbsolutePath()) {
if (file_exists($file)) {
unlink($file);
}
}
}
public function updateUploadedFile($file) {
$this->removeUpload();
$this->file = $file;
$this->preUpload();
$this->upload();
}
}
And Subcategory
<?php
namespace Evr\HomeBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Subcategory
*
* #ORM\Table(name="ev_subcategory")
* #ORM\Entity
*/
class Subcategory
{
/**
* #var integer
*
* #ORM\Column(name="subcategory_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
*
* #ORM\ManyToOne(targetEntity="Category",inversedBy="subcategories")
* #ORM\JoinColumn(name="category_id",referencedColumnName="category_id")
*/
private $category;
/**
* #var string
*
* #ORM\Column(name="subcategory", type="string", length=255)
*/
private $subcategory;
/**
* #ORM\OneToMany(targetEntity="Evr\ArticleBundle\Entity\Article", mappedBy="subcategory")
*/
protected $articles;
/**
* #ORM\OneToMany(targetEntity="Evr\CourseBundle\Entity\Course", mappedBy="subcategory")
*/
protected $courses;
public function __construct(){
$this->articles=new ArrayCollection();
$this->courses=new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set category
*
* #param integer $category
* #return Subcategory
*/
public function setCategory($category) {
$this->category = $category;
return $this;
}
/**
* Get category
*
* #return integer
*/
public function getCategory() {
return $this->category;
}
/**
* Set subcategory
*
* #param string $subcategory
* #return Subcategory
*/
public function setSubcategory($subcategory)
{
$this->subcategory = $subcategory;
return $this;
}
/**
* Get subcategory
*
* #return string
*/
public function getSubcategory()
{
return $this->subcategory;
}
/**
* Get articles
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getArticles()
{
return $this->articles;
}
}
Related
Introduction
I am using:
XAMPP with PHP v7.1.6
Symfony v3.3.4
Doctrine v2.5.4
StofDoctrineExtensionsBundle [1] in order to manage Tree structure.
Setting up
To setup Tree structure I used documentation on Symfony.com [2] followed by documentation on GitHub [3]. Then I proceeded with tree setup - used tree entity from example [4] and used code in [5] to create a tree.
I did setup the tree structure (that represents categories) called Category. I added several custom fields to the tree: for example is_active that represents active categories.
At the moment
I am using separate queries to get all Categories and corresponding Item counts, but i would like to get this info in just one, combined, query.
Question
Is it possible to get all Categories and corresponding Item counts in one query using DQL? If so then how?
MY CODE
Getting all Categories example
public function getCategoryTreeNodeArrayByRootId($category_tree_root_id)
{
$em = $this->getEntityManager();
$query = $em
->createQueryBuilder()
->select('ct')
->from('AppBundle:Category', 'ct')
->where('ct.root = :root')
->setParameter('root', $category_tree_root_id)
->orderBy('ct.root, ct.lft', 'ASC')
->getQuery();
$query->setHint(\Doctrine\ORM\Query::HINT_INCLUDE_META_COLUMNS, true);
$build_my_tree = $query->getArrayResult();
return $build_my_tree;
}
Getting element Item count that corresponds to Category example
public function getItemCountsByCategory($in_stock = true)
{
$em = $this->getEntityManager();
if ($in_stock === true)
{
// atrod preču skaitu pa kategorijām
$query = $em->createQueryBuilder()
->select('(i.category) as category_id, COUNT(i.id) as record_count')
->from('AppBundle:Item', 'i')
->where('i.in_stock != 0')
->groupBy('i.category')
->getQuery();
}
else if ($in_stock === false)
{
// atrod preču skaitu pa kategorijām
$query = $em->createQueryBuilder()
->select('(i.category) as category_id, COUNT(i.id) as record_count')
->from('AppBundle:Item', 'i')
->where('i.in_stock == 0')
->groupBy('i.category')
->getQuery();
}
$query->setHint(\Doctrine\ORM\Query::HINT_INCLUDE_META_COLUMNS, true);
$item_counts = $query->getArrayResult();
return $item_counts;
}
My Category entity:
<?php
namespace AppBundle\Entity;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection as ArrayCollection;
/**
* #Gedmo\Tree(type="nested")
* #ORM\Table(name="category")
* use repository for handy tree functions
* #ORM\Entity(repositoryClass="AppBundle\Repository\CategoryRepository")
*/
class Category
{
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue
*/
private $id;
/**
* #ORM\Column(type="string", length=100)
*
* #var string
*/
private $category_name_lv;
/**
* #ORM\Column(type="boolean")
*/
private $is_active;
/**
* #Gedmo\TreeLeft
* #ORM\Column(type="integer")
*/
private $lft;
/**
* #Gedmo\TreeLevel
* #ORM\Column(type="integer")
*/
private $lvl;
/**
* #Gedmo\TreeRight
* #ORM\Column(type="integer")
*/
private $rgt;
/**
* #Gedmo\TreeRoot
* #ORM\ManyToOne(targetEntity="Category")
* #ORM\JoinColumn(referencedColumnName="id", onDelete="CASCADE")
*/
private $root;
/**
* #Gedmo\TreeParent
* #ORM\ManyToOne(targetEntity="Category", inversedBy="children")
* #ORM\JoinColumn(referencedColumnName="id", onDelete="CASCADE")
*/
private $parent;
/**
* #ORM\OneToMany(targetEntity="Category", mappedBy="parent")
* #ORM\OrderBy({"lft" = "ASC"})
*/
private $children;
/**
* One Category has Many Items.
* #ORM\OneToMany(targetEntity="Item", mappedBy="category")
*/
private $items;
public function __construct()
{
$this->items = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set categoryNameLv
*
* #param string $categoryNameLv
*
* #return Category
*/
public function setCategoryNameLv($categoryNameLv)
{
$this->category_name_lv = $categoryNameLv;
return $this;
}
/**
* Get categoryNameLv
*
* #return string
*/
public function getCategoryNameLv()
{
return $this->category_name_lv;
}
/**
* Set isFile
*
* #param boolean $isActive
*
* #return Category
*/
public function setIsActive($isActive)
{
$this->is_active = $isActive;
return $this;
}
/**
* Get isFile
*
* #return boolean
*/
public function getIsActive()
{
return $this->is_active;
}
/**
* Set lft
*
* #param integer $lft
*
* #return Category
*/
public function setLft($lft)
{
$this->lft = $lft;
return $this;
}
/**
* Get lft
*
* #return integer
*/
public function getLft()
{
return $this->lft;
}
/**
* Set lvl
*
* #param integer $lvl
*
* #return Category
*/
public function setLvl($lvl)
{
$this->lvl = $lvl;
return $this;
}
/**
* Get lvl
*
* #return integer
*/
public function getLvl()
{
return $this->lvl;
}
/**
* Set rgt
*
* #param integer $rgt
*
* #return Category
*/
public function setRgt($rgt)
{
$this->rgt = $rgt;
return $this;
}
/**
* Get rgt
*
* #return integer
*/
public function getRgt()
{
return $this->rgt;
}
/**
* Set root
*
* #param \AppBundle\Entity\Category $root
*
* #return Category
*/
public function setRoot(\AppBundle\Entity\Category $root = null)
{
$this->root = $root;
return $this;
}
/**
* Get root
*
* #return \AppBundle\Entity\Category
*/
public function getRoot()
{
return $this->root;
}
/**
* Set parent
*
* #param \AppBundle\Entity\Category $parent
*
* #return Category
*/
public function setParent(\AppBundle\Entity\Category $parent = null)
{
$this->parent = $parent;
return $this;
}
/**
* Get parent
*
* #return \AppBundle\Entity\Category
*/
public function getParent()
{
return $this->parent;
}
/**
* Add child
*
* #param \AppBundle\Entity\Category $child
*
* #return Category
*/
public function addChild(\AppBundle\Entity\Category $child)
{
$this->children[] = $child;
return $this;
}
/**
* Remove child
*
* #param \AppBundle\Entity\Category $child
*/
public function removeChild(\AppBundle\Entity\Category $child)
{
$this->children->removeElement($child);
}
/**
* Get children
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getChildren()
{
return $this->children;
}
/**
* Add item
*
* #param \AppBundle\Entity\Item $item
*
* #return Category
*/
public function addItem(\AppBundle\Entity\Item $item)
{
$this->items[] = $item;
return $this;
}
/**
* Remove item
*
* #param \AppBundle\Entity\Item $item
*/
public function removeItem(\AppBundle\Entity\Item $item)
{
$this->items->removeElement($item);
}
/**
* Get items
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getItems()
{
return $this->items;
}
/**
* toString
*
* #return string
*/
public function __toString()
{
return $this->getCategoryNameLv();
}
}
My Item entity:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity(repositoryClass="AppBundle\Repository\ItemRepository")
* #ORM\Table(name="item")
*/
class Item
{
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string", length=100)
*/
protected $unique_id;
/**
* #ORM\Column(type="string", length=100)
*/
protected $name_lv;
/**
* #ORM\Column(type="string", length=100)
*/
protected $name_ru;
/**
* #ORM\Column(type="string", length=200)
*/
protected $category_sub;
/**
* #ORM\Column(type="string", length=500)
*/
protected $category_full;
/**
* #ORM\Column(type="string", length=500)
*/
protected $link_lv;
/**
* #ORM\Column(type="string", length=500)
*/
protected $link_ru;
/**
* #ORM\Column(type="string", length=500)
*/
protected $local_image_small_url;
/**
* #ORM\Column(type="string", length=500)
*/
protected $local_image_big_url;
/**
* #ORM\Column(type="string", length=500)
*/
protected $local_image_big2_url;
/**
* #ORM\Column(type="string", length=500)
*/
protected $local_image_big3_url;
/**
* #ORM\Column(type="string", length=3000)
*/
protected $description_lv;
/**
* #ORM\Column(type="string", length=3000)
*/
protected $description_ru;
/**
* #ORM\Column(type="decimal", scale=2)
*/
protected $price;
/**
* #ORM\Column(type="integer")
*/
protected $in_stock;
/**
* #ORM\Column(type="boolean")
*/
protected $is_exclusive;
/**
* #ORM\Column(type="boolean")
*/
protected $is_new;
/**
* #ORM\ManyToOne(targetEntity="Day", inversedBy="items")
* #ORM\JoinColumn(name="day_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $day;
/**
* Many Items have One Category.
* #ORM\ManyToOne(targetEntity="Category", inversedBy="items")
* #ORM\JoinColumn(name="category_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $category;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set uniqueId
*
* #param string $uniqueId
*
* #return Item
*/
public function setUniqueId($uniqueId)
{
$this->unique_id = $uniqueId;
return $this;
}
/**
* Get uniqueId
*
* #return string
*/
public function getUniqueId()
{
return $this->unique_id;
}
/**
* Set nameLv
*
* #param string $nameLv
*
* #return Item
*/
public function setNameLv($nameLv)
{
$this->name_lv = $nameLv;
return $this;
}
/**
* Get nameLv
*
* #return string
*/
public function getNameLv()
{
return $this->name_lv;
}
/**
* Set nameRu
*
* #param string $nameRu
*
* #return Item
*/
public function setNameRu($nameRu)
{
$this->name_ru = $nameRu;
return $this;
}
/**
* Get nameRu
*
* #return string
*/
public function getNameRu()
{
return $this->name_ru;
}
/**
* Set categorySub
*
* #param string $categorySub
*
* #return Item
*/
public function setCategorySub($categorySub)
{
$this->category_sub = $categorySub;
return $this;
}
/**
* Get categorySub
*
* #return string
*/
public function getCategorySub()
{
return $this->category_sub;
}
/**
* Set categoryFull
*
* #param string $categoryFull
*
* #return Item
*/
public function setCategoryFull($categoryFull)
{
$this->category_full = $categoryFull;
return $this;
}
/**
* Get categoryFull
*
* #return string
*/
public function getCategoryFull()
{
return $this->category_full;
}
/**
* Set linkLv
*
* #param string $linkLv
*
* #return Item
*/
public function setLinkLv($linkLv)
{
$this->link_lv = $linkLv;
return $this;
}
/**
* Get linkLv
*
* #return string
*/
public function getLinkLv()
{
return $this->link_lv;
}
/**
* Set linkRu
*
* #param string $linkRu
*
* #return Item
*/
public function setLinkRu($linkRu)
{
$this->link_ru = $linkRu;
return $this;
}
/**
* Get linkRu
*
* #return string
*/
public function getLinkRu()
{
return $this->link_ru;
}
/**
* Set localImageSmallUrl
*
* #param string $localImageSmallUrl
*
* #return Item
*/
public function setLocalImageSmallUrl($localImageSmallUrl)
{
$this->local_image_small_url = $localImageSmallUrl;
return $this;
}
/**
* Get localImageSmallUrl
*
* #return string
*/
public function getLocalImageSmallUrl()
{
return $this->local_image_small_url;
}
/**
* Set localImageBigUrl
*
* #param string $localImageBigUrl
*
* #return Item
*/
public function setLocalImageBigUrl($localImageBigUrl)
{
$this->local_image_big_url = $localImageBigUrl;
return $this;
}
/**
* Get localImageBigUrl
*
* #return string
*/
public function getLocalImageBigUrl()
{
return $this->local_image_big_url;
}
/**
* Set localImageBig2Url
*
* #param string $localImageBig2Url
*
* #return Item
*/
public function setLocalImageBig2Url($localImageBig2Url)
{
$this->local_image_big2_url = $localImageBig2Url;
return $this;
}
/**
* Get localImageBig2Url
*
* #return string
*/
public function getLocalImageBig2Url()
{
return $this->local_image_big2_url;
}
/**
* Set localImageBig3Url
*
* #param string $localImageBig3Url
*
* #return Item
*/
public function setLocalImageBig3Url($localImageBig3Url)
{
$this->local_image_big3_url = $localImageBig3Url;
return $this;
}
/**
* Get localImageBig3Url
*
* #return string
*/
public function getLocalImageBig3Url()
{
return $this->local_image_big3_url;
}
/**
* Set descriptionLv
*
* #param string $descriptionLv
*
* #return Item
*/
public function setDescriptionLv($descriptionLv)
{
$this->description_lv = $descriptionLv;
return $this;
}
/**
* Get descriptionLv
*
* #return string
*/
public function getDescriptionLv()
{
return $this->description_lv;
}
/**
* Set descriptionRu
*
* #param string $descriptionRu
*
* #return Item
*/
public function setDescriptionRu($descriptionRu)
{
$this->description_ru = $descriptionRu;
return $this;
}
/**
* Get descriptionRu
*
* #return string
*/
public function getDescriptionRu()
{
return $this->description_ru;
}
/**
* Set price
*
* #param string $price
*
* #return Item
*/
public function setPrice($price)
{
$this->price = $price;
return $this;
}
/**
* Get price
*
* #return string
*/
public function getPrice()
{
return $this->price;
}
/**
* Set inStock
*
* #param integer $inStock
*
* #return Item
*/
public function setInStock($inStock)
{
$this->in_stock = $inStock;
return $this;
}
/**
* Get inStock
*
* #return integer
*/
public function getInStock()
{
return $this->in_stock;
}
/**
* Set isExclusive
*
* #param boolean $isExclusive
*
* #return Item
*/
public function setIsExclusive($isExclusive)
{
$this->is_exclusive = $isExclusive;
return $this;
}
/**
* Get isExclusive
*
* #return boolean
*/
public function getIsExclusive()
{
return $this->is_exclusive;
}
/**
* Set isExclusive
*
* #param boolean $isNew
*
* #return Item
*/
public function setIsNew($isNew)
{
$this->is_new = $isNew;
return $this;
}
/**
* Get isExclusive
*
* #return boolean
*/
public function getIsNew()
{
return $this->is_new;
}
/**
* Set day
*
* #param \AppBundle\Entity\Day $day
*
* #return Item
*/
public function setDay(\AppBundle\Entity\Day $day = null)
{
$this->day = $day;
return $this;
}
/**
* Get day
*
* #return \AppBundle\Entity\Day
*/
public function getDay()
{
return $this->day;
}
/**
* Set category
*
* #param \AppBundle\Entity\Category $category
*
* #return Item
*/
public function setCategory(\AppBundle\Entity\Category $category = null)
{
$this->category = $category;
return $this;
}
/**
* Get category
*
* #return \AppBundle\Entity\Category
*/
public function getCategory()
{
return $this->category;
}
}
Conclusion
Please advise.
Thank you for your time and knowledge.
I have three tables and two relations. One table is many to many and the other table is one to many relation. I created a query in mysql but I'm not converting to dql or querybuilder in Symfony.
Sample query is:
SELECT * FROM `resturant` LEFT JOIN `food` ON `resturant`.`id` = `food`.`resturant_id`
WHERE `food`.`name` LIKE "%pizza%"
GROUP BY `resturant`.`name`
food entity in my project
<?php
namespace Food\AdminBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Food
*
* #ORM\Table(name="food")
* #ORM\Entity(repositoryClass="Food\AdminBundle\Repository\FoodRepository")
*/
class Food
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, nullable=true)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="contents", type="string", length=500, nullable=true)
*/
private $contents;
/**
* #var string
*
* #ORM\Column(name="price", type="integer", nullable=true)
*/
private $price;
/**
* #var bool
*
* #ORM\Column(name="approve", type="boolean")
*/
private $approve;
/**
* #var \Food\AdminBundle\Entity\Category
*
* #ORM\ManyToOne(targetEntity="Food\AdminBundle\Entity\Category")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="category_id", referencedColumnName="id", onDelete="CASCADE" )
* })
*/
private $category;
/**
* #var \Food\AdminBundle\Entity\Resturant
*
* #ORM\ManyToOne(targetEntity="Food\AdminBundle\Entity\Resturant")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="resturant_id", referencedColumnName="id", onDelete="CASCADE" )
* })
*/
private $resturant;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
*
* #return Food
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set contents
*
* #param string $contents
*
* #return Food
*/
public function setContents($contents)
{
$this->contents = $contents;
return $this;
}
/**
* Get contents
*
* #return string
*/
public function getContents()
{
return $this->contents;
}
/**
* Set approve
*
* #param boolean $approve
*
* #return Food
*/
public function setApprove($approve)
{
$this->approve = $approve;
return $this;
}
/**
* Get approve
*
* #return bool
*/
public function getApprove()
{
return $this->approve;
}
/**
* Set category
*
* #param \Food\AdminBundle\Entity\Category $category
*
* #return Food
*/
public function setCategory(\Food\AdminBundle\Entity\Category $category = null)
{
$this->category = $category;
return $this;
}
/**
* Get category
*
* #return \Food\AdminBundle\Entity\Category
*/
public function getCategory()
{
return $this->category;
}
/**
* Set resturant
*
* #param \Food\AdminBundle\Entity\Resturant $resturant
*
* #return Food
*/
public function setResturant(\Food\AdminBundle\Entity\Resturant $resturant = null)
{
$this->resturant = $resturant;
return $this;
}
/**
* Get resturant
*
* #return \Food\AdminBundle\Entity\Resturant
*/
public function getResturant()
{
return $this->resturant;
}
/**
* Set price
*
* #param integer $price
*
* #return Food
*/
public function setPrice($price)
{
$this->price = $price;
return $this;
}
/**
* Get price
*
* #return integer
*/
public function getPrice()
{
return $this->price;
}
}
Resturant entity
<?php
namespace Food\AdminBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* Resturant
*
* #ORM\Table(name="resturant")
* #ORM\Entity(repositoryClass="Food\AdminBundle\Repository\ResturantRepository")
*/
class Resturant
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=50)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="logo", type="string", length=30, nullable=true)
*/
private $logo;
/**
* #var string
*
* #ORM\Column(name="duration", type="string", length=150, nullable=true)
*/
private $duration;
/**
* #var string
*
* #ORM\Column(name="address", type="string", length=800, nullable=true)
*/
private $address;
/**
* #var int
*
* #ORM\Column(name="minimal", type="integer")
*/
private $minimal;
/**
* #var int
*
* #ORM\Column(name="delivery", type="integer")
*/
private $delivery;
/**
* #var int
*
* #ORM\Column(name="score", type="integer", nullable=true)
*/
private $score;
/**
* #var int
*
* #ORM\Column(name="cost", type="integer")
*/
private $cost;
/**
* #var int
*
* #ORM\Column(name="startlunch", type="time", nullable=true)
*/
private $startlunch;
/**
* #var int
*
* #ORM\Column(name="endlunch", type="time", nullable=true)
*/
private $endlunch;
/**
* #var int
*
* #ORM\Column(name="startdinner", type="time", nullable=true)
*/
private $startdinner;
/**
* #var int
*
* #ORM\Column(name="enddinner", type="time", nullable=true)
*/
private $enddinner;
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="Food\AdminBundle\Entity\Zone", inversedBy="Resturant")
* #ORM\JoinTable(name="resturant_has_zone",
* joinColumns={
* #ORM\JoinColumn(name="resturant_id", referencedColumnName="id" )
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="zone_id", referencedColumnName="id" )
* }
* )
*/
private $zone;
/**
* #ORM\OneToMany(targetEntity="Food\AdminBundle\Entity\Food", mappedBy="Resturant")
*/
private $food;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
*
* #return Resturant
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set logo
*
* #param string $logo
*
* #return Resturant
*/
public function setLogo($logo)
{
$this->logo = $logo;
return $this;
}
/**
* Get logo
*
* #return string
*/
public function getLogo()
{
return $this->logo;
}
/**
* Set duration
*
* #param string $duration
*
* #return Resturant
*/
public function setDuration($duration)
{
$this->duration = $duration;
return $this;
}
/**
* Get duration
*
* #return string
*/
public function getDuration()
{
return $this->duration;
}
/**
* Set address
*
* #param string $address
*
* #return Resturant
*/
public function setAddress($address)
{
$this->address = $address;
return $this;
}
/**
* Get address
*
* #return string
*/
public function getAddress()
{
return $this->address;
}
/**
* Set minimal
*
* #param integer $minimal
*
* #return Resturant
*/
public function setMinimal($minimal)
{
$this->minimal = $minimal;
return $this;
}
/**
* Get minimal
*
* #return int
*/
public function getMinimal()
{
return $this->minimal;
}
/**
* Set delivery
*
* #param integer $delivery
*
* #return Resturant
*/
public function setDelivery($delivery)
{
$this->delivery = $delivery;
return $this;
}
/**
* Get delivery
*
* #return int
*/
public function getDelivery()
{
return $this->delivery;
}
/**
* Set score
*
* #param integer $score
*
* #return Resturant
*/
public function setScore($score)
{
$this->score = $score;
return $this;
}
/**
* Get score
*
* #return int
*/
public function getScore()
{
return $this->score;
}
/**
* Set cost
*
* #param integer $cost
*
* #return Resturant
*/
public function setCost($cost)
{
$this->cost = $cost;
return $this;
}
/**
* Get cost
*
* #return int
*/
public function getCost()
{
return $this->cost;
}
/**
* Set startlunch
*
* #param \DateTime $startlunch
*
* #return Resturant
*/
public function setStartlunch($startlunch)
{
$this->startlunch = $startlunch;
return $this;
}
/**
* Get startlunch
*
* #return \DateTime
*/
public function getStartlunch()
{
return $this->startlunch;
}
/**
* Set endlunch
*
* #param \DateTime $endlunch
*
* #return Resturant
*/
public function setEndlunch($endlunch)
{
$this->endlunch = $endlunch;
return $this;
}
/**
* Get endlunch
*
* #return \DateTime
*/
public function getEndlunch()
{
return $this->endlunch;
}
/**
* Set startdinner
*
* #param \DateTime $startdinner
*
* #return Resturant
*/
public function setStartdinner($startdinner)
{
$this->startdinner = $startdinner;
return $this;
}
/**
* Get startdinner
*
* #return \DateTime
*/
public function getStartdinner()
{
return $this->startdinner;
}
/**
* Set enddinner
*
* #param \DateTime $enddinner
*
* #return Resturant
*/
public function setEnddinner($enddinner)
{
$this->enddinner = $enddinner;
return $this;
}
/**
* Get enddinner
*
* #return \DateTime
*/
public function getEnddinner()
{
return $this->enddinner;
}
/**
* Constructor
*/
public function __construct()
{
$this->zone = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add zone
*
* #param \Food\AdminBundle\Entity\Zone $zone
*
* #return Resturant
*/
public function addZone(\Food\AdminBundle\Entity\Zone $zone)
{
$this->zone[] = $zone;
return $this;
}
/**
* Remove zone
*
* #param \Food\AdminBundle\Entity\Zone $zone
*/
public function removeZone(\Food\AdminBundle\Entity\Zone $zone)
{
$this->zone->removeElement($zone);
}
/**
* Get zone
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getZone()
{
return $this->zone;
}
/**
* Add food
*
* #param \Food\AdminBundle\Entity\Food $food
*
* #return Resturant
*/
public function addFood(\Food\AdminBundle\Entity\Food $food)
{
$this->food[] = $food;
return $this;
}
/**
* Remove food
*
* #param \Food\AdminBundle\Entity\Food $food
*/
public function removeFood(\Food\AdminBundle\Entity\Food $food)
{
$this->food->removeElement($food);
}
/**
* Get food
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getFood()
{
return $this->food;
}
/**
* #Assert\File(
* maxSize="2m",
* mimeTypes = {"image/jpeg", "image/png"},
* mimeTypesMessage = "Please upload a valid Image File"
*
* )
*/
private $file;
// Uploading File LOL
/**
* Sets file.
*
* #param UploadedFile $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
}
/**
* Get file.
*
* #return UploadedFile
*/
public function getFile()
{
return $this->file;
}
public function getAbsolutePath()
{
return null === $this->logo
? null
: $this->getUploadRootDir() . '/' . $this->logo;
}
public function getWebPath()
{
return null === $this->picture
? null
: $this->getUploadDir() . '/' . $this->logo;
}
protected function getUploadRootDir()
{
// the absolute directory path where uploaded
// documents should be saved
return __DIR__ . '/../../../../web/' . $this->getUploadDir();
}
protected function getUploadDir()
{
// get rid of the __DIR__ so it doesn't screw up
// when displaying uploaded doc/image in the view.
return 'upload/resturant/logo';
}
public function upload()
{
// the file property can be empty if the field is not required
if (null === $this->getFile()) {
return;
}
// use the original file name here but you should
// sanitize it at least to avoid any security issues
// move takes the target directory and then the
// target filename to move to
$stringArray = explode(".", $this->getFile()->getClientOriginalName());
$suffix = $stringArray[count($stringArray) - 1];
$this->logo = $this->random_string() . '.' . $suffix;
$this->getFile()->move(
$this->getUploadRootDir(),
$this->logo
);
// clean up the file property as you won't need it anymore
$this->file = null;
}
private function random_string($hashstring = null, $randLengh = null)
{
$string = $hashstring;
$randLengh = 15;
if ($string == null) {
$string = 'abcdefghijklmnopqrstuvwxyz0123456789';
}
$charactersLength = strlen($string);
$randomString = '';
for ($i = 0; $i < $randLengh; $i++) {
$randomString .= $string[rand(0, $charactersLength - 1)];
}
return $randomString;
}
public function __toString()
{
return $this->getName();
}
}
and zone entity and relation
<?php
namespace Food\AdminBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Zone
*
* #ORM\Table(name="zone")
* #ORM\Entity(repositoryClass="Food\AdminBundle\Repository\ZoneRepository")
*/
class Zone
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=100, unique=true)
*/
private $name;
/**
* #var bool
*
* #ORM\Column(name="approve", type="boolean")
*/
private $approve;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
*
* #return Zone
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set approve
*
* #param boolean $approve
*
* #return Zone
*/
public function setApprove($approve)
{
$this->approve = $approve;
return $this;
}
/**
* Get approve
*
* #return bool
*/
public function getApprove()
{
return $this->approve;
}
public function __toString()
{
return $this->getName();
}
}
Im trying to write this query in symfony2 with queryBuilder() but i get an syntax error [Syntax Error] line 0, col 58: Error: Expected end of string, got 'SELECT'
SELECT * FROM upload_video as p ORDER BY (SELECT COUNT(*) FROM vote as v WHERE v.video_id = p.id ) DESC;
So can anyone help me convert this query into Symfony2 doctrine format
$qb->createQuery("SELECT p FROM HotelPlanBundle:UploadVideo as p ORDER BY (SELECT COUNT(v) FROM HotelPlanBundle:Vote v WHERE v.video_id = p.id ) desc");
this is the UploadVideo entity
namespace HotelPlanBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\JoinColumn;
/**
* UploadVideo
*
* #ORM\Table(name="upload_video")
* #ORM\Entity(repositoryClass="HotelPlanBundle\Repository\UploadVideoRepository")
* #ORM\HasLifecycleCallbacks
*/
class UploadVideo
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
private $file;
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=255, nullable=true)
*/
private $title;
/**
* #var string
*
* #ORM\Column(name="path", type="text", nullable=true)
*/
private $path;
/**
* #var string
*
* #ORM\Column(name="video_path", type="text", nullable=true)
*/
private $videoPath;
/**
* #var \DateTime
*
* #ORM\Column(name="created_date", type="datetime")
*/
private $createdDate;
/**
* #var boolean
*
* #ORM\Column(name="is_approved", type="boolean")
*/
private $isApproved;
/**
* #var \DateTime
* #ORM\Column(name="approved_date", type="datetime", nullable=true)
*/
private $approvedDate;
/**
* #ORM\OneToMany( targetEntity="HotelPlanBundle\Entity\Vote", mappedBy="videoId", cascade={"all"}, orphanRemoval=true )
* #ORM\OrderBy({"id" = "ASC"})
*/
protected $votes;
/**
* #var integer
* #ORM\Column(name="is_winner", type="boolean")
*/
private $isWinner;
/**
* #var integer
* #ORM\Column(name="views", type="integer")
*/
private $views;
/**
* #var boolean
* #ORM\Column(name="is_deleted", type="boolean")
*/
private $isDeleted;
/**
* #ORM\ManyToOne(targetEntity="User")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $userId;
/**
* #var string
*
* #ORM\Column(name="status", type="string", length=255, nullable=true)
* #ORM\ManyToOne(targetEntity="VideoStatus")
* #ORM\JoinColumn(name="is_approved", referencedColumnName="id")
*/
private $status;
/**
* #return null|string
* #ORM\Column(name="link", type="string", length=255, unique=true)
*/
private $link;
/**
* #var
* #ORM\Column(name="thumbnail", type="string", length=100, nullable=true)
*/
private $thumbnail;
/**
* #return null|string
* #ORM\Column(name="resetlink", type="string", length=255, nullable=true)
*/
private $resetLink;
public function __construct()
{
$this->setIsWinner( FALSE );
$this->setIsDeleted( FALSE );
$this->setIsApproved( FALSE );
$this->votes = new ArrayCollection();
}
public function getAbsolutePath()
{
return NULL === $this->path
? NULL
: $this->getUploadRootDir() . '/' . $this->path;
}
public function getWebPath()
{
return NULL === $this->path
? NULL
: $this->getUploadDir() . '/' . $this->path;
}
public function getWebThumbnail()
{
return '/uploads/documents' . '/' . $this->getThumbnail();
}
protected function getUploadRootDir()
{
// the absolute directory path where uploaded
// documents should be saved
return __DIR__ . '/../../../web/' . $this->getUploadDir();
}
protected function getUploadDir()
{
// get rid of the __DIR__ so it doesn't screw up
// when displaying uploaded doc/image in the view.
return 'uploads/documents';
}
public function upload()
{
// the file property can be empty if the field is not required
if ( NULL === $this->getFile() ) {
return;
}
// use the original file name here but you should
// sanitize it at least to avoid any security issues
// move takes the target directory and then the
// target filename to move to
$this->getFile()->move(
$this->getUploadRootDir(),
$this->getFile()->getClientOriginalName()
);
// set the path property to the filename where you've saved the file
$this->path = $this->getFile()->getClientOriginalName();
// clean up the file property as you won't need it anymore
$this->file = NULL;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* #param string $title
* #return UploadVideo
*/
public function setTitle( $title )
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set file
*
* #param string $file
* #return UploadVideo
*/
public function setFile( $file )
{
$this->file = $file;
return $this;
}
/**
* Get file
*
* #return string
*/
public function getFile()
{
return $this->file;
}
/**
* Set path
*
* #param string $path
* #return UploadVideo
*/
public function setPath( $path )
{
$this->path = $path;
return $this;
}
/**
* Get path
*
* #return string
*/
public function getPath()
{
return $this->path;
}
/**
* Set videoPath
*
* #param string $videoPath
* #return UploadVideo
*/
public function setVideoPath( $videoPath )
{
$this->videoPath = $videoPath;
return $this;
}
/**
* Get videoPath
*
* #return string
*/
public function getVideoPath()
{
return $this->videoPath;
}
/**
* Set createdDate
*
* #param \DateTime $createdDate
* #return UploadVideo
*/
public function setCreatedDate( $createdDate )
{
$this->createdDate = $createdDate;
return $this;
}
/**
* Get createdDate
*
* #return \DateTime
*/
public function getCreatedDate()
{
return $this->createdDate;
}
/**
* Set isApproved
*
* #param boolean $isApproved
* #return UploadVideo
*/
public function setIsApproved( $isApproved )
{
$this->isApproved = $isApproved;
return $this;
}
/**
* Get isApproved
*
* #return boolean
*/
public function getIsApproved()
{
return $this->isApproved;
}
/**
* Set approvedDate
*
* #param \DateTime $approvedDate
* #return UploadVideo
*/
public function setApprovedDate( $approvedDate )
{
$this->approvedDate = $approvedDate;
return $this;
}
/**
* Get approvedDate
*
* #return \DateTime
*/
public function getApprovedDate()
{
return $this->approvedDate;
}
/**
* Set isWinner
*
* #param boolean $isWinner
* #return UploadVideo
*/
public function setIsWinner( $isWinner )
{
$this->isWinner = $isWinner;
return $this;
}
/**
* Get isWinner
*
* #return boolean
*/
public function getIsWinner()
{
return $this->isWinner;
}
/**
* Set isDeleted
*
* #param boolean $isDeleted
* #return UploadVideo
*/
public function setIsDeleted( $isDeleted )
{
$this->isDeleted = $isDeleted;
return $this;
}
/**
* Get isDeleted
*
* #return boolean
*/
public function getIsDeleted()
{
return $this->isDeleted;
}
/**
* Set status
*
* #param string $status
* #return UploadVideo
*/
public function setStatus( $status )
{
$this->status = $status;
return $this;
}
/**
* Get status
*
* #return string
*/
public function getStatus()
{
return $this->status;
}
/**
* Set link
*
* #param string $link
* #return UploadVideo
*/
public function setLink( $link )
{
$this->link = $link;
return $this;
}
/**
* Get link
*
* #return string
*/
public function getLink()
{
return $this->link;
}
/**
* Set thumbnail
*
* #param string $thumbnail
* #return UploadVideo
*/
public function setThumbnail( $thumbnail )
{
$this->thumbnail = $thumbnail;
return $this;
}
/**
* Get thumbnail
*
* #return string
*/
public function getThumbnail()
{
return $this->thumbnail;
}
/**
* Set resetLink
*
* #param string $resetLink
* #return UploadVideo
*/
public function setResetLink( $resetLink )
{
$this->resetLink = $resetLink;
return $this;
}
/**
* Get resetLink
*
* #return string
*/
public function getResetLink()
{
return $this->resetLink;
}
/**
* Add votes
*
* #param \HotelPlanBundle\Entity\Vote $votes
* #return UploadVideo
*/
public function addVote( \HotelPlanBundle\Entity\Vote $votes )
{
$this->votes[] = $votes;
return $this;
}
/**
* Remove votes
*
* #param \HotelPlanBundle\Entity\Vote $votes
*/
public function removeVote( \HotelPlanBundle\Entity\Vote $votes )
{
$this->votes->removeElement( $votes );
}
/**
* Get votes
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getVotes()
{
return $this->votes;
}
/**
* Set userId
*
* #param \HotelPlanBundle\Entity\User $userId
* #return UploadVideo
*/
public function setUserId( \HotelPlanBundle\Entity\User $userId = NULL )
{
$this->userId = $userId;
return $this;
}
/**
* Get userId
*
* #return \HotelPlanBundle\Entity\User
*/
public function getUserId()
{
return $this->userId;
}
/**
* Set views
*
* #param integer $views
* #return UploadVideo
*/
public function setViews($views)
{
$this->views = $views;
return $this;
}
/**
* Get views
*
* #return integer
*/
public function getViews()
{
return $this->views;
}
}
`This is the Vote entity
namespace HotelPlanBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Vote
*
* #ORM\Table(name="vote")
* #ORM\Entity(repositoryClass="HotelPlanBundle\Repository\VoteRepository")
*/
class Vote
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="User", inversedBy="Likes")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $userId;
/**
* #var int
*
* #ORM\ManyToOne(targetEntity="HotelPlanBundle\Entity\UploadVideo", inversedBy="votes", cascade={"persist"})
* #ORM\JoinColumn(name="video_id", referencedColumnName="id")
*
*/
private $videoId;
/**
* #var \DateTime
*
* #ORM\Column(name="created_at", type="datetime")
*/
private $createdAt;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set createdAt
*
* #param \DateTime $createdAt
* #return Vote
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* #return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set userId
*
* #param \HotelPlanBundle\Entity\User $userId
* #return Vote
*/
public function setUserId(\HotelPlanBundle\Entity\User $userId = null)
{
$this->userId = $userId;
return $this;
}
/**
* Get userId
*
* #return \HotelPlanBundle\Entity\User
*/
public function getUserId()
{
return $this->userId;
}
/**
* Set videoId
*
* #param \HotelPlanBundle\Entity\UploadVideo $videoId
* #return Vote
*/
public function setVideoId(\HotelPlanBundle\Entity\UploadVideo $videoId = null)
{
$this->videoId = $videoId;
return $this;
}
/**
* Get videoId
*
* #return \HotelPlanBundle\Entity\UploadVideo
*/
public function getVideoId()
{
return $this->videoId;
}
}
What i'm trying to do is i'm trying to order the videos based on the votes that are in the vote table or in the collection.
You can use QueryBuilder to achieve that:
$qb = $em->createQueryBuilder();
$result = $qb->select('uv, COUNT(v) AS HIDDEN votesCount')
->from('HotelPlanBundle\Entity\UploadVideo', 'uv')
->leftJoin('uv.votes', 'v')
->where('v.videoId = uv.id')
->orderBy('votesCount', 'DESC')
->groupBy('uv')
->getQuery()
->getResult();
or if you want to use DQL:
$em->createQuery('SELECT uv, COUNT(v) as AS HIDDEN votesCount FROM HotelPlanBundle\Entity\UploadVideo uv LEFT JOIN uv.votes v WHERE v.videoId = uv.id GROUP BY uv ORDER BY votesCount DESC');
In editAction method of controller I tried to edit existed entities in class table inheritance.
But after form submit I get error:
Neither the property "id" nor one of the methods
"addId()"/"removeId()", "setId()", "id()", "__set()" or "__call()"
exist and have public access in class "AdBundle\Entity\AdFlat".
Why AdFlat entity not extended of AdBase entity?
Base entity:
<?php
namespace AdBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* AdBase
*
* #ORM\Table(name="ad_base")
* #ORM\Entity(repositoryClass="AdBundle\Repository\AdBaseRepository")
* #ORM\HasLifecycleCallbacks()
* #ORM\InheritanceType("JOINED")
* #ORM\DiscriminatorColumn(name="discr", type="string")
* #ORM\DiscriminatorMap({
"flat" = "AdFlat"
* })
*/
abstract class AdBase
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string
*
* #ORM\Column(name="author", type="string", length=255)
*/
private $author;
/**
* #ORM\ManyToOne(targetEntity="AdBundle\Entity\AdCategory")
*/
private $category;
/**
* #ORM\Column(type="string")
* #Assert\NotBlank(message="Field Location should not be blank")
*/
private $location;
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=255)
* #Assert\NotBlank(message="Not specified Title")
*/
private $title;
/**
* #var string
*
* #ORM\Column(name="description", type="text", nullable=true)
* #Assert\NotBlank(message="Not specified Description")
*/
private $description;
/**
* #ORM\OneToMany(targetEntity="AdBundle\Entity\AdPhoto", mappedBy="ad")
*/
private $photos;
/**
* #ORM\Column(type="float")
* #Assert\NotBlank()
*/
private $price;
/**
* #ORM\ManyToOne(targetEntity="AdBundle\Entity\AdPriceType")
*/
private $priceType;
/**
* #var \DateTime
*
* #ORM\Column(name="createdAt", type="datetime")
*/
private $createdAt;
/**
* #var string
*
* #ORM\Column(name="updatedAt", type="datetime")
*/
private $updatedAt;
/**
* #var bool
*
* #ORM\Column(name="visible", type="boolean")
*/
private $visible = false;
/**
* #ORM\Column(type="boolean")
*/
private $active = true;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set author
*
* #param string $author
*
* #return AdBase
*/
public function setAuthor($author)
{
$this->author = $author;
return $this;
}
/**
* Get author
*
* #return string
*/
public function getAuthor()
{
return $this->author;
}
/**
* #return mixed
*/
public function getCategory()
{
return $this->category;
}
/**
* #param mixed $category
*/
public function setCategory($category)
{
$this->category = $category;
}
/**
* #return mixed
*/
public function getLocation()
{
return $this->location;
}
/**
* #param mixed $location
*/
public function setLocation($location)
{
$this->location = $location;
}
/**
* Set title
*
* #param string $title
*
* #return AdBase
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set description
*
* #param string $description
*
* #return AdBase
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set photos
*
* #param string $photos
*
* #return AdBase
*/
public function setPhotos($photos)
{
$this->photos = $photos;
return $this;
}
/**
* Get photos
*
* #return string
*/
public function getPhotos()
{
return $this->photos;
}
/**
* #return mixed
*/
public function getPrice()
{
return $this->price;
}
/**
* #param mixed $price
*/
public function setPrice($price)
{
$this->price = $price;
}
/**
* #return mixed
*/
public function getPriceType()
{
return $this->priceType;
}
/**
* #param mixed $priceType
*/
public function setPriceType($priceType)
{
$this->priceType = $priceType;
}
/**
* Set createdAt
*
* #param \DateTime $createdAt
*
* #return AdBase
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* #return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set updatedAt
*
* #param string $updatedAt
*
* #return AdBase
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* #return string
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* Set visible
*
* #param boolean $visible
*
* #return AdBase
*/
public function setVisible($visible)
{
$this->visible = $visible;
return $this;
}
/**
* Get visible
*
* #return bool
*/
public function getVisible()
{
return $this->visible;
}
/**
* #return mixed
*/
public function getActive()
{
return $this->active;
}
/**
* #param mixed $active
*/
public function setActive($active)
{
$this->active = $active;
}
/**
* #ORM\PrePersist()
*/
public function prePersist()
{
$this->author = 'voodoo';
$this->createdAt = new \DateTime('now', new \DateTimeZone('UTC'));
$this->updatedAt = new \DateTime('now', new \DateTimeZone('UTC'));
$this->location = 'Donetsk';
}
/**
* #ORM\PreUpdate()
*/
public function preUpdate()
{
$this->updatedAt = new \DateTime('now', new \DateTimeZone('UTC'));
}
}
Extended entity:
<?php
namespace AdBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* AdFlat
*
* #ORM\Table(name="ad_flat")
* #ORM\Entity(repositoryClass="AdBundle\Repository\AdFlatRepository")
*/
class AdFlat extends AdBase
{
/**
* #var integer
*
* #ORM\Column(type="integer")
* #Assert\NotBlank(message="ad_flat.rooms.not_blank")
*/
private $rooms;
/**
* #var float
*
* #ORM\Column(name="square", type="float", nullable=true)
*/
private $square;
/**
* #var float
*
* #ORM\Column(name="liveSquare", type="float", nullable=true)
*/
private $liveSquare;
/**
* #var float
*
* #ORM\Column(type="float", nullable=true)
*/
private $kitchenSquare;
/**
* #var int
*
* #ORM\Column(name="floor", type="integer")
*/
private $floor;
/**
* #var int
*
* #ORM\Column(name="floors", type="integer")
*/
private $floors;
/**
* #var string
*
* #ORM\ManyToOne(targetEntity="AdBundle\Entity\AdWallType")
*/
private $wallType;
/**
* #ORM\ManyToOne(targetEntity="AdBundle\Entity\AdWCType")
*/
private $wcType;
/**
* #return mixed
*/
public function getRooms()
{
return $this->rooms;
}
/**
* #param mixed $rooms
*/
public function setRooms($rooms)
{
$this->rooms = $rooms;
}
/**
* Set square
*
* #param float $square
*
* #return AdFlat
*/
public function setSquare($square)
{
$this->square = $square;
return $this;
}
/**
* Get square
*
* #return float
*/
public function getSquare()
{
return $this->square;
}
/**
* Set liveSquare
*
* #param float $liveSquare
*
* #return AdFlat
*/
public function setLiveSquare($liveSquare)
{
$this->liveSquare = $liveSquare;
return $this;
}
/**
* Get liveSquare
*
* #return float
*/
public function getLiveSquare()
{
return $this->liveSquare;
}
/**
* #return float
*/
public function getKitchenSquare()
{
return $this->kitchenSquare;
}
/**
* #param float $kitchenSquare
*/
public function setKitchenSquare($kitchenSquare)
{
$this->kitchenSquare = $kitchenSquare;
}
/**
* Set floor
*
* #param integer $floor
*
* #return AdFlat
*/
public function setFloor($floor)
{
$this->floor = $floor;
return $this;
}
/**
* Get floor
*
* #return int
*/
public function getFloor()
{
return $this->floor;
}
/**
* Set floors
*
* #param integer $floors
*
* #return AdFlat
*/
public function setFloors($floors)
{
$this->floors = $floors;
return $this;
}
/**
* Get floors
*
* #return int
*/
public function getFloors()
{
return $this->floors;
}
/**
* Set wallType
*
* #param string $wallType
*
* #return AdFlat
*/
public function setWallType($wallType)
{
$this->wallType = $wallType;
return $this;
}
/**
* Get wallType
*
* #return string
*/
public function getWallType()
{
return $this->wallType;
}
/**
* Set wcType
*
* #param string $wcType
*
* #return AdFlat
*/
public function setWcType($wcType)
{
$this->wcType = $wcType;
return $this;
}
/**
* Get wcType
*
* #return string
*/
public function getWcType()
{
return $this->wcType;
}
}
In controller:
public function editAction(Request $request)
{
$adId = $request->get('id');
if (!$adId) {
return $this->renderError('Unknown Ad');
}
$adEntity = $this->getDoctrine()->getRepository('AdBundle:AdBase')->find($adId);
if (null === $adEntity) {
return $this->renderError('Unknown Ad');
}
$reflection = new \ReflectionClass($adEntity);
$adForm = $this->getForm($reflection->getShortName());
$form = $this->createForm($adForm, $adEntity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($adEntity);
$em->flush();
return $this->redirectToRoute('ad_bundle_ad_view', array(
'id' => $adEntity->getId()
));
}
return $this->render('AdBundle:Ad:edit-ad-flat.html.twig', array(
'form' => $form->createView()
));
}
private function renderError($message = '')
{
return $this->render('::error.html.twig', array(
'error' => $message
));
}
private function getEntity($className)
{
$entityName = 'AdBundle\Entity\\' . $className;
return new $entityName();
}
private function getForm($className)
{
$formName = 'AdBundle\Form\Type\\' . $className . 'Type';
return new $formName();
}
The error message simply states that you are missing a way to set the entity's $id property. It doesn't tell you anything about the class not extending the base class. However, your base class only defines a getter method for the $id property but no setter method.
Doctrine Entities work as POPOs (Plain Old PHP Objects). To achieve extending correctly you need to work with the MappedSuperClass here is an example
What I'm trying to do is to get an Post(Post Entity) form where I can select a featured picture from another entity(File Entity).
At the end I would like to display only the files of type "featured Image" but even if I remove my query_builder I've got a exception that says:
Class "Site\Backend\Adminbundle\Entity\File" seems not to be a managed Doctrine entity. Did you forget to map it?
Here is my PostType form
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('title')
->add('thumb', 'entity', array(
'class' => 'Site\Backend\Adminbundle\Entity\File',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('f')
//->where('f.state = :state')
//->setParameter('state', $prms['state'])
->orderBy('f.dateUpdated', 'DESC');
}
))
}
In the other hand I have my two entities:
Here is my File entity
<?php
namespace Site\Backend\AdminBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Site\Backend\AdminBundle\Entity\File
*
* #ORM\Table()
* #ORM\Entity
* #ORM\HasLifecycleCallbacks
*/
class File {
/**
* #var integer $id
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var \DateTime
*
* #ORM\Column(name="dateCreated", type="datetime")
*/
private $dateCreated;
/**
* #var \DateTime
*
* #ORM\Column(name="dateUpdated", type="datetime")
*/
private $dateUpdated;
/**
* #var string $name
*
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* #Assert\File(maxSize="6000000")
*/
private $file;
/**
* #ORM\Column(type="string", length=255)
*/
public $path;
/**
* #ORM\ManyToOne(targetEntity="TypeFile", inversedBy="files")
*/
private $type;
/**
* #var string
*
* #ORM\OneToMany(targetEntity="Site\Backend\BlogBundle\Entity\Post", mappedBy="thumb")
*/
private $posts;
public function __construct() {
$this->setDateCreated(new \DateTime());
$this->setDateUpdated(new \DateTime());
$this->type = new ArrayCollection();
}
/**
* #ORM\PrePersist()
* #ORM\PreUpdate()
*/
public function preUpload() {
$this->setDateUpdated(new \DateTime());
if (null !== $this->file) {
// do whatever you want to generate a unique name
$this->path = sha1(uniqid(mt_rand(), true)) . '.' . $this->file->guessExtension();
} else {
//throwException($e);
}
}
/**
* #ORM\PostPersist()
* #ORM\PostUpdate()
*/
public function upload() {
if (null === $this->file) {
return;
}
// if there is an error when moving the file, an exception will
// be automatically thrown by move(). This will properly prevent
// the entity from being persisted to the database on error
$this->file->move($this->getUploadRootDir(), $this->path);
unset($this->file);
}
/**
* #ORM\PostRemove()
*/
public function removeUpload() {
if ($file = $this->getAbsolutePath()) {
if (file_exists($file)) {
unlink($file);
}
}
}
public function getAbsolutePath() {
return null === $this->path ? null : $this->getUploadRootDir() . '/' . $this->path;
}
public function getWebPath() {
return null === $this->path ? null : './' . $this->getUploadDir() . '/' . $this->path;
}
protected function getUploadRootDir() {
// the absolute directory path where uploaded documents should be saved
return __DIR__ . '/../../../../../web/' . $this->getUploadDir();
}
protected function getUploadDir() {
// get rid of the __DIR__ so it doesn't screw when displaying uploaded doc/image in the view.
return 'uploads/' . $this->type;
}
/**
* Get id
*
* #return integer
*/
public function getId() {
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return File
*/
public function setName($name) {
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName() {
return $this->name;
}
/**
* Set path
*
* #param string $path
* #return File
*/
public function setPath($path) {
$this->path = $path;
return $this;
}
/**
* Get path
*
* #return string
*/
public function getPath() {
return $this->path;
}
public function __toString() {
return $this->name;
}
/**
* Add type
*
* #param Site\Backend\AdminBundle\Entity\TypeFile $type
* #return File
*/
public function addType(\Site\Backend\AdminBundle\Entity\TypeFile $type) {
$this->type[] = $type;
return $this;
}
/**
* Remove type
*
* #param Site\Backend\AdminBundle\Entity\TypeFile $type
*/
public function removeType(\Site\Backend\AdminBundle\Entity\TypeFile $type) {
$this->type->removeElement($type);
}
/**
* Get type
*
* #return Doctrine\Common\Collections\Collection
*/
public function getType() {
return $this->type;
}
/**
* Set type
*
* #param Site\Backend\AdminBundle\Entity\TypeFile $type
* #return File
*/
public function setType(\Site\Backend\AdminBundle\Entity\TypeFile $type = null) {
$this->type = $type;
return $this;
}
public function getFile() {
return $this->file;
}
public function setFile($file) {
$this->file = $file;
}
/**
* Set dateCreated
*
* #param \DateTime $dateCreated
* #return File
*/
public function setDateCreated($dateCreated) {
$this->dateCreated = $dateCreated;
return $this;
}
/**
* Get dateCreated
*
* #return \DateTime
*/
public function getDateCreated() {
return $this->dateCreated;
}
/**
* Set dateUpdated
*
* #param \DateTime $dateUpdated
* #return File
*/
public function setDateUpdated($dateUpdated) {
$this->dateUpdated = $dateUpdated;
return $this;
}
/**
* Get dateUpdated
*
* #return \DateTime
*/
public function getDateUpdated() {
return $this->dateUpdated;
}
/**
* Add posts
*
* #param \Site\Backend\BlogBundle\Entity\Post $posts
* #return File
*/
public function addPost(\Site\Backend\BlogBundle\Entity\Post $posts)
{
$this->posts[] = $posts;
return $this;
}
/**
* Remove posts
*
* #param \Site\Backend\BlogBundle\Entity\Post $posts
*/
public function removePost(\Site\Backend\BlogBundle\Entity\Post $posts)
{
$this->posts->removeElement($posts);
}
/**
* Get posts
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getPosts()
{
return $this->posts;
}
}
And Here is my Post entity
<?php
namespace Site\Backend\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Post
*
* #ORM\Table()
* #ORM\Entity
*/
class Post
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* #var \DateTime
*
* #ORM\Column(name="dateCreated", type="datetime")
*/
private $dateCreated;
/**
* #var \DateTime
*
* #ORM\Column(name="dateUpdated", type="datetime")
*/
private $dateUpdated;
/**
* #var \DateTime
*
* #ORM\Column(name="datePublished", type="datetime", nullable=true)
*/
private $datePublished;
/**
* #var string
*
* #ORM\Column(name="state", type="string", length=255)
*/
private $state;
/**
* #var string
*
* #ORM\Column(name="content", type="text")
*/
private $content;
/**
* #var Author
* #ORM\ManyToOne(targetEntity="Site\Backend\AdminBundle\Entity\User", inversedBy="posts")
*/
private $author;
/**
* #var Thumb
* #ORM\ManyToOne(targetEntity="Site\Backend\AdminBundle\Entity\File", inversedBy="posts")
*/
private $thumb;
/**
* #var Comments
* #ORM\OneToMany(targetEntity="Comment", mappedBy="post")
*/
private $comments;
/**
* #var Categories
* #ORM\ManyToMany(targetEntity="Category", cascade={"persist"})
*/
private $categories;
/**
* #var Tags
* #ORM\ManyToMany(targetEntity="Tag", cascade={"persist"})
*/
private $tags;
/**
* Constructeur
*/
public function __construct()
{
$this->setDateCreated(new \DateTime());
$this->setDateUpdated(new \DateTime());
$this->comments = new ArrayCollection();
$this->Categories = new ArrayCollection();
$this->tags = new ArrayCollection();
}
public function __toString(){
return $this->title;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* #param string $title
* #return Post
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set dateCreated
*
* #param \DateTime $dateCreated
* #return Comment
*/
public function setDateCreated($dateCreated)
{
$this->dateCreated = $dateCreated;
return $this;
}
/**
* Get dateCreated
*
* #return \DateTime
*/
public function getDateCreated()
{
return $this->dateCreated;
}
/**
* Set dateUpdated
*
* #param \DateTime $dateUpdated
* #return Comment
*/
public function setDateUpdated($dateUpdated)
{
$this->dateUpdated = $dateUpdated;
return $this;
}
/**
* Get dateUpdated
*
* #return \DateTime
*/
public function getDateUpdated()
{
return $this->dateUpdated;
}
/**
* Get datePublished
*
* #return \DateTime
*/
public function getDatePublished()
{
return $this->datePublished;
}
/**
* Set datePublished
*
* #param \DateTime $datePublished
* #return Comment
*/
public function setDatePublished($datePublished)
{
$this->datePublished = $datePublished;
return $this;
}
/**
* Set state
*
* #param string $state
* #return Post
*/
public function setState($state)
{
$this->state = $state;
return $this;
}
/**
* Get state
*
* #return string
*/
public function getState()
{
return $this->state;
}
/**
* Set content
*
* #param string $content
* #return Post
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content
*
* #return string
*/
public function getContent()
{
return $this->content;
}
/**
* Set author
*
* #param \Site\Backend\AdminBundle\Entity\User $author
* #return Post
*/
public function setAuthor(\Site\Backend\AdminBundle\Entity\User $author = null)
{
$this->author = $author;
return $this;
}
/**
* Get author
*
* #return \Site\Backend\AdminBundle\Entity\User
*/
public function getAuthor()
{
return $this->author;
}
/**
* Add comments
*
* #param \Site\Backend\BlogBundle\Entity\Comment $comments
* #return Post
*/
public function addComment(\Site\Backend\BlogBundle\Entity\Comment $comments)
{
$this->comments[] = $comments;
return $this;
}
/**
* Remove comments
*
* #param \Site\Backend\BlogBundle\Entity\Comment $comments
*/
public function removeComment(\Site\Backend\BlogBundle\Entity\Comment $comments)
{
$this->comments->removeElement($comments);
}
/**
* Get comments
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getComments()
{
return $this->comments;
}
/**
* Add Categories
*
* #param \Site\Backend\BlogBundle\Entity\Category $categories
* #return Post
*/
public function addCategory(\Site\Backend\BlogBundle\Entity\Category $category)
{
$this->categories[] = $category;
return $this;
}
/**
* Add Categories
*
* #param \Site\Backend\BlogBundle\Entity\Category $categories
* #return Post
*/
public function addCategoriesNew(\Site\Backend\BlogBundle\Entity\Category $category)
{
$this->categories[] = $category;
return $this;
}
/**
* Remove Categories
*
* #param \Site\Backend\BlogBundle\Entity\Category $categories
*/
public function removeCategory(\Site\Backend\BlogBundle\Entity\Category $category)
{
$this->categories->removeElement($category);
}
/**
* Remove Categories
*
* #param \Site\Backend\BlogBundle\Entity\Category $categories
*/
public function removeCategoriesNew(\Site\Backend\BlogBundle\Entity\Category $category)
{
}
/**
* Get Categories
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getCategories()
{
return $this->categories;
}
/**
* Get Categories
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getCategoriesNew()
{
//return $this->categories;
}
/**
* Add tags
*
* #param \Site\Backend\BlogBundle\Entity\tag $tags
* #return Post
*/
public function addTag(\Site\Backend\BlogBundle\Entity\Tag $tags)
{
$this->tags[] = $tags;
return $this;
}
/**
* Add tags
*
* #param \Site\Backend\BlogBundle\Entity\tag $tags
* #return Post
*/
public function addTagsNew(\Site\Backend\BlogBundle\Entity\Tag $tags)
{
$this->tags[] = $tags;
return $this;
}
/**
* Remove tags
*
* #param \Site\Backend\BlogBundle\Entity\Tag $tags
*/
public function removeTag(\Site\Backend\BlogBundle\Entity\Tag $tags)
{
$this->tags->removeElement($tags);
}
/**
* Remove Tags
*
* #param \Site\Backend\BlogBundle\Entity\tag $tags
*/
public function removeTagsNew(\Site\Backend\BlogBundle\Entity\Tag $tags)
{
//$this->tags->removeElement($tags);
}
/**
* Get Tags
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getTags()
{
return $this->tags;
}
/**
* Get Tags
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getTagsNew()
{
//return $this->Tags;
}
/**
* Add Categories
*
* #param \Site\Backend\BlogBundle\Entity\Category $categories
* #return Post
*/
public function addCategorie(\Site\Backend\BlogBundle\Entity\Category $categories)
{
$this->categories[] = $categories;
return $this;
}
/**
* Remove Categories
*
* #param \Site\Backend\BlogBundle\Entity\Category $categories
*/
public function removeCategorie(\Site\Backend\BlogBundle\Entity\Category $categories)
{
$this->categories->removeElement($categories);
}
/**
* #ORM\preUpdate
*/
public function setUpdateValue(){
$this->setDateUpdated(new \DateTime());
}
/**
* Set thumb
*
* #param \Site\Backend\AdminBundle\Entity\File $thumb
* #return Post
*/
public function setThumb(\Site\Backend\AdminBundle\Entity\File $thumb = null)
{
$this->thumb = $thumb;
return $this;
}
/**
* Get thumb
*
* #return \Site\Backend\AdminBundle\Entity\File
*/
public function getThumb()
{
return $this->thumb;
}
}
Thanks in advance
I hope I was clear, if It is not the case please tell me.
Try changing:
Site\Backend\Adminbundle\Entity\File
to
Site\Backend\AdminBundle\Entity\File
An other way to resolve the case :
->add('thumb', null, array(
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('f')
->select('f')
->leftJoin('f.type', 't')
->where('t.name = :type')
->setParameter('type', 'Featured Images')
->orderBy('f.dateUpdated', 'DESC');
}
))
In case you are working with multiple connections, you might have forgotten to map your bundle. Make sure it's the case in app/config/config.yml:
orm:
entity_managers:
default:
mappings:
AppBundle: ~
SiteBackendAdminBundle: ~
Or if you want to split in several entity managers:
orm:
entity_managers:
default:
mappings:
AppBundle: ~
admin:
mappings:
SiteBackendAdminBundle: ~
Documentation Symfony/Doctrine: Multiple Entity Managers.