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
Related
Environment:
"require": {
"php": "5.6",
"zendframework/zendframework": "2.5.0",
"zendframework/zend-servicemanager": "2.5.1",
"zendframework/zend-developer-tools": "1.0.0",
"doctrine/doctrine-orm-module": "0.9.2",
"acelaya/zf2-acmailer": "4.*",
"tasmaniski/zend-params-helper": "^1.0"
}
Fatal error: Uncaught exception 'Zend\Stdlib\Exception\BadMethodCallException' with message 'The option "filters" does not have a callable "setFilters" ("setfilters") setter method which must be defined' in C:\OSPanel\domains\csmCrew_github\vendor\zendframework\zend-servicemanager\src\ServiceManager.php on line 1135
I am trying to add SQL filter using this DareDevels tutorial
my interface: VacanciesFilterInterface.php
namespace Common\DoctrineFilter;
Interface VacanciesFilterInterface
{
public function setFilters($filters);
}
my filter class: VacanciesFilter.php
namespace Common\DoctrineFilter;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Query\Filter\SQLFilter;
class VacanciesFilter extends SQLFilter
{
/**
* Gets the SQL query part to add to a query.
*
* #param ClassMetaData $targetEntity
* #param string $targetTableAlias
*
* #return string The constraint SQL if there is available, empty string otherwise.
*/
public function addFilterConstraint (ClassMetadata $targetEntity, $targetTableAlias)
{
// Check if the entity implements the LocalAware interface
if (!$targetEntity->reflClass->implementsInterface('\Common\DoctrineFilter\VacanciesFilterInterface')) {
return '';
}
return $targetTableAlias.'.Vacancies = ' . $this->getParameter('Vacancies'); // getParameter applies quoting automatically
}
my module.config.php:
'doctrine' => array(
'driver' => array(
// defines an annotation driver with two paths, and names it `my_annotation_driver`
'common_entity' => array(
'class' => \Doctrine\ORM\Mapping\Driver\AnnotationDriver::class,
'cache' => 'array',
'paths' => array(
__DIR__ . '/../src/Common/Entity',
__DIR__ . '/../src/Common/Entity/Repository',
),
),
// default metadata driver, aggregates all other drivers into a single one.
// Override `orm_default` only if you know what you're doing
'orm_default' => array(
'drivers' => array(
// register `my_annotation_driver` for any entity under namespace `My\Namespace`
'Common\Entity' => 'common_entity',
),
'filters' => array(
'vacancies' => 'Common\DoctrineFilter\VacanciesFilter',
),
),
),
),
I add filter in my controller using these methods:
public function enableVacanciesFilter()
{
$em = $this->getEntityManager();
$filter = $em->getFilters()->enable('vacancies');
$filter->setParameter('vacancies', '41');
}
/**
* Disable locale filter
*/
public function disableVacanciesFilter()
{
$em = $this->getEntityManager();
$filter = $em->getFilters()->disable('vacancies');
}
So the question is: how can I implement that callable setFilters setter method ?
I have tried to implement Interface in my Entity:
<?php
namespace Common\Entity;
use Common\DoctrineFilter\VacanciesFilterInterface;
use Doctrine\ORM\Mapping as ORM;
/**
* Vacancies
*
* #ORM\Table(name="vacancies", indexes={#ORM\Index(name="RankId_fk", columns={"rank_id"}), #ORM\Index(name="id", columns={"id"}), #ORM\Index(name="FK_vacancies_crewing_companies", columns={"company_id"}), #ORM\Index(name="FK_vacancies_vessels", columns={"vessel_id"}), #ORM\Index(name="FK_vacancies_vacancy_positions", columns={"position"}), #ORM\Index(name="status_id", columns={"status_id"})})
* #ORM\Entity
*/
class Vacancies implements VacanciesFilterInterface
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=128, nullable=false)
*/
private $title = '';
/**
* #var string
*
* #ORM\Column(name="additional_info", type="string", length=255, nullable=false)
*/
private $additionalInfo = '';
/**
* #var integer
*
* #ORM\Column(name="duration_of_contract_in_months", type="smallint", nullable=false)
*/
private $durationOfContractInMonths;
/**
* #var \DateTime
*
* #ORM\Column(name="joining_date", type="date", nullable=false)
*/
private $joiningDate;
/**
* #var integer
*
* #ORM\Column(name="salary_per_month", type="integer", nullable=false)
*/
private $salaryPerMonth;
/**
* #var boolean
*
* #ORM\Column(name="required_age_from", type="boolean", nullable=false)
*/
private $requiredAgeFrom;
/**
* #var boolean
*
* #ORM\Column(name="required_age_to", type="boolean", nullable=false)
*/
private $requiredAgeTo;
/**
* #var \DateTime
*
* #ORM\Column(name="creation_date", type="datetime", nullable=false)
*/
private $creationDate;
/**
* #var integer
*
* #ORM\Column(name="views_count", type="integer", nullable=true)
*/
private $viewsCount = '0';
/**
* #var integer
*
* #ORM\Column(name="job_seekers_applied", type="integer", nullable=true)
*/
private $jobSeekersApplied = '0';
/**
* #var boolean
*
* #ORM\Column(name="is_published", type="boolean", nullable=false)
*/
private $isPublished = '0';
/**
* #var \Common\Entity\CrewingCompanies
*
* #ORM\ManyToOne(targetEntity="Common\Entity\CrewingCompanies")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="company_id", referencedColumnName="id")
* })
*/
private $company;
/**
* #var \Common\Entity\VacancyPositions
*
* #ORM\ManyToOne(targetEntity="Common\Entity\VacancyPositions")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="position", referencedColumnName="id")
* })
*/
private $position;
/**
* #var \Common\Entity\Vessels
*
* #ORM\ManyToOne(targetEntity="Common\Entity\Vessels")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="vessel_id", referencedColumnName="id")
* })
*/
private $vessel;
/**
* #var \Common\Entity\Ranks
*
* #ORM\ManyToOne(targetEntity="Common\Entity\Ranks")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="rank_id", referencedColumnName="id")
* })
*/
private $rank;
/**
* #var \Common\Entity\VacancyStatuses
*
* #ORM\ManyToOne(targetEntity="Common\Entity\VacancyStatuses")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="status_id", referencedColumnName="id")
* })
*/
private $status;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* #param string $title
*
* #return Vacancies
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set additionalInfo
*
* #param string $additionalInfo
*
* #return Vacancies
*/
public function setAdditionalInfo($additionalInfo)
{
$this->additionalInfo = $additionalInfo;
return $this;
}
/**
* Get additionalInfo
*
* #return string
*/
public function getAdditionalInfo()
{
return $this->additionalInfo;
}
/**
* Set durationOfContractInMonths
*
* #param integer $durationOfContractInMonths
*
* #return Vacancies
*/
public function setDurationOfContractInMonths($durationOfContractInMonths)
{
$this->durationOfContractInMonths = $durationOfContractInMonths;
return $this;
}
/**
* Get durationOfContractInMonths
*
* #return integer
*/
public function getDurationOfContractInMonths()
{
return $this->durationOfContractInMonths;
}
/**
* Set joiningDate
*
* #param \DateTime $joiningDate
*
* #return Vacancies
*/
public function setJoiningDate($joiningDate)
{
$this->joiningDate = $joiningDate;
return $this;
}
/**
* Get joiningDate
*
* #return \DateTime
*/
public function getJoiningDate()
{
return $this->joiningDate;
}
/**
* Set salaryPerMonth
*
* #param integer $salaryPerMonth
*
* #return Vacancies
*/
public function setSalaryPerMonth($salaryPerMonth)
{
$this->salaryPerMonth = $salaryPerMonth;
return $this;
}
/**
* Get salaryPerMonth
*
* #return integer
*/
public function getSalaryPerMonth()
{
return $this->salaryPerMonth;
}
/**
* Set requiredAgeFrom
*
* #param boolean $requiredAgeFrom
*
* #return Vacancies
*/
public function setRequiredAgeFrom($requiredAgeFrom)
{
$this->requiredAgeFrom = $requiredAgeFrom;
return $this;
}
/**
* Get requiredAgeFrom
*
* #return boolean
*/
public function getRequiredAgeFrom()
{
return $this->requiredAgeFrom;
}
/**
* Set requiredAgeTo
*
* #param boolean $requiredAgeTo
*
* #return Vacancies
*/
public function setRequiredAgeTo($requiredAgeTo)
{
$this->requiredAgeTo = $requiredAgeTo;
return $this;
}
/**
* Get requiredAgeTo
*
* #return boolean
*/
public function getRequiredAgeTo()
{
return $this->requiredAgeTo;
}
/**
* Set creationDate
*
* #param \DateTime $creationDate
*
* #return Vacancies
*/
public function setCreationDate($creationDate)
{
$this->creationDate = $creationDate;
return $this;
}
/**
* Get creationDate
*
* #return \DateTime
*/
public function getCreationDate()
{
return $this->creationDate;
}
/**
* Set viewsCount
*
* #param integer $viewsCount
*
* #return Vacancies
*/
public function setViewsCount($viewsCount)
{
$this->viewsCount = $viewsCount;
return $this;
}
/**
* Get viewsCount
*
* #return integer
*/
public function getViewsCount()
{
return $this->viewsCount;
}
/**
* Set jobSeekersApplied
*
* #param integer $jobSeekersApplied
*
* #return Vacancies
*/
public function setJobSeekersApplied($jobSeekersApplied)
{
$this->jobSeekersApplied = $jobSeekersApplied;
return $this;
}
/**
* Get jobSeekersApplied
*
* #return integer
*/
public function getJobSeekersApplied()
{
return $this->jobSeekersApplied;
}
/**
* Set isPublished
*
* #param boolean $isPublished
*
* #return Vacancies
*/
public function setIsPublished($isPublished)
{
$this->isPublished = $isPublished;
return $this;
}
/**
* Get isPublished
*
* #return boolean
*/
public function getIsPublished()
{
return $this->isPublished;
}
/**
* Set company
*
* #param \Common\Entity\CrewingCompanies $company
*
* #return Vacancies
*/
public function setCompany(\Common\Entity\CrewingCompanies $company = null)
{
$this->company = $company;
return $this;
}
/**
* Get company
*
* #return \Common\Entity\CrewingCompanies
*/
public function getCompany()
{
return $this->company;
}
/**
* Set position
*
* #param \Common\Entity\VacancyPositions $position
*
* #return Vacancies
*/
public function setPosition(\Common\Entity\VacancyPositions $position = null)
{
$this->position = $position;
return $this;
}
/**
* Get position
*
* #return \Common\Entity\VacancyPositions
*/
public function getPosition()
{
return $this->position;
}
/**
* Set vessel
*
* #param \Common\Entity\Vessels $vessel
*
* #return Vacancies
*/
public function setVessel(\Common\Entity\Vessels $vessel = null)
{
$this->vessel = $vessel;
return $this;
}
/**
* Get vessel
*
* #return \Common\Entity\Vessels
*/
public function getVessel()
{
return $this->vessel;
}
/**
* Set rank
*
* #param \Common\Entity\Ranks $rank
*
* #return Vacancies
*/
public function setRank(\Common\Entity\Ranks $rank = null)
{
$this->rank = $rank;
return $this;
}
/**
* Get rank
*
* #return \Common\Entity\Ranks
*/
public function getRank()
{
return $this->rank;
}
/**
* Set status
*
* #param \Common\Entity\VacancyStatuses $status
*
* #return Vacancies
*/
public function setStatus(\Common\Entity\VacancyStatuses $status = null)
{
$this->status = $status;
return $this;
}
/**
* Get status
*
* #return \Common\Entity\VacancyStatuses
*/
public function getStatus()
{
return $this->status;
}
public function setFilters ($filters)
{
$this->filters = $filters;
// TODO: Implement setFilters() method.
}
}
But nothing helps to deal with that error.
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 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();