I have table item with item_id, item_title, item_description, item_created, item_approved. I also have a table article with PK item_id (from item table) and article_body.
Now I would like to select all the articles where item.item_approved is NOT equal to NULL. But I'm stuck with creating the query. This is what I have now:
$entityManager = $this->getDoctrine()->getManager();
$repository = $entityManager->getRepository('VolleyScoutBundle:Article');
$query = $repository->createQueryBuilder('a')
->where('a.item.ItemApproved != NULL')
->getQuery();
$articles = $query->getResult();
This gave me the error: [Syntax Error] line 0, col 73: Error: Expected =, <, <=, <>, >, >=, !=, got '.'
This is my Article Entity:
<?php
namespace VolleyScout\VolleyScoutBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Article
*
* #ORM\Table(name="article")
* #ORM\Entity
*/
class Article
{
/**
* #var string
*
* #ORM\Column(name="article_body", type="text", nullable=false)
*/
private $articleBody;
/**
* #var \VolleyScout\VolleyScoutBundle\Entity\Item
*
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
* #ORM\OneToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Item")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="item_id", referencedColumnName="item_id")
* })
*/
private $item;
/**
* Set articleBody
*
* #param string $articleBody
* #return Article
*/
public function setArticleBody($articleBody)
{
$this->articleBody = $articleBody;
return $this;
}
/**
* Get articleBody
*
* #return string
*/
public function getArticleBody()
{
return $this->articleBody;
}
/**
* Set item
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Item $item
* #return Article
*/
public function setItem(\VolleyScout\VolleyScoutBundle\Entity\Item $item)
{
$this->item = $item;
return $this;
}
/**
* Get item
*
* #return \VolleyScout\VolleyScoutBundle\Entity\Item
*/
public function getItem()
{
return $this->item;
}
}
This is my Item Entity:
<?php
namespace VolleyScout\VolleyScoutBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Item
*
* #ORM\Table(name="item", indexes={#ORM\Index(name="fk_item_users1_idx", columns={"user_id"}), #ORM\Index(name="fk_item_myteam1_idx", columns={"myteam_id"})})
* #ORM\Entity
*/
class Item
{
/**
* #var string
*
* #ORM\Column(name="item_title", type="string", length=255, nullable=false)
*/
private $itemTitle;
/**
* #var string
*
* #ORM\Column(name="item_description", type="text", nullable=false)
*/
private $itemDescription;
/**
* #var \DateTime
*
* #ORM\Column(name="item_created", type="datetime", nullable=false)
*/
private $itemCreated;
/**
* #var \DateTime
*
* #ORM\Column(name="item_approved", type="datetime", nullable=true)
*/
private $itemApproved;
/**
* #var \DateTime
*
* #ORM\Column(name="item_deleted", type="datetime", nullable=true)
*/
private $itemDeleted;
/**
* #var integer
*
* #ORM\Column(name="item_id", type="bigint")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $itemId;
/**
* #var \VolleyScout\VolleyScoutBundle\Entity\Myteam
*
* #ORM\ManyToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Myteam")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="myteam_id", referencedColumnName="myteam_id")
* })
*/
private $myteam;
/**
* #var \VolleyScout\VolleyScoutBundle\Entity\Users
*
* #ORM\ManyToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Users")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
* })
*/
private $user;
/**
* Set itemTitle
*
* #param string $itemTitle
* #return Item
*/
public function setItemTitle($itemTitle)
{
$this->itemTitle = $itemTitle;
return $this;
}
/**
* Get itemTitle
*
* #return string
*/
public function getItemTitle()
{
return $this->itemTitle;
}
/**
* Set itemDescription
*
* #param string $itemDescription
* #return Item
*/
public function setItemDescription($itemDescription)
{
$this->itemDescription = $itemDescription;
return $this;
}
/**
* Get itemDescription
*
* #return string
*/
public function getItemDescription()
{
return $this->itemDescription;
}
/**
* Set itemCreated
*
* #param \DateTime $itemCreated
* #return Item
*/
public function setItemCreated($itemCreated)
{
$this->itemCreated = $itemCreated;
return $this;
}
/**
* Get itemCreated
*
* #return \DateTime
*/
public function getItemCreated()
{
return $this->itemCreated;
}
/**
* Set itemApproved
*
* #param \DateTime $itemApproved
* #return Item
*/
public function setItemApproved($itemApproved)
{
$this->itemApproved = $itemApproved;
return $this;
}
/**
* Get itemApproved
*
* #return \DateTime
*/
public function getItemApproved()
{
return $this->itemApproved;
}
/**
* Set itemDeleted
*
* #param \DateTime $itemDeleted
* #return Item
*/
public function setItemDeleted($itemDeleted)
{
$this->itemDeleted = $itemDeleted;
return $this;
}
/**
* Get itemDeleted
*
* #return \DateTime
*/
public function getItemDeleted()
{
return $this->itemDeleted;
}
/**
* Get itemId
*
* #return integer
*/
public function getItemId()
{
return $this->itemId;
}
/**
* Set myteam
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Myteam $myteam
* #return Item
*/
public function setMyteam(\VolleyScout\VolleyScoutBundle\Entity\Myteam $myteam = null)
{
$this->myteam = $myteam;
return $this;
}
/**
* Get myteam
*
* #return \VolleyScout\VolleyScoutBundle\Entity\Myteam
*/
public function getMyteam()
{
return $this->myteam;
}
/**
* Set user
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Users $user
* #return Item
*/
public function setUser(\VolleyScout\VolleyScoutBundle\Entity\Users $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return \VolleyScout\VolleyScoutBundle\Entity\Users
*/
public function getUser()
{
return $this->user;
}
}
try:
$query = $repository->createQueryBuilder('a')
->join('a.item', 'i')
->where('i.ItemApproved is not NULL')
->getQuery();
Related
I am building a shipping system for an eCommerce site using doctrine. To do this I have get the appropriate shipping methods and prices based on product and region data in the checkout.
I am using the following code with the QueryBuilder:
$shippingPriceReccords
= $this->em->createQueryBuilder()
->select('price')
->from('OrderShippingPrice', 'price')
->innerJoin('OrderShippingMethod', 'method', 'price.fkOrderShippingMethod = method.id')
->innerJoin('OrderShippingMethodRegionMapping', 'map', 'map.fkOrderShippingMethod = method.id')
->where('price.fkProductType = :fkProductType')
->andwhere('price.fkBrand = :fkBrand')
->andwhere('map.fkRegion = :fkRegion')
->setParameters([
'fkProductType' => $fkProductType,
'fkBrand' => $fkBrand,
'fkRegion' => $regionID,
])
->getQuery()
->setFetchMode("OrderCart", "address", \Doctrine\ORM\Mapping\ClassMetadata::FETCH_EAGER)
->getResult();
But this is failing and giving me this error:
[Syntax Error] line 0, col 87: Error: Expected Literal, got 'JOIN'
The DQL from the above query:
SELECT price FROM OrderShippingPrice price INNER JOIN OrderShippingMethod method INNER JOIN OrderShippingMethodRegionMapping map WHERE price.fkProductType = :fkProductType AND price.fkBrand = :fkBrand AND map.fkRegion = :fkRegion
OrderShippingPrice contains:
<?php
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
/**
* OrderShippingPrice
*
* #ORM\Table(name="orderShippingPrice")
* #ORM\Entity
*/
class OrderShippingPrice
{
/**
*
* Normal string / int object data
*
*/
/**
* #var int
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var int
*
* #ORM\Column(name="fkOrderShippingMethod", type="integer", nullable=true)
*/
private $fkOrderShippingMethod = '0';
/**
* #var float
*
* #ORM\Column(name="price", type="float", precision=7, scale=2, nullable=false)
*/
private $price = '0.00';
/**
* #var int
*
* #ORM\Column(name="fkProductType", type="integer", nullable=true)
*/
private $fkProductType = '0';
/**
* #var int
*
* #ORM\Column(name="fkBrand", type="integer", nullable=true)
*/
private $fkBrand = '0';
/**
* #var string
*
* #ORM\Column(name="isExpeditable", type="string", length=16, nullable=false)
*/
private $isExpeditable = '0';
/**
* #var string
*
* #ORM\Column(name="expediteDescription", type="text", length=65535, nullable=false)
*/
private $expediteDescription = '';
/**
* #var string
*
* #ORM\Column(name="showLiftGateOption", type="string", length=16, nullable=false)
*/
private $showLiftGateOption = '0';
/**
* #var string
*
* #ORM\Column(name="showDestinationOption", type="string", length=16, nullable=false)
*/
private $showDestinationOption = '0';
/**
* #var string
*
* #ORM\Column(name="productNotAvailable", type="string", length=16, nullable=false)
*/
private $productNotAvailable = '0';
/**
*
* Relationship managment propperties
*
*/
/**
* Many OrderShippingPrices have One OrderShippingMethod.
* #ORM\ManyToOne(targetEntity="OrderShippingMethod", fetch="EAGER")
* #ORM\JoinColumn(name="fkOrderShippingMethod", referencedColumnName="id")
**/
private $orderShippingMethod;
/**
* Get orderOrderShippingMethod
*
* #return OrderShippingMethod
*/
public function getOrderShippingMethod()
{
return $this->orderShippingMethod;
}
/**
*
* Normal string / int getters and setters
*
*/
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set fkOrderShippingMethod
*
* #param string $fkOrderShippingMethod
*
* #return OrderCart
*/
public function setFkOrderShippingMethod($fkOrderShippingMethod)
{
$this->fkOrderShippingMethod = $fkOrderShippingMethod;
return $this;
}
/**
* Get fkOrderShippingMethod
*
* #return string
*/
public function getFkOrderShippingMethod()
{
return $this->fkOrderShippingMethod;
}
/**
* Set price
*
* #param string $price
*
* #return OrderCart
*/
public function setPrice($price)
{
$this->price = $price;
return $this;
}
/**
* Get price
*
* #return string
*/
public function getPrice()
{
return $this->price;
}
/**
* Set fkProductType
*
* #param string $fkProductType
*
* #return OrderCart
*/
public function setFkProductType($fkProductType)
{
$this->fkProductType = $fkProductType;
return $this;
}
/**
* Get fkProductType
*
* #return string
*/
public function getFkProductType()
{
return $this->fkProductType;
}
/**
* Set fkBrand
*
* #param string $fkBrand
*
* #return OrderCart
*/
public function setFkBrand($fkBrand)
{
$this->fkBrand = $fkBrand;
return $this;
}
/**
* Get fkBrand
*
* #return string
*/
public function getFkBrand()
{
return $this->fkBrand;
}
/**
* Set isExpeditable
*
* #param string $isExpeditable
*
* #return OrderCart
*/
public function setIsExpeditable($isExpeditable)
{
$this->isExpeditable = $isExpeditable;
return $this;
}
/**
* Get isExpeditable
*
* #return string
*/
public function getIsExpeditable()
{
return $this->isExpeditable;
}
/**
* Set expediteDescription
*
* #param string $expediteDescription
*
* #return OrderCart
*/
public function setExpediteDescription($expediteDescription)
{
$this->expediteDescription = $expediteDescription;
return $this;
}
/**
* Get expediteDescription
*
* #return string
*/
public function getExpediteDescription()
{
return $this->expediteDescription;
}
/**
* Set showLiftGateOption
*
* #param string $showLiftGateOption
*
* #return OrderCart
*/
public function setShowLiftGateOption($showLiftGateOption)
{
$this->showLiftGateOption = $showLiftGateOption;
return $this;
}
/**
* Get showLiftGateOption
*
* #return string
*/
public function getShowLiftGateOption()
{
return $this->showLiftGateOption;
}
/**
* Set showDestinationOption
*
* #param string $showDestinationOption
*
* #return OrderCart
*/
public function setShowDestinationOption($showDestinationOption)
{
$this->showDestinationOption = $showDestinationOption;
return $this;
}
/**
* Get showDestinationOption
*
* #return string
*/
public function getShowDestinationOption()
{
return $this->showDestinationOption;
}
/**
* Set productNotAvailable
*
* #param string $productNotAvailable
*
* #return OrderCart
*/
public function setProductNotAvailable($productNotAvailable)
{
$this->productNotAvailable = $productNotAvailable;
return $this;
}
/**
* Get productNotAvailable
*
* #return string
*/
public function getProductNotAvailable()
{
return $this->productNotAvailable;
}
}
OrderShippingMethod contains:
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
/**
* OrderShippingMethod
*
* #ORM\Table(name="orderShippingMethod")
* #ORM\Entity
*/
class OrderShippingMethod
{
/**
*
* Normal string / int object data
*
*/
/**
* #var int
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="text", length=255, nullable=false)
*/
private $name = '';
/**
* #var string
*
* #ORM\Column(name="carrier", type="string", length=32, nullable=false)
*/
private $carrier = '';
/**
* #var string
*
* #ORM\Column(name="bvCode", type="string", length=32, nullable=false)
*/
private $bvCode = '';
/**
* #var string
*
* #ORM\Column(name="trackingUrl", type="string", length=255, nullable=true)
*/
private $trackingUrl = '';
/**
* #var int
*
* #ORM\Column(name="minimumLeadTime", type="integer", nullable=false)
*/
private $minimumLeadTime = '0';
/**
* #var int
*
* #ORM\Column(name="maximumLeadTime", type="integer", nullable=false)
*/
private $maximumLeadTime = '0';
/**
*
* Relationship managment propperties
*
*/
/**
* #ORM\OneToMany(targetEntity="OrderShippingMethodRegionMapping", mappedBy="orderShippingMethod", cascade={"persist", "remove"}, orphanRemoval=true, fetch="EAGER")
* #var orderShippingMethodRegionMappings[]
**/
public $orderShippingMethodRegionMappings;
/**
* Constructor
*
* Create array for collection of orderShippingMethodRegionMappings
*/
public function __construct()
{
$this->orderShippingMethodRegionMappings = new ArrayCollection();
}
/**
* Get orderShippingMethodRegionMapping(s)
*
* #return array
*/
public function getOrderShippingMethodRegionMappings()
{
return $this->orderShippingMethodRegionMappings;
}
/**
*
* Normal string / int getters and setters
*
*/
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param int $name
*
* #return OrderCart
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return int
*/
public function getName()
{
return $this->name;
}
/**
* Set carrier
*
* #param string $carrier
*
* #return OrderCart
*/
public function setCarrier($carrier)
{
$this->carrier = $carrier;
return $this;
}
/**
* Get carrier
*
* #return string
*/
public function getCarrier()
{
return $this->carrier;
}
/**
* Set bvCode
*
* #param string $bvCode
*
* #return OrderCart
*/
public function setBvCode($bvCode)
{
$this->bvCode = $bvCode;
return $this;
}
/**
* Get bvCode
*
* #return string
*/
public function getBvCode()
{
return $this->bvCode;
}
/**
* Set trackingUrl
*
* #param string $trackingUrl
*
* #return OrderCart
*/
public function setTrackingUrl($trackingUrl)
{
$this->trackingUrl = $trackingUrl;
return $this;
}
/**
* Get trackingUrl
*
* #return string
*/
public function getTrackingUrl()
{
return $this->trackingUrl;
}
/**
* Set minimumLeadTime
*
* #param string $minimumLeadTime
*
* #return OrderCart
*/
public function setMinimumLeadTime($minimumLeadTime)
{
$this->minimumLeadTime = $minimumLeadTime;
return $this;
}
/**
* Get minimumLeadTime
*
* #return string
*/
public function getMinimumLeadTime()
{
return $this->minimumLeadTime;
}
/**
* Set maximumLeadTime
*
* #param string $maximumLeadTime
*
* #return OrderCart
*/
public function setMaximumLeadTime($maximumLeadTime)
{
$this->maximumLeadTime = $maximumLeadTime;
return $this;
}
/**
* Get maximumLeadTime
*
* #return string
*/
public function getMaximumLeadTime()
{
return $this->maximumLeadTime;
}
}
OrderShippingMethodRegionMapping contains:
<?php
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
/**
* OrderShippingMethodRegionMapping
*
* #ORM\Table(name="orderShippingMethodRegionMapping")
* #ORM\Entity
*/
class OrderShippingMethodRegionMapping
{
/**
*
* Normal string / int object data
*
*/
/**
* #var int
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var int
*
* #ORM\Column(name="fkOrderShippingMethod", type="integer")
*/
private $fkOrderShippingMethod = '0';
/**
* #var int
*
* #ORM\Column(name="fkOrderRegion", type="integer")
*/
private $fkOrderRegion = '0';
/**
*
* Relationship managment propperties
*
*/
/**
* Many OrderShippingPrices have One OrderShippingMethod.
* #ORM\ManyToOne(targetEntity="OrderShippingMethod", inversedBy="orderShippingMethodRegionMappings")
* #ORM\JoinColumn(name="fkOrderShippingMethod", referencedColumnName="id")
**/
private $orderShippingMethod;
/**
* Sets a new OrderShippingMethod and cleans the previous one if set
* #param OrderShippingMethod
*/
public function setOrderShippingMethod(OrderShippingMethod $orderShippingMethod)
{
$this->orderShippingMethod = $orderShippingMethod;
}
/**
* Get orderOrderShippingMethod
*
* #return OrderShippingMethod
*/
public function getOrderShippingMethod()
{
return $this->orderShippingMethod;
}
/**
*
* Normal string / int getters and setters
*
*/
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set fkOrderShippingMethod
*
* #param int $fkOrderShippingMethod
*
* #return OrderCart
*/
public function setFkOrderShippingMethod($fkOrderShippingMethod)
{
$this->fkOrderShippingMethod = $fkOrderShippingMethod;
return $this;
}
/**
* Get fkOrderShippingMethod
*
* #return int
*/
public function getFkOrderShippingMethod()
{
return $this->fkOrderShippingMethod;
}
/**
* Set fkOrderRegion
*
* #param int $fkOrderRegion
*
* #return OrderCart
*/
public function setFkOrderRegion($fkOrderRegion)
{
$this->fkOrderRegion = $fkOrderRegion;
return $this;
}
/**
* Get fkOrderRegion
*
* #return int
*/
public function getFkOrderRegion()
{
return $this->fkOrderRegion;
}
}
Can someone tell me what I am doing wrong? Thanks for the help.
You are missing an argument in the innerJoin method call. You have to do this :
$shippingPriceReccords
= $this->em->createQueryBuilder()
->select('price')
->from('OrderShippingPrice', 'price')
->innerJoin('OrderShippingMethod', 'method', 'WITH', 'price.fkOrderShippingMethod = method.id')
->innerJoin('OrderShippingMethodRegionMapping', 'map', 'WITH', 'map.fkOrderShippingMethod = method.id')
->where('price.fkProductType = :fkProductType')
->andwhere('price.fkBrand = :fkBrand')
->andwhere('map.fkRegion = :fkRegion')
->setParameters([
'fkProductType' => $fkProductType,
'fkBrand' => $fkBrand,
'fkRegion' => $regionID,
])
->getQuery()
->setFetchMode("OrderCart", "address", \Doctrine\ORM\Mapping\ClassMetadata::FETCH_EAGER)
->getResult();
See the documentation :
// Example - $qb->innerJoin('u.Group', 'g', Expr\Join::WITH, $qb->expr()->eq('u.status_id', '?1'))
// Example - $qb->innerJoin('u.Group', 'g', 'WITH', 'u.status = ?1')
// Example - $qb->innerJoin('u.Group', 'g', 'WITH', 'u.status = ?1', 'g.id')
public function innerJoin($join, $alias, $conditionType = null, $condition = null, $indexBy = null);
i have been learning zend framework for the past days now. I have encountered this error when i tried to run the site:
Annotation #ORM\PrePersist is not allowed to be declared on property Post\Entity\Post::$alterado. You may only use this annotation on these code elements: METHOD.
heres my Post.php file:
<?php
namespace Post\Entity;
use Application\Entity\AbstractEntity;
use Doctrine\ORM\Mapping as ORM;
/**
* Post
*
* #ORM\Table(name="post")
* #ORM\Entity
* #ORM\HasLifecycleCallbacks
* #ORM\Entity(repositoryClass="Post\Entity\PostRepository")
*/
class Post extends AbstractEntity
{
/**
* #var integer
* #ORM\Id
* #ORM\Column(name="id", type="integer", nullable=false)
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="titulo", type="string", length=80, nullable=false)
*/
private $titulo;
/**
* #var string
*
* #ORM\Column(name="descricao", type="string", length=150, nullable=false)
*/
private $descricao;
/**
* #var string
*
* #ORM\Column(name="texto", type="text", nullable=false)
*/
private $texto;
/**
* #var \DateTime
*
* #ORM\Column(name="cadastro", type="datetime", nullable=false)
*/
private $cadastro;
/**
* #var \DateTime
* #ORM\PrePersist
* #ORM\Column(name="alterado", type="datetime", nullable=false)
*/
private $alterado;
/**
* #var boolean
* #ORM\PosUpdate
* #ORM\Column(name="ativo", type="boolean", nullable=false)
*/
private $ativo = '0';
/**
* #var \Categoria\Entity\Category
*
* #ORM\ManyToOne(targetEntity="Categoria\Entity\Category")
* #ORM\JoinColumn(name="categoria_id", referencedColumnName="id")
*/
private $categoriaId;
/**
* Set id
*
* #param integer $id
*
* #return Post
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set titulo
*
* #param string $titulo
*
* #return Post
*/
public function setTitulo($titulo)
{
$this->titulo = $titulo;
return $this;
}
/**
* Get titulo
*
* #return string
*/
public function getTitulo()
{
return $this->titulo;
}
/**
* Set descricao
*
* #param string $descricao
*
* #return Post
*/
public function setDescricao($descricao)
{
$this->descricao = $descricao;
return $this;
}
/**
* Get descricao
*
* #return string
*/
public function getDescricao()
{
return $this->descricao;
}
/**
* Set texto
*
* #param string $texto
*
* #return Post
*/
public function setTexto($texto)
{
$this->texto = $texto;
return $this;
}
/**
* Get texto
*
* #return string
*/
public function getTexto()
{
return $this->texto;
}
/**
* Set cadastro
*
* #param \DateTime $cadastro
*
* #return Post
*/
public function setCadastro($cadastro)
{
$this->cadastro = $cadastro;
return $this;
}
/**
* Get cadastro
*
* #return \DateTime
*/
public function getCadastro()
{
return $this->cadastro;
}
/**
* Set alterado
*
* #param \DateTime $alterado
*
* #return Post
*/
public function setAlterado($alterado)
{
$this->alterado = $alterado;
return $this;
}
/**
* Get alterado
*
* #return \DateTime
*/
public function getAlterado()
{
return $this->alterado;
}
/**
* Set ativo
*
* #param boolean $ativo
*
* #return Post
*/
public function setAtivo($ativo)
{
$this->ativo = $ativo;
return $this;
}
/**
* Get ativo
*
* #return boolean
*/
public function getAtivo()
{
return $this->ativo;
}
/**
* Set categoriaId
*
* #param \Categoria\Entity\Category $categoriaId
*
* #return Post
*/
public function setCategoriaId(\Categoria\Entity\Category $categoriaId = null)
{
$this->categoriaId = $categoriaId;
return $this;
}
/**
* Get categoriaId
*
* #return \Categoria\Entity\Category
*/
public function getCategoriaId()
{
return $this->categoriaId;
}
}
Am i missing any "use" statement on the file?
Any help is apreciated, thank you for your atention :)
I fixed it, i should have call #ORM\PrePersist and #ORM\PosUpdate on the method. :)
I have two entities Categorie and ChampCat with ManyToMany relation and I want to get list of categories with related ChampCat by using doctrine QueryBuilder so I have used this query:
class CategorieRepository extends \Doctrine\ORM\EntityRepository
{
public function myFindCategories(array $tab){
$em = $this->getEntityManager();
$query = $em->select(array('cat', 'ch'))
->from('AnnonceBundle\Entity\Categorie', 'cat')
->join('cat.champsCat', 'ch')
->where('cat.id In (:tabOfIds)')
->setParameter('tabOfIds', array_values($tab))
->getQuery();
return $query->getResult();
}
}
Here u can see the Categorie Entity:
<?php
namespace AnnonceBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Categorie
*
* #ORM\Table(name="categorie")
* #ORM\Entity(repositoryClass="AnnonceBundle\Repository\CategorieRepository")
*/
class Categorie implements \JsonSerializable
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="refCat", type="string", length=100)
*/
private $refCat;
/**
* #var string
*
* #ORM\Column(name="libelleCat", type="string", length=50)
*/
private $libelleCat;
/**
* #ORM\ManyToMany(targetEntity="AnnonceBundle\Entity\ChampCat",cascade={"persist"})
*/
private $champsCat;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set refCat
*
* #param string $refCat
*
* #return Categorie
*/
public function setRefCat($refCat)
{
$this->refCat = $refCat;
return $this;
}
/**
* Get refCat
*
* #return string
*/
public function getRefCat()
{
return $this->refCat;
}
/**
* Set libelleCat
*
* #param string $libelleCat
*
* #return Categorie
*/
public function setLibelleCat($libelleCat)
{
$this->libelleCat = $libelleCat;
return $this;
}
/**
* Get libelleCat
*
* #return string
*/
public function getLibelleCat()
{
return $this->libelleCat;
}
/**
* Set champsCat
*
* #param \AnnonceBundle\Entity\ChampCat $champsCat
*
* #return Categorie
*/
public function setChampsCat(\AnnonceBundle\Entity\ChampCat $champsCat = null)
{
$this->champsCat = $champsCat;
return $this;
}
/**
* Get champsCat
*
* #return \AnnonceBundle\Entity\ChampCat
*/
public function getChampsCat()
{
return $this->champsCat;
}
/**
* Constructor
*/
public function __construct()
{
$this->champsCat = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add champsCat
*
* #param \AnnonceBundle\Entity\ChampCat $champsCat
*
* #return Categorie
*/
public function addChampsCat(\AnnonceBundle\Entity\ChampCat $champsCat)
{
$this->champsCat[] = $champsCat;
return $this;
}
/**
* Remove champsCat
*
* #param \AnnonceBundle\Entity\ChampCat $champsCat
*/
public function removeChampsCat(\AnnonceBundle\Entity\ChampCat $champsCat)
{
$this->champsCat->removeElement($champsCat);
}
function jsonSerialize()
{
return get_object_vars($this);
}
}
And this is ChamCat Entity:
<?php
namespace AnnonceBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* ChampCat
*
* #ORM\Table(name="champ_cat")
* #ORM\Entity(repositoryClass="AnnonceBundle\Repository\ChampCatRepository")
*/
class ChampCat implements \JsonSerializable
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
*#var string
*
* #ORM\Column(name="refCh", type="string")
*/
private $refCh;
/**
* #var string
*
* #ORM\Column(name="nom_ch", type="string", length=255)
*/
private $nomCh;
/**
* #var bool
*
* #ORM\Column(name="app_ch", type="boolean")
*/
private $appCh;
/**
* #var string
*
* #ORM\Column(name="format_ch", type="string", length=255)
*/
private $formatCh;
/**
* Get id
*
* #return string
*/
public function getId()
{
return $this->refCh;
}
/**
* Set refCh
*
* #param integer $refCh
* #return ChampCat
*/
public function setRefCh($refCh)
{
$this->refCh = $refCh;
return $this;
}
/**
* Get refCh
*
* #return integer
*/
public function getRefCh()
{
return $this->refCh;
}
/**
* Set nomCh
*
* #param string $nomCh
* #return ChampCat
*/
public function setNomCh($nomCh)
{
$this->nomCh = $nomCh;
return $this;
}
/**
* Get nomCh
*
* #return string
*/
public function getNomCh()
{
return $this->nomCh;
}
/**
* Set appCh
*
* #param boolean $appCh
* #return ChampCat
*/
public function setAppCh($appCh)
{
$this->appCh = $appCh;
return $this;
}
/**
* Get appCh
*
* #return boolean
*/
public function getAppCh()
{
return $this->appCh;
}
/**
* Set formatCh
*
* #param string $formatCh
* #return ChampCat
*/
public function setFormatCh($formatCh)
{
$this->formatCh = $formatCh;
return $this;
}
/**
* Get formatCh
*
* #return string
*/
public function getFormatCh()
{
return $this->formatCh;
}
function jsonSerialize()
{
return get_object_vars($this);
}
}
Unfortunately this query didn't work, so what am I missing ?
Is it working with this syntax?
public function myFindCategories(array $tab){
if(count($tab) == 0) return []; //or IN() will bug with an empty array
$query = $this->createQueryBuilder('cat')
->addSelect('ch')
->innerJoin('cat.champsCat', 'ch')
->where('cat.id in (:tabOfIds)')
->setParameter('tabOfIds', $tab)
->getQuery();
return $query->getResult();
}
what's the wrong in this code
i want to create query with multi join but unfortunately this error displayed
[this is my SearchController controller]
class SearchController extends Controller {
public function indexAction() {
$em = $this->get('doctrine.orm.entity_manager');
$users = $em
->createQueryBuilder('P')
->select('P.pname, Q.qname,P.gender,P.phone,P.yearsOfExperience,P.graduationYear,c.cname')
->add('from', 'serachBundle:Person P')
->Join('P.Qualification', 'Q')
->Join('Q.Category', 'c')
->where('P.qualificationId=Q.id')
->andwhere('Q.categoryId=c.id')
->getQuery();
$user = $users->getResult();
print_r($user);
//qualification
$Qlist = $this->getDoctrine()
->getRepository('serachBundle:qualification')
->findAll();
//category
$Catlist = $this->getDoctrine()
->getRepository('serachBundle:category')
->findAll();
return $this->render('serachBundle:Default:Search.html.twig', array('user' => $user, 'Qlist' => $Qlist, 'Catlist' => $Catlist));
}
this my person entity
<?php
namespace Search\serachBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Person
*
* #ORM\Table(name="person", indexes={#ORM\Index(name="companyId", columns={"companyId", "qualificationId"}), #ORM\Index(name="qualificationId", columns={"qualificationId"}), #ORM\Index(name="IDX_34DCD1762480E723", columns={"companyId"})})
* #ORM\Entity
*/
class Person
{
/**
* #var string
*
* #ORM\Column(name="pname", type="string", length=255, nullable=false)
*/
private $pname;
/**
* #var string
*
* #ORM\Column(name="address", type="string", length=255, nullable=false)
*/
private $address;
/**
* #var string
*
* #ORM\Column(name="phone", type="string", length=255, nullable=false)
*/
private $phone;
/**
* #var string
*
* #ORM\Column(name="gender", type="string", length=255, nullable=false)
*/
private $gender;
/**
* #var integer
*
* #ORM\Column(name="yearsOfExperience", type="integer", nullable=false)
*/
private $yearsofexperience;
/**
* #var integer
*
* #ORM\Column(name="graduationYear", type="integer", nullable=false)
*/
private $graduationyear;
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var \Search\serachBundle\Entity\Company
*
* #ORM\ManyToOne(targetEntity="Search\serachBundle\Entity\Company")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="companyId", referencedColumnName="id")
* })
*/
private $companyid;
/**
* #var \Search\serachBundle\Entity\Qualification
*
* #ORM\ManyToOne(targetEntity="Search\serachBundle\Entity\Qualification")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="qualificationId", referencedColumnName="id")
* })
*/
private $qualificationid;
/**
* Set pname
*
* #param string $pname
* #return Person
*/
public function setPname($pname)
{
$this->pname = $pname;
return $this;
}
/**
* Get pname
*
* #return string
*/
public function getPname()
{
return $this->pname;
}
/**
* Set address
*
* #param string $address
* #return Person
*/
public function setAddress($address)
{
$this->address = $address;
return $this;
}
/**
* Get address
*
* #return string
*/
public function getAddress()
{
return $this->address;
}
/**
* Set phone
*
* #param string $phone
* #return Person
*/
public function setPhone($phone)
{
$this->phone = $phone;
return $this;
}
/**
* Get phone
*
* #return string
*/
public function getPhone()
{
return $this->phone;
}
/**
* Set gender
*
* #param string $gender
* #return Person
*/
public function setGender($gender)
{
$this->gender = $gender;
return $this;
}
/**
* Get gender
*
* #return string
*/
public function getGender()
{
return $this->gender;
}
/**
* Set yearsofexperience
*
* #param integer $yearsofexperience
* #return Person
*/
public function setYearsofexperience($yearsofexperience)
{
$this->yearsofexperience = $yearsofexperience;
return $this;
}
/**
* Get yearsofexperience
*
* #return integer
*/
public function getYearsofexperience()
{
return $this->yearsofexperience;
}
/**
* Set graduationyear
*
* #param integer $graduationyear
* #return Person
*/
public function setGraduationyear($graduationyear)
{
$this->graduationyear = $graduationyear;
return $this;
}
/**
* Get graduationyear
*
* #return integer
*/
public function getGraduationyear()
{
return $this->graduationyear;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set companyid
*
* #param \Search\serachBundle\Entity\Company $companyid
* #return Person
*/
public function setCompanyid(\Search\serachBundle\Entity\Company $companyid = null)
{
$this->companyid = $companyid;
return $this;
}
/**
* Get companyid
*
* #return \Search\serachBundle\Entity\Company
*/
public function getCompanyid()
{
return $this->companyid;
}
/**
* Set qualificationid
*
* #param \Search\serachBundle\Entity\Qualification $qualificationid
* #return Person
*/
public function setQualificationid(\Search\serachBundle\Entity\Qualification $qualificationid = null)
{
$this->qualificationid = $qualificationid;
return $this;
}
/**
* Get qualificationid
*
* #return \Search\serachBundle\Entity\Qualification
*/
public function getQualificationid()
{
return $this->qualificationid;
}
}
(Entities are objects with identity. Their identity has a conceptual meaning inside your domain. In a CMS application each article has a unique id. You can uniquely identify each article by that id.)
this my Qualification entity
<?php
namespace Search\serachBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Qualification
*
* #ORM\Table(name="qualification", indexes={#ORM\Index(name="categoryId", columns={"categoryId"})})
* #ORM\Entity
*/
class Qualification
{
/**
* #var string
*
* #ORM\Column(name="qname", type="string", length=255, nullable=false)
*/
private $qname;
/**
* #var string
*
* #ORM\Column(name="specialty", type="string", length=255, nullable=false)
*/
private $specialty;
/**
* #var string
*
* #ORM\Column(name="qualifDesc", type="string", length=255, nullable=false)
*/
private $qualifdesc;
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var \Search\serachBundle\Entity\Category
*
* #ORM\ManyToOne(targetEntity="Search\serachBundle\Entity\Category")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="categoryId", referencedColumnName="id")
* })
*/
private $categoryid;
/**
* Set qname
*
* #param string $qname
* #return Qualification
*/
public function setQname($qname)
{
$this->qname = $qname;
return $this;
}
/**
* Get qname
*
* #return string
*/
public function getQname()
{
return $this->qname;
}
/**
* Set specialty
*
* #param string $specialty
* #return Qualification
*/
public function setSpecialty($specialty)
{
$this->specialty = $specialty;
return $this;
}
/**
* Get specialty
*
* #return string
*/
public function getSpecialty()
{
return $this->specialty;
}
/**
* Set qualifdesc
*
* #param string $qualifdesc
* #return Qualification
*/
public function setQualifdesc($qualifdesc)
{
$this->qualifdesc = $qualifdesc;
return $this;
}
/**
* Get qualifdesc
*
* #return string
*/
public function getQualifdesc()
{
return $this->qualifdesc;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set categoryid
*
* #param \Search\serachBundle\Entity\Category $categoryid
* #return Qualification
*/
public function setCategoryid(\Search\serachBundle\Entity\Category $categoryid = null)
{
$this->categoryid = $categoryid;
return $this;
}
/**
* Get categoryid
*
* #return \Search\serachBundle\Entity\Category
*/
public function getCategoryid()
{
return $this->categoryid;
}
}
Thanks in advance if anyone can solve this. It would be a really great help
You named the relations as $qualificationid, so you should make your query something like this:
->Join('P.qualificationid', 'Q')
->Join('Q.categoryid', 'c')
Remember to make all queries inside repositories classes. And please, try to rename your bundle from serach to search :-D
I'm trying to putting up my first join with Symfony2 createNativeQuery.
I made the first one without join and there's no problem.
I think I lost some steps, to follow my code:
$rsm = new ResultSetMapping;
$rsm
->addEntityResult('Art\ArticleBundle\Entity\ArticleData', 'ad')
// ->addEntityResult('Art\ArticleBundle\Entity\Article', 'a')
// ->addFieldResult('ad', 'id', 'article_data_id')
->addJoinedEntityResult('Art\ArticleBundle\Entity\Article', 'a', 'ad', 'article')
->addFieldResult('a', 'date_article', 'dateArticle');
$query = $this->_em->createNativeQuery(
'SELECT ad.id AS article_data_id, ad.title, ad.abstract '
. 'FROM art_article_data ad '
. 'INNER JOIN art_article ON (ad.article_id = a.id) '
,
$rsm
);
return $query->getResult();
It returns the following error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'a.id' in 'on clause'").
Why a.id is unknown? It is within the entity and the table.
What step have I lost ?
As #Javad required, below there's the Article entity
<?php
namespace Art\ArticleBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Art\ArticleBundle\Entity\ArticleType;
use Art\ArticleBundle\Entity\ArticleCategory;
use Art\ArticleBundle\Entity\ArticleData;
use Art\ArticleBundle\Entity\ArticleHasBrand;
/**
* Article
*
* #ORM\Entity(repositoryClass="Art\ArticleBundle\Entity\Repository\ArticleRepository")
* #ORM\Table(name="art_article", options={"collate"="utf8_general_ci", "charset"="utf8"})
* #ORM\HasLifecycleCallbacks()
*/
class Article
{
function __construct()
{
$this->articles__article_data = new ArrayCollection();
$this->articles_article_has_brand = new ArrayCollection();
$this->setDateInsert(new \DateTime);
$this->setDateEdit(new \DateTime);
$this->setIsVisible(1);
$this->setIsMain(0);
$this->setOrd(0);
}
function __toString()
{
// $this->dateArticle;
}
/**
* #var integer
*
* #ORM\Id
* #ORM\Column(name="id", type="integer", options={"unsigned"=true})
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var \stdClass
*
* #ORM\ManyToOne(targetEntity="ArticleType", inversedBy="article_types__article")
* #ORM\JoinColumn(name="article_type_id", referencedColumnName="id", nullable=false)
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $articleType;
/**
* #var \stdClass
*
* #ORM\ManyToOne(targetEntity="ArticleCategory", inversedBy="article_categories__article")
* #ORM\JoinColumn(name="article_category_id", referencedColumnName="id", nullable=true)
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $articleCategory;
/**
* #var \DateTime
*
* #ORM\Column(name="date_article", type="date", nullable=true)
*/
private $dateArticle;
/**
* #var string
*
* #ORM\Column(name="url", type="string", length=127, nullable=true)
*/
private $url;
/**
* #var \DateTime
*
* #ORM\Column(name="date_insert", type="datetime", nullable=false)
*/
private $dateInsert;
/**
* #var \DateTime
*
* #ORM\Column(name="date_edit", type="datetime", nullable=true)
*/
private $dateEdit;
/**
* #var boolean
*
* #ORM\Column(name="is_visible", type="boolean", options={"unsigned"=true, "default"=1}, nullable=false)
*/
private $isVisible;
/**
* #var boolean
*
* #ORM\Column(name="is_main", type="boolean", options={"unsigned"=true, "default"=0}, nullable=false)
*/
private $isMain;
/**
* #var integer
*
* #ORM\Column(name="ord", type="integer", options={"unsigned"=true, "default"=0}, nullable=true)
*/
private $ord;
/**
* #var object
*
* #ORM\OneToMany(targetEntity="ArticleData", mappedBy="article")
*/
private $articles__article_data;
/**
* #var object
*
* #ORM\OneToMany(targetEntity="ArticleHasBrand", mappedBy="article")
*/
private $articles_article_has_brand;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set dateArticle
*
* #param \DateTime $dateArticle
* #return Article
*/
public function setDateArticle($dateArticle)
{
$this->dateArticle = $dateArticle;
return $this;
}
/**
* Get dateArticle
*
* #return \DateTime
*/
public function getDateArticle()
{
return $this->dateArticle;
}
/**
* Get url
*
* #return string
*/
public function getUrl()
{
return $this->url;
}
/**
* Set dateInsert
*
* #param string $url
* #return Article
*/
public function setUrl($url)
{
$this->url = $url;
}
/**
* Set dateInsert
*
* #param \DateTime $dateInsert
* #return Article
*/
public function setDateInsert($dateInsert)
{
$this->dateInsert = $dateInsert;
return $this;
}
/**
* Get dateInsert
*
* #return \DateTime
*/
public function getDateInsert()
{
return $this->dateInsert;
}
/**
* Set dateEdit
*
* #param \DateTime $dateEdit
* #return Article
*/
public function setDateEdit($dateEdit)
{
$this->dateEdit = $dateEdit;
return $this;
}
/**
* Get dateEdit
*
* #return \DateTime
*/
public function getDateEdit()
{
return $this->dateEdit;
}
/**
* Set isVisible
*
* #param boolean $isVisible
* #return Article
*/
public function setIsVisible($isVisible)
{
$this->isVisible = $isVisible;
return $this;
}
/**
* Get isVisible
*
* #return boolean
*/
public function getIsVisible()
{
return $this->isVisible;
}
/**
* Set isMain
*
* #param boolean $isMain
* #return Article
*/
public function setIsMain($isMain)
{
$this->isMain = $isMain;
return $this;
}
/**
* Get isMain
*
* #return boolean
*/
public function getIsMain()
{
return $this->isMain;
}
/**
* Set ord
*
* #param integer $ord
* #return Article
*/
public function setOrd($ord)
{
$this->ord = $ord;
return $this;
}
/**
* Get ord
*
* #return integer
*/
public function getOrd()
{
return $this->ord;
}
/**
* Set articleType
*
* #param \Art\ArticleBundle\Entity\ArticleType $articleType
* #return Article
*/
public function setArticleType(ArticleType $articleType = null)
{
$this->articleType = $articleType;
return $this;
}
/**
* Get articleType
*
* #return \Art\ArticleBundle\Entity\ArticleType
*/
public function getArticleType()
{
return $this->articleType;
}
/**
* Set articleCategory
*
* #param \Art\ArticleBundle\Entity\ArticleCategory $articleCategory
* #return Article
*/
public function setArticleCategory(ArticleCategory $articleCategory = null)
{
$this->articleCategory = $articleCategory;
return $this;
}
/**
* Get articleCategory
*
* #return \Art\ArticleBundle\Entity\ArticleCategory
*/
public function getArticleCategory()
{
return $this->articleCategory;
}
/**
* Add articles__article_data
*
* #param \Art\ArticleBundle\Entity\ArticleData $articlesArticleData
* #return Article
*/
public function addArticlesArticleData(ArticleData $articlesArticleData)
{
$this->articles__article_data[] = $articlesArticleData;
return $this;
}
/**
* Remove articles__article_data
*
* #param \Art\ArticleBundle\Entity\ArticleData $articlesArticleData
*/
public function removeArticlesArticleData(ArticleData $articlesArticleData)
{
$this->articles__article_data->removeElement($articlesArticleData);
}
/**
* Get articles__article_data
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getArticlesArticleData()
{
return $this->articles__article_data;
}
/**
* Add articles_article_has_brand
*
* #param \Art\ArticleBundle\Entity\ArticleHasBrand $articlesArticleHasBrand
* #return Article
*/
public function addArticlesArticleHasBrand(ArticleHasBrand $articlesArticleHasBrand)
{
$this->articles_article_has_brand[] = $articlesArticleHasBrand;
return $this;
}
/**
* Remove articles_article_has_brand
*
* #param \Art\ArticleBundle\Entity\ArticleHasBrand $articlesArticleHasBrand
*/
public function removeArticlesArticleHasBrand(ArticleHasBrand $articlesArticleHasBrand)
{
$this->articles_article_has_brand->removeElement($articlesArticleHasBrand);
}
/**
* Get articles_article_has_brand
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getArticlesArticleHasBrand()
{
return $this->articles_article_has_brand;
}
}
Your query should be:
$query = $this->_em->createNativeQuery(
'SELECT ad.id AS article_data_id, ad.title, ad.abstract '
. 'FROM art_article_data ad '
. 'INNER JOIN art_article a ON (ad.article_id = a.id) '
,
$rsm
);
You missed the alias for art_article