Symfony2, doctrine limit and offset doesn't work as expected - php

I'm trying to get results from my table using limit and offsett from my table, and this table has a relations with another two tables.. one of them is MayToOne and the other is OneToOne
If I don't use setFirstResult and setMaxResults the results are the expected because return the information of my table and the information of the other tables related but using setFirst.. and setMax.. only return the information of the table and not the information of the tables related.
$query = $this->repository->createQueryBuilder('p')
->where('LOWER(p.name) LIKE LOWER(:term)')
->orWhere('LOWER(p.summary) LIKE LOWER(:term)')
->orwhere('LOWER(p.description) LIKE LOWER(:term)')
->setParameter('term', '%'.$parameters['term'].'%')
->setFirstResult($offset)
->setMaxResults($limit)
->orderBy('p.id','DESC')
->getQuery();
$attractions = $query->getResult();
And I'm trying with paginator using this query:
$query = $this->repository->createQueryBuilder('p')
->where('LOWER(p.name) LIKE LOWER(:term)')
->orWhere('LOWER(p.summary) LIKE LOWER(:term)')
->orwhere('LOWER(p.description) LIKE LOWER(:term)')
->setParameter('term', '%'.$parameters['term'].'%')
->setFirstResult($offset)
->setMaxResults($limit)
->orderBy('p.id','DESC');
$attractions = new Paginator($query, $fetchJoinCollection = true);
But is not working because appear this error:
[Semantical Error] The annotation "#Enum" in property Doctrine\ORM\Mapping\GeneratedValue::$strategy was never imported. Did you maybe forget to add a "use" statement for this annotation?
Entity
<?php
namespace Acme\MyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Acme\MyBundle\Model\AttractionInterface;
use Gedmo\Mapping\Annotation as Gedmo;
use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Exclude;
use JMS\Serializer\Annotation\Expose;
use JMS\Serializer;
/**
* Attraction
*
* #ORM\Table(name="attractions")
* #ORM\Entity
*
* #ExclusionPolicy("all")
*/
class Attraction implements AttractionInterface
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #Expose
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=150, nullable=false)
* #Expose
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="latitude", type="string", length=30, nullable=false)
* #Expose
*/
private $latitude;
/**
* #var string
*
* #ORM\Column(name="longitude", type="string", length=30, nullable=false)
* #Expose
*/
private $longitude;
/**
* #var string
*
* #ORM\Column(name="summary", type="text", nullable=false)
* #Expose
*/
private $summary;
/**
* #var string
*
* #ORM\Column(name="description", type="text", nullable=true)
* #Expose
*/
private $description;
/**
* #var integer
*
* #ORM\Column(name="available_places", type="smallint")
* #Expose
*/
private $availablePlaces;
/**
* #var float
*
* #ORM\Column(name="original_price", type="float", nullable=false)
* #Expose
*/
private $originalPrice;
/**
* #var float
*
* #ORM\Column(name="new_price", type="float", nullable=false)
* #Expose
*/
private $newPrice;
/**
* #var \DateTime
*
* #ORM\Column(name="starting_point", type="datetime", nullable=false)
* #Expose
*/
private $startingPoint;
/**
* #var string
*
* #ORM\Column(name="picture", type="string", length=50, nullable=false)
* #Expose
*/
private $picture;
/**
* #var integer
*
* #ORM\ManyToOne(targetEntity="Vendor")
* #ORM\JoinColumn(name="vendor", referencedColumnName="id")
* #Expose
*/
private $vendor;
/**
* #var integer
*
* #ORM\OneToOne(targetEntity="Category")
* #ORM\JoinColumn(name="category", referencedColumnName="id")
* #Expose
*/
private $category;
/**
* #var \DateTime
*
* #ORM\Column(name="created_at", type="datetime", options={"default" = "CURRENT_TIMESTAMP"}, nullable=true)
* #Gedmo\Timestampable(on="create")
* #Expose
*/
private $createdAt;
/**
* #var \DateTime
*
* #ORM\Column(name="updated_at", type="datetime", options={"default" = "CURRENT_TIMESTAMP"}, nullable=true)
* #Expose
*/
private $updatedAt;
public function __toString()
{
return $this->name;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Attraction
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set latitude
*
* #param string $latitude
* #return Attraction
*/
public function setLatitude($latitude)
{
$this->latitude = $latitude;
return $this;
}
/**
* Get latitude
*
* #return string
*/
public function getLatitude()
{
return $this->latitude;
}
/**
* Set longitude
*
* #param string $longitude
* #return Attraction
*/
public function setLongitude($longitude)
{
$this->longitude = $longitude;
return $this;
}
/**
* Get longitude
*
* #return string
*/
public function getLongitude()
{
return $this->longitude;
}
/**
* Set summary
*
* #param string $summary
* #return Attraction
*/
public function setSummary($summary)
{
$this->summary = $summary;
return $this;
}
/**
* Get summary
*
* #return string
*/
public function getSummary()
{
return $this->summary;
}
/**
* Set description
*
* #param string $description
* #return Attraction
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set availablePlaces
*
* #param integer $availablePlaces
* #return Attraction
*/
public function setAvailablePlaces($availablePlaces)
{
$this->availablePlaces = $availablePlaces;
return $this;
}
/**
* Get availablePlaces
*
* #return integer
*/
public function getAvailablePlaces()
{
return $this->availablePlaces;
}
/**
* Set originalPrice
*
* #param float $originalPrice
* #return Attraction
*/
public function setOriginalPrice($originalPrice)
{
$this->originalPrice = $originalPrice;
return $this;
}
/**
* Get originalPrice
*
* #return float
*/
public function getOriginalPrice()
{
return $this->originalPrice;
}
/**
* Set newPrice
*
* #param float $newPrice
* #return Attraction
*/
public function setNewPrice($newPrice)
{
$this->newPrice = $newPrice;
return $this;
}
/**
* Get newPrice
*
* #return float
*/
public function getNewPrice()
{
return $this->newPrice;
}
/**
* Set startingPoint
*
* #param \DateTime $startingPoint
* #return Attraction
*/
public function setStartingPoint($startingPoint)
{
$this->startingPoint = \DateTime::createFromFormat('Y-m-d H:i:s', $startingPoint);
return $this;
}
/**
* Get startingPoint
*
* #return \DateTime
*/
public function getStartingPoint()
{
return $this->startingPoint;
}
/**
* Set picture
*
* #param string $picture
* #return Attraction
*/
public function setPicture($picture)
{
$this->picture = $picture;
return $this;
}
/**
* Get picture
*
* #return string
*/
public function getPicture()
{
return $this->picture;
}
/**
* Set vendor
*
* #param integer $vendor
* #return Attraction
*/
public function setVendor($vendor)
{
$this->vendor = $vendor;
return $this;
}
/**
* Get vendor
*
* #return integer
*/
public function getVendor()
{
return $this->vendor;
}
/**
* Set category
*
* #param integer $category
* #return Attraction
*/
public function setCategory($category)
{
$this->category = $category;
return $this;
}
/**
* Get categoryId
*
* #return integer
*/
public function getCategory()
{
return $this->category;
}
/**
* Set createdAt
*
* #param \DateTime $createdAt
* #return Attraction
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* #return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set updatedAt
*
* #param \DateTime $updatedAt
* #return Attraction
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* #return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
}

have you seen https://github.com/schmittjoh/JMSSerializerBundle/issues/380 ?
it seems having the same issue.
a question: doe it work without that complex query? for example with something like:
$query = $this->repository->createQueryBuilder('p')
->orderBy('p.id','DESC')
->getQuery();
$attractions = $query->getResult();

Related

Semantical Error line 0, col 140 near 'Q INNER JOIN': Error: Class Search\serachBundle\Entity\Person has no association named Qualification

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

using reflection methods in traits

I have 15 Entity Classes, and I want to put getEntityName static function in there but I don't want to make code duplication, and some of my entities extends vendor related class so I cannot put an abstract class to extend from these entities. I wanted to use traits.
<?php
namespace FrontendBundle\Traits\Entity;
trait GetEntityNameTrait {
public static function getEntityName()
{
return str_replace('\\', '\\\\', get_parent_class());
}
}
I use this trait in any of this entities, but I'm getting following compile error.
Compile Error: Cannot redeclare class FrontendBundle\Entity\Country
I tried several reflection methods all are the same result is this a bug or something is not logical ?
Others:
get_parent_class()
get_class()
get_called_class()
__CLASS__
<?php
namespace FrontendBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FrontendBundle\EntityInterface;
use Symfony\Component\Validator\Constraints as Assert;
use FrontendBundle\Traits\Entity\GetEntityNameTrait;
/**
* Country
*
* #ORM\Table(name="country", uniqueConstraints={#ORM\UniqueConstraint(name="id_UNIQUE", columns={"id"}), #ORM\UniqueConstraint(name="iso_UNIQUE", columns={"iso"}), #ORM\UniqueConstraint(name="slug_tr_UNIQUE", columns={"slug_tr"}), #ORM\UniqueConstraint(name="slug_en_UNIQUE", columns={"slug_en"})})
* #ORM\Entity(repositoryClass="FrontendBundle\Repository\CountryRepository")
* #ORM\HasLifecycleCallbacks
*/
class Country implements EntityInterface
{
use GetEntityNameTrait;
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="iso", type="string", length=2, nullable=false)
*/
private $iso;
/**
* #var string
*
* #ORM\Column(name="iso3", type="string", length=3, nullable=true)
*/
private $iso3;
/**
* #var string
*
* #ORM\Column(name="currency_code", type="string", length=3, nullable=true)
*/
private $currencyCode;
/**
* #var string
*
* #ORM\Column(name="currency_name", type="string", length=20, nullable=true)
*/
private $currencyName;
/**
* #var string
*
* #ORM\Column(name="currency_symbol", type="string", length=5, nullable=true)
*/
private $currencySymbol;
/**
* #var string
*
* #ORM\Column(name="name_en", type="string", length=80, nullable=false)
*/
private $nameEn;
/**
* #var string
*
* #ORM\Column(name="name_tr", type="string", length=80, nullable=false)
*/
private $nameTr;
/**
* #var integer
*
* #ORM\Column(name="numcode", type="smallint", nullable=true)
*/
private $numcode;
/**
* #var integer
*
* #ORM\Column(name="phonecode", type="integer", nullable=false)
*/
private $phonecode;
/**
* #var float
*
* #ORM\Column(name="latitude", type="float", precision=18, scale=10, nullable=false)
*/
private $latitude;
/**
* #var float
*
* #ORM\Column(name="longitude", type="float", precision=18, scale=10, nullable=false)
*/
private $longitude;
/**
* #var string
*
* #ORM\Column(name="slug_en", type="string", length=50, nullable=false)
*/
private $slugEn;
/**
* #var string
*
* #ORM\Column(name="slug_tr", type="string", length=50, nullable=false)
*/
private $slugTr;
/**
* #var \DateTime
*
* #ORM\Column(name="created_at", type="datetime", nullable=false)
*/
private $createdAt;
/**
* #var \DateTime
*
* #ORM\Column(name="updated_at", type="datetime", nullable=false)
*/
private $updatedAt;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set iso
*
* #param string $iso
* #return Country
*/
public function setIso($iso)
{
$this->iso = $iso;
return $this;
}
/**
* Get iso
*
* #return string
*/
public function getIso()
{
return $this->iso;
}
/**
* Set iso3
*
* #param string $iso3
* #return Country
*/
public function setIso3($iso3)
{
$this->iso3 = $iso3;
return $this;
}
/**
* Get iso3
*
* #return string
*/
public function getIso3()
{
return $this->iso3;
}
/**
* Set currencyCode
*
* #param string $currencyCode
* #return Country
*/
public function setCurrencyCode($currencyCode)
{
$this->currencyCode = $currencyCode;
return $this;
}
/**
* Get currencyCode
*
* #return string
*/
public function getCurrencyCode()
{
return $this->currencyCode;
}
/**
* Set currencyName
*
* #param string $currencyName
* #return Country
*/
public function setCurrencyName($currencyName)
{
$this->currencyName = $currencyName;
return $this;
}
/**
* Get currencyName
*
* #return string
*/
public function getCurrencyName()
{
return $this->currencyName;
}
/**
* Set currencySymbol
*
* #param string $currencySymbol
* #return Country
*/
public function setCurrencySymbol($currencySymbol)
{
$this->currencySymbol = $currencySymbol;
return $this;
}
/**
* Get currencySymbol
*
* #return string
*/
public function getCurrencySymbol()
{
return $this->currencySymbol;
}
/**
* Set nameEn
*
* #param string $nameEn
* #return Country
*/
public function setNameEn($nameEn)
{
$this->nameEn = $nameEn;
return $this;
}
/**
* Get nameEn
*
* #return string
*/
public function getNameEn()
{
return $this->nameEn;
}
/**
* Set nameTr
*
* #param string $nameTr
* #return Country
*/
public function setNameTr($nameTr)
{
$this->nameTr = $nameTr;
return $this;
}
/**
* Get nameTr
*
* #return string
*/
public function getNameTr()
{
return $this->nameTr;
}
/**
* Set numcode
*
* #param integer $numcode
* #return Country
*/
public function setNumcode($numcode)
{
$this->numcode = $numcode;
return $this;
}
/**
* Get numcode
*
* #return integer
*/
public function getNumcode()
{
return $this->numcode;
}
/**
* Set phonecode
*
* #param integer $phonecode
* #return Country
*/
public function setPhonecode($phonecode)
{
$this->phonecode = $phonecode;
return $this;
}
/**
* Get phonecode
*
* #return integer
*/
public function getPhonecode()
{
return $this->phonecode;
}
/**
* Set latitude
*
* #param float $latitude
* #return Country
*/
public function setLatitude($latitude)
{
$this->latitude = $latitude;
return $this;
}
/**
* Get latitude
*
* #return float
*/
public function getLatitude()
{
return $this->latitude;
}
/**
* Set longitude
*
* #param float $longitude
* #return Country
*/
public function setLongitude($longitude)
{
$this->longitude = $longitude;
return $this;
}
/**
* Get longitude
*
* #return float
*/
public function getLongitude()
{
return $this->longitude;
}
/**
* Set slugEn
*
* #param string $slugEn
* #return Country
*/
public function setSlugEn($slugEn)
{
$this->slugEn = $slugEn;
return $this;
}
/**
* Get slugEn
*
* #return string
*/
public function getSlugEn()
{
return $this->slugEn;
}
/**
* Set slugTr
*
* #param string $slugTr
* #return Country
*/
public function setSlugTr($slugTr)
{
$this->slugTr = $slugTr;
return $this;
}
/**
* Get slugTr
*
* #return string
*/
public function getSlugTr()
{
return $this->slugTr;
}
/**
* Set createdAt
*
* #param \DateTime $createdAt
* #return Country
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* #return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set updatedAt
*
* #param \DateTime $updatedAt
* #return Country
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* #return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
*
* #ORM\PrePersist
* #ORM\PreUpdate
*/
public function updatedTimestamps()
{
$this->setUpdatedAt(new \DateTime('now'));
if ($this->getCreatedAt() == null) {
$this->setCreatedAt(new \DateTime('now'));
}
}
}

