I have this entity:
class TruckOrderPud {
/**
* #ORM\Id
* #ORM\OneToOne(targetEntity="Pud")
* #ORM\JoinColumn(name="pud_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $pudId;
/**
* #ORM\Id
* #ORM\OneToOne(targetEntity="TruckOrder")
* #ORM\JoinColumn(name="truck_order_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $truckOrderId;
/**
* #ORM\Column(name="package_count", type="integer", options={"unsigned"=true}, nullable=true)
*/
protected $packageCount;
/**
* #ORM\Column(name="package_weight", type="integer", options={"unsigned"=true}, nullable=true)
*/
protected $packageWeight;
}
I am needing get only single column pudId from this entity by truckOrderId parameter.
I am try:
$result = $this->_em->createQuery('SELECT top.pudId FROM AppTruckingBundle:TruckOrderPud top WHERE top.truckOrderId = ?1')
->setParameter(1, $truckOrderId)
->getSQL();`
Then I have error:
"[Semantical Error] line 0, col 11 near 'pudId FROM AppTruckingBundle:TruckOrderPud': Error: Invalid PathExpression. Must be a StateFieldPathExpression."
If i change query from 'SELECT top.pudId...' on 'SELECT top...' is it all OK.
I am also try:
$qb = $this->_em->createQueryBuilder();
$qb->select('top.pudId')
->from('AppTruckingBundle:TruckOrderPud', 'top')
->where($qb->expr()->eq('top.truckOrderId', $truckOrderId));`
In this case I am getting identical error. If i change $qb->select('top.pudId') on $qb->select('top') is it all OK.
Thanks for help!
PudId is a foreign key that refers to Pud, so you have to refer to Pud. Try this
$qb->select('IDENTITY(top.pud) PudId')
->from('AppTruckingBundle:TruckOrderPud', 'top')
->where($qb->expr()->eq('top.truckOrderId', $truckOrderId));`
Try to remove the 1 from the ?1 and leave only ?:
$result = $this->_em->createQuery('SELECT top.pudId FROM AppTruckingBundle:TruckOrderPud top WHERE top.truckOrderId = ?')
->setParameter(1, $truckOrderId)
->getSQL();
Doctrine does the parameter positioning by itself (first param on the first question mark spot). Read here for details.
Related
I'm trying to make a many to many join with a Doctrine findBy()
$articles = $entityManager->getRepository(Articles::class)
->findBy(['rubriquesrubriques'=>$id],['idarticles'=>"ASC"]);
But I get
An exception occurred while executing
'SELECT t0.idarticles AS idarticles_1,
t0.thetitle AS thetitle_2, t0.theslug AS theslug_3, t0.thedescription AS
thedescription_4, t0.thedate AS thedate_5, t0.users_idusers AS users_idusers_6
FROM articles t0 WHERE
articles_has_rubriques.rubriques_idrubriques = ?
ORDER BY t0.idarticles ASC' with params ["2"]:
SQLSTATE[42S22]: Column not found: 1054 Champ
'articles_has_rubriques.rubriques_idrubriques' inconnu dans where clause
The column articles_has_rubriques.rubriques_idrubriques exists in my DB,
but I don't see the INNER JOIN !
When I make my many to many with a simple Find():
$articles = $entityManager->getRepository(Articles::class)->find($id);
The query is correct!
SELECT t0.idrubriques AS idrubriques_1,
t0.thertitle AS thertitle_2
FROM
rubriques t0
INNER JOIN articles_has_rubriques ON t0.idrubriques =
articles_has_rubriques.rubriques_idrubriques
WHERE articles_has_rubriques.articles_idarticles = ?
is it impossible to perform my many2many query with a findBy in the 4.1.6 version of Symfony???
This is my ORM relation:
In entity Rubriques.php:
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="Articles", mappedBy="rubriquesrubriques")
*/
private $articlesarticles;
In entity Articles.php
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="Rubriques", inversedBy="articlesarticles")
* #ORM\JoinTable(name="articles_has_rubriques",
* joinColumns={
* #ORM\JoinColumn(name="articles_idarticles", referencedColumnName="idarticles")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="rubriques_idrubriques", referencedColumnName="idrubriques")
* }
* )
*/
private $rubriquesrubriques;
Thank you so much
My question was stupid :
I'have an easy way to do that:
$rubriqueActu = $entityManager->getRepository(Rubriques::class)->find($id);
$articles = $rubriqueActu->getArticlesarticles();
SQL:
SELECT t0.idarticles AS idarticles_1, t0.thetitle AS thetitle_2,
t0.theslug AS theslug_3, t0.thedescription AS thedescription_4,
t0.thedate AS thedate_5, t0.users_idusers AS users_idusers_6
FROM articles t0 INNER JOIN articles_has_rubriques
ON t0.idarticles = articles_has_rubriques.articles_idarticles
WHERE articles_has_rubriques.rubriques_idrubriques = ?
Let's say I have two entities Bus and People with a relation OneToMany between them.
Bus can hold a maximum of 10 persons.
How to create a constraint to control this?
For example:
* #MyAssert\ParentMaxChild(max=10)
* #ORM\ManyToOne(targetEntity="Webface\CharacterBundle\Entity\Bus", inversedBy="wac")
* #ORM\JoinColumn(name="bus_id", referencedColumnName="id", nullable=false)
private $bus;
Use the Count constraint.
In your Bus class, add the constraint in the Person annotation:
/**
* ... Rest of the annotation ...
* #Assert\Count(
* max = "10",
* maxMessage = "Bus can hold a maximum of 10 persons."
* )
*/
protected $persons;
Note that you can specify a min parameter and the according message.
If I do this:
->select('a')
->from('AppBundle:Accomodation', 'a')
->innerJoin('AppBundle:AccomodationRoom', 'ap', Join::WITH, $qb->expr()->eq('ap.accomodation', 'a.id'))
->getQuery()->getResult();
all the fields in Accomodation are selected.
But if I add this 'ap' to the select, as below, it doesn't select the Accomodation fields:
->select('a', 'ap')
->from('AppBundle:Accomodation', 'a')
->innerJoin('AppBundle:AccomodationRoom', 'ap', Join::WITH, $qb->expr()->eq('ap.accomodation', 'a.id'))
->getQuery()->getResult();
The relation between entities. This is Accomodation:
/**
* #ORM\OneToMany(targetEntity="AccomodationRoom", mappedBy="accomodation")
*/
private $rooms;
And this is AccomodationRoom:
/**
* #ORM\ManyToOne(targetEntity="Accomodation", inversedBy="rooms")
* #ORM\JoinColumn(name="accomodation_id", referencedColumnName="id")
*/
private $accomodation;
Please, any idea?
You have to select Accomodation and AccomodationRooms.
->select('a', 'ap')
->from('AppBundle:Accomodation', 'a')
->innerJoin('a.rooms', 'ap')
->getQuery()->getResult();
I have database table with 7 rows, I'am trying to fetch those rows with findAll function
$machine_current_counter_repo = $this->getDoctrine()->getRepository('DummyMonitorBundle:MachineCurrentCounter');
$counters = $machine_current_counter_repo->findAll();
The result is 7 rows, but all rows contain data from the first row.
Here is database table entity.
And table structure:
`machine_current_counter` (
`machine_id` tinyint(3) unsigned NOT NULL,
`counter_value` int(10) unsigned NOT NULL,
`time_stamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`machine_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
What could cause this problem?
Ps. Entity is generated from database so by default first column setup was this (not sure why type was "boolean", but I changes it to integer, still that didn't solved the problem):
/**
* #var boolean
*
* #ORM\Column(name="machine_id", type="boolean")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $machineId;
#ORM\Id means values of this field MUST be unique. With the boolean type they can be unique only in 2 or less row (because boolean has only 2 values - 0 and 1).
I think you have logical mistake, and must simply change type of field to integer. Like that:
/**
* #var integer
*
* #ORM\Column(name="machine_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $machineId;
Then update your schema by ./app/console doctrine:schema:update then recreate data into table.
I am using doctrine2 with symfony2 and I am trying to perform a simple select query:
I want to run:
SELECT * FROM table WHERE status in (1, -1)
This PHP code:
$queryBuilder = $this->_em->createQueryBuilder();
$queryBuilder
->select('n')
->from('MyBundle:Table', 'n')
->where('n.status IN (1, -1)');
return $queryBuilder->getQuery()->getResult();
Gives the following exception:
[Syntax Error] line 0, col 96: Error: Expected Literal, got '-'
This is the attribute definition within the entity:
/**
* #var integer
*
* #ORM\Column(name="status", type="integer", nullable=true)
*/
private $status;
If I use only positive numbers within the in argument, it will work. The exception only happens with negative numbers.
What causes this exception?
Should do the trick :
$queryBuilder = $this->_em->createQueryBuilder();
$queryBuilder
->select('n')
->from('MyBundle:Table', 'n')
->where('n.status IN (:status)')
->setParameter('status', array(1, -1));