Doctrine2: how to get one of the inversed side entities with ManyToOne relationship for a given id

I've two doctrine entities with ManyToOne relationship:
/**
* #ORM\ManyToOne(targetEntity="Iballot\CmsBundle\Entity\PollingStation2", cascade={"persist"})
* #ORM\JoinColumn(nullable=false)
* #Expose
*/
private $pollingStation2;
and I'd like get a specific one result for a given PollingStation2 ID and I've try that but doesn't work:
public function getPresidential($polId)
{
$qb = $this->createQueryBuilder('r');
$qb->where('r.pollingStation2 = :polling_station2_id')
->setParameter('polling_station2_id', $polId);
return $qb->getQuery()
->getResult();
}
Entities
namespace Iballot\CmsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* PollingStation2
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="Iballot\CmsBundle\Entity\PollingStation2Repository")
*/
class PollingStation2
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="verfierNumber", type="integer", length=255, nullable=true)
*/
protected $verfierNumber;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="verifier_number", type="string", length=255)
*/
private $verifierNumber;
/**
* #var string
*
* #ORM\Column(name="address", type="string", length=255)
*/
private $address;
/**
* #ORM\OneToMany(targetEntity="Iballot\CmsBundle\Entity\PollingStation2", mappedBy="pollingStation")
*/
protected $result;
/**
* #ORM\ManyToOne(targetEntity="Iballot\CmsBundle\Entity\Constituency", cascade={"persist"})
* #ORM\JoinColumn(nullable=false)
*/
private $constituency;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
public function __toString() {
return $this->getName();
}
/**
* Set name
*
* #param string $name
*
* #return PollingStation2
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set address
*
* #param string $address
*
* #return PollingStation2
*/
public function setAddress($address)
{
$this->address = $address;
return $this;
}
/**
* Get address
*
* #return string
*/
public function getAddress()
{
return $this->address;
}
/**
* Set verifierNumber
*
* #param integer $verifierNumber
*
* #return PollingStation2
*/
public function setVerifierNumber($verifierNumber)
{
$this->verifierNumber = $verifierNumber;
return $this;
}
/**
* Get verifierNumber
*
* #return integer
*/
public function getVerifierNumber()
{
return $this->verifierNumber;
}
/**
* Set verfierNumber
*
* #param integer $verfierNumber
*
* #return PollingStation2
*/
public function setVerfierNumber($verfierNumber)
{
$this->verfierNumber = $verfierNumber;
return $this;
}
/**
* Get verfierNumber
*
* #return integer
*/
public function getVerfierNumber()
{
return $this->verfierNumber;
}
/**
* Set constituency
*
* #param \Iballot\CmsBundle\Entity\Constituency $constituency
*
* #return PollingStation2
*/
public function setConstituency(\Iballot\CmsBundle\Entity\Constituency $constituency)
{
$this->constituency = $constituency;
return $this;
}
/**
* Get constituency
*
* #return \Iballot\CmsBundle\Entity\Constituency
*/
public function getConstituency()
{
return $this->constituency;
}
/**
* Constructor
*/
public function __construct()
{
$this->result = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add result
*
* #param \Iballot\CmsBundle\Entity\PollingStation2 $result
*
* #return PollingStation2
*/
public function addResult(\Iballot\CmsBundle\Entity\PollingStation2 $result)
{
$this->result[] = $result;
return $this;
}
/**
* Remove result
*
* #param \Iballot\CmsBundle\Entity\PollingStation2 $result
*/
public function removeResult(\Iballot\CmsBundle\Entity\PollingStation2 $result)
{
$this->result->removeElement($result);
}
/**
* Get result
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getResult()
{
return $this->result;
}
}

symfony 2 how to create or map entity in different bundles

I have project which have different bundles. I want to create or divide entities in different bundle how can I create this. I am new in symfony please help me . I created but I can not do mapping correctly . a table which have primary key can't use in another bundle.
Here is my Entity
namespace DomainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* WebsiteDomainConfigTest
*
* #ORM\Table(name="website_domain_config_test", indexes={#ORM\Index(name="fk_website_domain_config_test_1_idx", columns={"vendor_id"}), #ORM\Index(name="fk_website_domain_config_test_2_idx", columns={"country_id"})})
* #ORM\Entity
*/
class WebsiteDomainConfigTest
{
/**
* #var string
*
* #ORM\Column(name="domain", type="string", length=255, nullable=true)
*/
private $domain;
/**
* #var string
*
* #ORM\Column(name="vendor_code", type="string", length=15, nullable=true)
*/
private $vendorCode;
/**
* #var string
*
* #ORM\Column(name="domain_path", type="string", length=255, nullable=true)
*/
private $domainPath;
/**
* #var string
*
* #ORM\Column(name="assets_path", type="string", length=45, nullable=true)
*/
private $assetsPath;
/**
* #var string
*
* #ORM\Column(name="language", type="string", length=3, nullable=true)
*/
private $language;
/**
* #var string
*
* #ORM\Column(name="affiliate", type="string", length=25, nullable=true)
*/
private $affiliate;
/**
* #var string
*
* #ORM\Column(name="affiliate_logo", type="string", length=255, nullable=true)
*/
private $affiliateLogo;
/**
* #var string
*
* #ORM\Column(name="affiliate_address", type="string", length=255, nullable=true)
*/
private $affiliateAddress;
/**
* #var integer
*
* #ORM\Column(name="status", type="integer", nullable=true)
*/
private $status;
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var \MobileSplash\SplashRequestBundle\Entity\Countries
*
* #ORM\ManyToOne(targetEntity="MobileSplash\SplashRequestBundle\Entity\Countries")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="country_id", referencedColumnName="id")
* })
*/
private $country;
/**
* #var \MobileSplash\SplashRequestBundle\Entity\Vendors
*
* #ORM\ManyToOne(targetEntity="MobileSplash\SplashRequestBundle\Entity\Vendors")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="vendor_id", referencedColumnName="id")
* })
*/
private $vendor;
/**
* Set domain
*
* #param string $domain
* #return WebsiteDomainConfigTest
*/
public function setDomain($domain)
{
$this->domain = $domain;
return $this;
}
/**
* Get domain
*
* #return string
*/
public function getDomain()
{
return $this->domain;
}
/**
* Set vendorCode
*
* #param string $vendorCode
* #return WebsiteDomainConfigTest
*/
public function setVendorCode($vendorCode)
{
$this->vendorCode = $vendorCode;
return $this;
}
/**
* Get vendorCode
*
* #return string
*/
public function getVendorCode()
{
return $this->vendorCode;
}
/**
* Set domainPath
*
* #param string $domainPath
* #return WebsiteDomainConfigTest
*/
public function setDomainPath($domainPath)
{
$this->domainPath = $domainPath;
return $this;
}
/**
* Get domainPath
*
* #return string
*/
public function getDomainPath()
{
return $this->domainPath;
}
/**
* Set assetsPath
*
* #param string $assetsPath
* #return WebsiteDomainConfigTest
*/
public function setAssetsPath($assetsPath)
{
$this->assetsPath = $assetsPath;
return $this;
}
/**
* Get assetsPath
*
* #return string
*/
public function getAssetsPath()
{
return $this->assetsPath;
}
/**
* Set language
*
* #param string $language
* #return WebsiteDomainConfigTest
*/
public function setLanguage($language)
{
$this->language = $language;
return $this;
}
/**
* Get language
*
* #return string
*/
public function getLanguage()
{
return $this->language;
}
/**
* Set affiliate
*
* #param string $affiliate
* #return WebsiteDomainConfigTest
*/
public function setAffiliate($affiliate)
{
$this->affiliate = $affiliate;
return $this;
}
/**
* Get affiliate
*
* #return string
*/
public function getAffiliate()
{
return $this->affiliate;
}
/**
* Set affiliateLogo
*
* #param string $affiliateLogo
* #return WebsiteDomainConfigTest
*/
public function setAffiliateLogo($affiliateLogo)
{
$this->affiliateLogo = $affiliateLogo;
return $this;
}
/**
* Get affiliateLogo
*
* #return string
*/
public function getAffiliateLogo()
{
return $this->affiliateLogo;
}
/**
* Set affiliateAddress
*
* #param string $affiliateAddress
* #return WebsiteDomainConfigTest
*/
public function setAffiliateAddress($affiliateAddress)
{
$this->affiliateAddress = $affiliateAddress;
return $this;
}
/**
* Get affiliateAddress
*
* #return string
*/
public function getAffiliateAddress()
{
return $this->affiliateAddress;
}
/**
* Set status
*
* #param integer $status
* #return WebsiteDomainConfigTest
*/
public function setStatus($status)
{
$this->status = $status;
return $this;
}
/**
* Get status
*
* #return integer
*/
public function getStatus()
{
return $this->status;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set country
*
* #param \MobileSplash\SplashRequestBundle\Entity\Countries $country
* #return WebsiteDomainConfigTest
*/
public function setCountry(\MobileSplash\SplashRequestBundle\Entity\Countries $country = null)
{
$this->country = $country;
return $this;
}
/**
* Get country
*
* #return \MobileSplash\SplashRequestBundle\Entity\Countries
*/
public function getCountry()
{
return $this->country;
}
/**
* Set vendor
*
* #param \MobileSplash\SplashRequestBundle\Entity\Vendors $vendor
* #return WebsiteDomainConfigTest
*/
public function setVendor(\MobileSplash\SplashRequestBundle\Entity\Vendors $vendor = null)
{
$this->vendor = $vendor;
return $this;
}
/**
* Get vendor
*
* #return \MobileSplash\SplashRequestBundle\Entity\Vendors
*/
public function getVendor()
{
return $this->vendor;
}
}
in your annotation use namespace like this:
targetEntity="\Mj\FooBundle\Entity\Foo"

Entity class has no field or association named like in my DB

I want to create an request that recovers a number according to e.rules_id, e.status_id
public function findStat()
{
$types = $this->em
->getRepository('EthanRestBundle:EthanRules')
->createQueryBuilder('e')
->select('e.rules_id, e.status_id, COUNT(*)')
->groupBy('e.rules_id, e.status_id')
->orderBy('rules_id');
$types = $types->getQuery()->getSingleScalarResult();
return $types;
}
The error message:
[Semantical Error] line 0, col 9 near 'rules_id, e.status_id,': Error: Class Ethan\RestBundle\Entity\EthanRules has no field or association named rules_id"
I develop on Symfony 2 with FosrestBundle to create a REST Application
namespace Ethan\RestBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* EthanRules
*
* #ORM\Table(name="ethan_rules")
* #ORM\Entity
*/
class EthanRules
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="customer_key", type="string", length=45, nullable=false)
*/
private $customerKey;
/**
* #var string
*
* #ORM\Column(name="subscription_key", type="string", length=45, nullable=true)
*/
private $subscriptionKey;
/**
* #var string
*
* #ORM\Column(name="pole", type="string", length=45, nullable=false)
*/
private $pole;
/**
* #var string
*
* #ORM\Column(name="link_number", type="string", length=255, nullable=false)
*/
private $linkNumber;
/**
* #var string
*
* #ORM\Column(name="user", type="string", length=100, nullable=true)
*/
private $user;
/**
* #var \DateTime
*
* #ORM\Column(name="date_rdv", type="datetime", nullable=true)
*/
private $dateRdv;
/**
* #var \DateTime
*
* #ORM\Column(name="created_at", type="datetime", nullable=false)
*/
private $createdAt;
/**
* #var \DateTime
*
* #ORM\Column(name="updated_at", type="datetime", nullable=false)
*/
private $updatedAt;
/**
* #var \Rules
*
* #ORM\ManyToOne(targetEntity="Rules")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="rules_id", referencedColumnName="id")
* })
*/
private $rules;
/**
* #var \Status
*
* #ORM\ManyToOne(targetEntity="Status")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="status_id", referencedColumnName="id")
* })
*/
private $status;
public function _construct()
{
$this->createdAt = new \DateTime();
$this->createdAt->setTimestamp(time());
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set customerKey
*
* #param string $customerKey
* #return EthanRules
*/
public function setCustomerKey($customerKey)
{
$this->customerKey = $customerKey;
return $this;
}
/**
* Get customerKey
*
* #return string
*/
public function getCustomerKey()
{
return $this->customerKey;
}
/**
* Set subscriptionKey
*
* #param string $subscriptionKey
* #return EthanRules
*/
public function setSubscriptionKey($subscriptionKey)
{
$this->subscriptionKey = $subscriptionKey;
return $this;
}
/**
* Get subscriptionKey
*
* #return string
*/
public function getSubscriptionKey()
{
return $this->subscriptionKey;
}
/**
* Set pole
*
* #param string $pole
* #return EthanRules
*/
public function setPole($pole)
{
$this->pole = $pole;
return $this;
}
/**
* Get pole
*
* #return string
*/
public function getPole()
{
return $this->pole;
}
/**
* Set linkNumber
*
* #param string $linkNumber
* #return EthanRules
*/
public function setLinkNumber($linkNumber)
{
$this->linkNumber = $linkNumber;
return $this;
}
/**
* Get linkNumber
*
* #return string
*/
public function getLinkNumber()
{
return $this->linkNumber;
}
/**
* Set user
*
* #param string $user
* #return EthanRules
*/
public function setUser($user)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return string
*/
public function getUser()
{
return $this->user;
}
/**
* Set dateRdv
*
* #param \DateTime $dateRdv
* #return EthanRules
*/
public function setDateRdv($dateRdv)
{
$this->dateRdv = $dateRdv;
return $this;
}
/**
* Get dateRdv
*
* #return \DateTime
*/
public function getDateRdv()
{
return $this->dateRdv;
}
/**
* Set createdAt
*
* #param \DateTime $createdAt
* #return EthanRules
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* #return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set updatedAt
*
* #param \DateTime $updatedAt
* #return EthanRules
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* #return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* Set rules
*
* #param \Ethan\RestBundle\Entity\Rules $rules
* #return EthanRules
*/
public function setRules(\Ethan\RestBundle\Entity\Rules $rules = null)
{
$this->rules = $rules;
return $this;
}
/**
* Get rules
*
* #return \Ethan\RestBundle\Entity\Rules
*/
public function getRules()
{
return $this->rules;
}
/**
* Set status
*
* #param \Ethan\RestBundle\Entity\Status $status
* #return EthanRules
*/
public function setStatus(\Ethan\RestBundle\Entity\Status $status = null)
{
$this->status = $status;
return $this;
}
/**
* Get status
*
* #return \Ethan\RestBundle\Entity\Status
*/
public function getStatus()
{
return $this->status;
}
}
Since you have not defined a property $rules_id Doctrine can not select one. Instead join both entities and select their ids:
$types = $this->em
->getRepository('EthanRestBundle:EthanRules')
->createQueryBuilder('e')
->select('r.id AS rule_id, s.id AS status_id, COUNT(DISTINCT e.id)')
->leftJoin('e.rules', 'r')
->leftJoin('e.status', 's')
->groupBy('r.id, s.id')
->orderBy('r.id');
BUT: you use getSingleScalarResult() which will not work with multiple result rows!
Propably getArrayResult() will be best choice for you, since it looks like you don't want to work with objects.
Doctrine by default names all fields with camelCase in the entity classes but with_underscores in the Database.
You need to change your rules_id and status_id to rules and status accordingly.

Categories