i have an sql table called "planning" whchi contain id_place , id_event , date_event. i wrote this code with symfony2 to send the dates of an event grouped by place.
$query2 = $em->createQueryBuilder();
$query2->select("p.libPlace , p.idPlace ")
->from('AppBundle:Place', 'p')
->leftJoin('AppBundle:Planning', 'pl', 'WITH', 'pl.idPlace = p.idPlace')
->where('pl.idCevent = :id')
->setParameter('id', $idEvent)
->groupBy('p.idPlace');
$q2 = $query2->getQuery();
$places = $q2->getResult();
$taille = sizeof($places);
$j=0; $a=array();
for ($i=0; $i<= $taille-1; $i++)
{
$query4 = $em->createQueryBuilder();
$query4->select(" z.dateEventPlace dateEventPlace")
->from('AppBundle:Planning', 'z')
->where('z.idCevent = :id')
->andWhere('z.idPlace = :idp')
->setParameter('id', $idEvent)
->setParameter('idp', $places[$i]['idPlace'])
->orderBy('dateEventPlace', 'ASC');
$q4 = $query4->getQuery();
$planning = $q4->getResult();
$p[$j]=$places[$i];
$j++;
$p[$j]=$planning;
$j++;
}return array('places' => $p);
i got this json response :
places":[
{"libPlace":"Le Colis\u00e9e","idPlace":1},
[{"dateEventPlace":"2017-03-19T08:09:00+0000"},{"dateEventPlace":"2017-03-19T14:19:00+0000"},{"dateEventPlace":"2017-03-24T14:08:00+0000"}],
{"libPlace":"ABC","idPlace":2},
[{"dateEventPlace":"2017-03-22T14:10:00+0000"},{"dateEventPlace":"2017-03-24T16:20:00+0000"}]
]
and i want to get something like this:
places":[
{
"libPlace":"Le Colis\u00e9e","idPlace":1,"times":[
{"dateEventPlace":"2017-03-19T08:09:00+0000"},{"dateEventPlace":"2017-03-19T14:19:00+0000"}]
},
{
"libPlace":"ABC","idPlace":2,"times":[
{"dateEventPlace":"2017-03-22T14:10:00+0000"}]
}
]
any solution pls??
Related
I'm working on a server that uses doctrine orm, and i came across the following function:
public function showForJustWhoCanSee() {
$subquery = $this->em->createQueryBuilder();
$subquery->select('userSignature')
->from('HospitalApi\Entity\EletronicDocumentSignature', 'signature')
->innerJoin('HospitalApi\Entity\User', 'userSignature', 'WITH', 'userSignature = signature.user')
//->where('signature.signed = 0')
->groupBy('signature._document')
->orderBy('signature.order', 'ASC');
$select = $this->em->createQueryBuilder();
$select->select('ed')
->from($this->getEntityPath(), 'ed')
->innerJoin("HospitalApi\Entity\User", "u", "with", "ed.user = u")
->leftJoin("HospitalApi\Entity\EletronicDocumentSignature", 'eds', 'WITH', 'eds._document = ed')
->leftJoin("eds.user", 'us', 'WITH', 'u = :user OR us = :user')
->where( $select->expr()->eq( 'us', $select->expr()->any( $subquery->getDQL() )) )
// ->andwhere('eds.signed = 0')
// ->andwhere( $select->expr()->in('ed.status', $this->_alowedStatusToSign) )
->orwhere('u = :user')
->setParameter('user', $this->getSession() )
->andWhere('ed.c_removed = 0');
return $select;
}
I would like to know what the colon on ':user' does at:
->leftJoin("eds.user", 'us', 'WITH', 'u = :user OR us = :user')
Thanks in advance.
The colon is used in parameter binding... you define a name: :user
And then you assign a value to that parameter in:
->setParameter('user', $this->getSession() )
That's my problem:
I'm trying to make a sub select inside a select with doctrine but already told me that i have bounded too few parameters.
That's my code:
//eseguo un group by per capire quali diciture mostrare nel select
$repoMappatura = $this->getDoctrine()->getRepository('AppBundle:CombinazioneAlberoMappaCategorieArticoli');
$qb = $repoMappatura->createQueryBuilder('combinazioneAlberoMappaCategorieArticoli')
->leftJoin('combinazioneAlberoMappaCategorieArticoli.albero', 'albero')
->select('combinazioneAlberoMappaCategorieArticoli.valore')
->where('combinazioneAlberoMappaCategorieArticoli.albero = :albe')
->setParameter('albe', $alberoFiglio);
$count = 894;
/** #var $vincolo VincoliControlloAlberiFigliConfiguratore[]*/
foreach ($alberoFiglio->getVincoli() as $vincolo)
{
if (key_exists($vincolo->getAlberoVincolo()->getId(), $arrayChiaviVincoli)) {
$log->info('Esistente in array! Valore: ' . $arrayChiaviVincoli[$vincolo->getAlberoVincolo()->getId()]);
$qb2 = $repoMappatura->createQueryBuilder('qb2Mappa');
$qb = $qb->andWhere(
$qb->expr()->in('combinazioneAlberoMappaCategorieArticoli.id',
$qb2->select('qb2Mappa.id')
->where('qb2Mappa.valore = :val' . $count)
->andWhere('qb2Mappa.albero = :alb')
->setParameters(['val' . $count => $arrayChiaviVincoli[$vincolo->getAlberoVincolo()->getId()], 'alb' . $count => $alberoFiglio])
->getDQL()
)
);
$count++;
}
}
$qb = $qb->groupBy('combinazioneAlberoMappaCategorieArticoli.valore')
->getQuery()->getArrayResult();
That is the returned error by symfony:
Too few parameters: the query defines 3 parameters but you only bound 1
I have also tried to use the
->setParameters(....)
to $qb and not to $qb2 but the result is the same
Too few parameters: the query defines 3 parameters but you only bound 2
I have solved my problem with this code:
$repoMappatura = $this->getDoctrine()->getRepository('AppBundle:CombinazioneAlberoMappaCategorieArticoli');
$qb = $repoMappatura->createQueryBuilder('combinazioneAlberoMappaCategorieArticoli')
->leftJoin('combinazioneAlberoMappaCategorieArticoli.albero', 'albero')
->leftJoin('combinazioneAlberoMappaCategorieArticoli.mappaCategorieArticoli', 'mappaCategorieArticoli')
->select('combinazioneAlberoMappaCategorieArticoli.valore')
->where('combinazioneAlberoMappaCategorieArticoli.albero = ?1')
->setParameter(1, $alberoFiglio);
$count = 2;
/** #var $vincolo VincoliControlloAlberiFigliConfiguratore*/
foreach ($alberoFiglio->getVincoli() as $vincolo)
{
if (key_exists($vincolo->getAlberoVincolo()->getId(), $arrayChiaviVincoli)) {
$log->info('Esistente in array! Valore: ' . $arrayChiaviVincoli[$vincolo->getAlberoVincolo()->getId()]);
$qb2 = $this->getDoctrine()->getRepository('AppBundle:CombinazioneAlberoMappaCategorieArticoli')->createQueryBuilder('qb2Mappa'.$count)
->leftJoin('qb2Mappa'.$count.'.albero', 'alber'.$count)
->leftJoin('qb2Mappa'.$count.'.mappaCategorieArticoli', 'mpc'.$count)
->select('mpc'.$count.'.id')
->where('qb2Mappa'.$count.'.valore = ?' . $count)
->andWhere('alber'.$count.'.id = ?' . ($count + 1));
$qb = $qb->andWhere(
$qb->expr()->in('mappaCategorieArticoli.id',$qb2->getDQL())
)->setParameter($count, $arrayChiaviVincoli[$vincolo->getAlberoVincolo()->getId()])
->setParameter($count + 1, $vincolo->getAlberoVincolo()->getId());
}
$count = $count + 2;
}
$log->info($qb->getQuery()->getSQL());
$log->info(count($qb->getParameters()));
$qb = $qb->groupBy('combinazioneAlberoMappaCategorieArticoli.valore')
->getQuery()->getArrayResult();
Added count parameter to make each for loop different and add manually parameters with ? . $count
I have the following function, the params are "asc,PV1"
public function get_products($order, $sku){
if(is_array($result) && $result['active']){
$id_emp = $result['id_emp'];
$mts = 'module_tienda_status';
$mtc = 'module_tienda_categories';
$mtp = 'module_tienda_productos';
$mtpt = 'module_tienda_product_type';
$mtpa = 'module_tienda_product_attribute';
$mta = 'module_tienda_attributes';
$mtao = 'module_tienda_attribute_option';
$mtpv = 'module_tienda_product_variation';
$mtpvo = 'module_tienda_product_variation_options';
$get = array(
$mtp.'.id',
$mtp.'.name',
$mtp.'.sku',
$mtp.'.desc_short',
$mtp.'.desc_long',
$mtp.'.id_category',
$mtp.'.price',
$mtp.'.id_status',
$mtp.'.inventory',
$mtp.'.imgs',
$mtp.'.qty',
$mtp.'.featured',
$mtc.'.name as cat_name',
$mtpt.'.id as type_id',
$mtpa.'.id_attribute',
$mtpa.'.id_option',
$mta.'.name as attr_name',
$mtao.'.option as opt_name',
$mtpv.'.inventory as var_inv',
$mtpv.'.qty as var_qty',
$mtpv.'.price as var_price',
$mtpvo.'.id_product_variation as var_id',
$mtpvo.'.id_attribute as var_attr',
$mtpvo.'.id_option as var_opt'
);
$this->db->select($get);
$this->db->from($mts);
$this->db->join($mtp, $mtp.'.id_status = '.$mts.'.id');
$this->db->join($mtc, $mtp.'.id_category = '.$mtc.'.id', 'left');
$this->db->join($mtpt, $mtp.'.id_product_type = '.$mtpt.'.id');
$this->db->join($mtpa, $mtpa.'.id_product = '.$mtp.'.id', 'left');
$this->db->join($mta, $mta.'.id = '.$mtpa.'.id_attribute', 'left');
$this->db->join($mtao, $mtao.'.id = '.$mtpa.'.id_option', 'left');
$this->db->join($mtpv, $mtp.'.id = '.$mtpv.'.id_product', 'left');
$this->db->join($mtpvo, $mtpv.'.id = '.$mtpvo.'.id_product_variation', 'left');
$this->db->where($mtp.'.id_emp', $id_emp);
if($sku != 'null'){
$this->db->where($mtp.'.sku', $sku);
}
if(!is_null($order)){
$this->db->order_by('module_tienda_productos.created', $order);
}
$query = $this->db->get()->result();
}else{
return $result;
}
}
the result is an array with 49 rows of the same product, the difference is that there are different attributes and variations, here is the example https://pastebin.com/3X4w6wEi
What i want is 1 single result with an array of attributes and 1 array of variations something like this (is a json, please decode it)
[{'id':'343','name':'Producto variable 1','sku':'PV1','desc_short':'<p><br></p>','desc_long':'<p><br></p>','id_category':null,'price':null,'id_status':'1','inventory':'0','imgs':null,'qty':'0','featured':'0','cat_name':null,'type_id':'2','attributes':[{'id_attribute':'49','attr_name':'Colors','opts':{'107':'Amarillo','110':'Celeste','121':'Rojo','122':'Azul'}},{'id_attribute':'57','attr_name':'Size','opts':{'112':'xs','113':'s','114':'n','116':'xl'}}],'variations':[{'var_id':'42','var_inv':'0','var_qty':'0','var_price':'0.00','opts':[{'id_attribute':'49','attr_name':'Colors','opts':{'107':'Amarillo','110':'Celeste','121':'Rojo','122':'Azul'}},{'id_attribute':'57','attr_name':'Size','opts':{'112':'xs'}}]}]}]
I want to count all fields that fits my conditions and get them page by page with doctrine query builder.
I'm generating the query depends my filter fields.
First section is counting the records so i can calculate the pages.
$qb = $em->createQueryBuilder();
$qb
->select('COUNT(m.id)')
->from('CSMediaBundle:MediaItem', 'm')
->where(
$qb->expr()->eq('m.media', $media->getId())
);
$filters = $request->request->get('filter');
if(!empty($filters['size'])) {
foreach($filters['size'] as $key => $value) {
if(!empty($value)) {
$qb->andWhere(
$qb->expr()->eq('m.'.$key, ':'.$key)
)->setParameter($key, $value);
}
}
}
if(!empty($filters['sliders'])) {
$qb
->leftJoin('CSSliderBundle:SliderItem', 's', 'ON', 'm.id = s.media_id')
->andWhere(
$qb->expr()->in('s.sliders', $filters['sliders'])
);
}
$media_count = $qb->getQuery()->getSingleScalarResult();
Second section is getting records by calculated page using same filters, just changing the select and final parts (getSingleScalarResult to getResult)
I wonder if is there any way to just change the select and the result parts so i would not use the filters again and again...
Yeah, that's what functions are for:
function filter($qb, $filters) {
if (!empty($filters['size'])) {
foreach($filters['size'] as $key => $value) {
if (!empty($value)) {
$qb->andWhere(
$qb->expr()->eq('m.'.$key, ':'.$key)
)->setParameter($key, $value);
}
}
}
if (!empty($filters['sliders'])) {
$qb
->leftJoin('CSSliderBundle:SliderItem', 's', 'ON', 'm.id = s.media_id')
->andWhere(
$qb->expr()->in('s.sliders', $filters['sliders'])
);
}
return $qb;
}
$filters = $request->request->get('filter');
// count
$qb = $em->createQueryBuilder();
$qb
->select('COUNT(m.id)')
->from('CSMediaBundle:MediaItem', 'm')
->where(
$qb->expr()->eq('m.media', $media->getId())
);
$media_count = filter($qb, $filters)->getQuery()->getSingleScalarResult();
// entities
$qb = $em->createQueryBuilder();
$qb
->select('m')
->from('CSMediaBundle:MediaItem', 'm')
->where(
$qb->expr()->eq('m.media', $media->getId())
);
$media_entities = filter($qb, $filters)->getQuery()->getResult();
Another way is to clone the query builder object:
$qb = $em->createQueryBuilder();
$qb->from('CSMediaBundle:MediaItem', 'm')
->where(
$qb->expr()->eq('m.media', $media->getId())
);
$filters = $request->request->get('filter');
if (!empty($filters['size'])) {
foreach($filters['size'] as $key => $value) {
if (!empty($value)) {
$qb->andWhere(
$qb->expr()->eq('m.'.$key, ':'.$key)
)->setParameter($key, $value);
}
}
}
if (!empty($filters['sliders'])) {
$qb
->leftJoin('CSSliderBundle:SliderItem', 's', 'ON', 'm.id = s.media_id')
->andWhere(
$qb->expr()->in('s.sliders', $filters['sliders'])
);
}
$qb2 = clone $qb;
$qb->select('COUNT(m.id)')
$media_count = $qb->getQuery()->getSingleScalarResult();
$qb2->select('m')
$media_entities = $qb2->getQuery()->getResult();
In Javascript-TypeORM you can clone the existing query builder as shown below.
let baseQuery=this.Repository.createQueryBuilder("user");
//Add conditions
if(user_type==="admin"){
baseQuery.where("user.user_type = : user_type",{user_type:"admin"})
}
//Clone query builder
let count_query= new SelectQueryBuilder(baseQuery);
const count= count_query.select('COUNT(*)', 'count').getRawOne();
let sum_query= new SelectQueryBuilder(baseQuery);
const sum= sum_query.select('SUM(user.amount)', 'amount').getRawOne();
I have made a symfony form and I used:
$conn->insert('Invoiceshasitems', array('Invoiceitemsid' => '$items'));
for data insertion but it not works ho i do insert data from symfony controller.
here is my code:
$itemscounter = $request->request->get('itemscounter');
if(isset($_SESSION['invoiceid'])) {
$invoiceid=$_SESSION['invoiceid'];
//echo $invoiceid;
//exit;
}
//$entity= new Invoiceshasitems();
if($request->isXmlHttpRequest()) {
if($itemscounter > 1){
for($i=1; $i<=$itemscounter; $i++){
if($i==1){
$items = $_POST['items'];
}else {
$items.$i = $_POST['items'.$i];
}
}
}else{
$items = $_POST['items'];
$conn->insert('Invoiceshasitems', array('Invoiceitemsid' => '$items'));
}
}
You can use native query in Doctrine, there is some docs here: http://docs.doctrine-project.org/en/latest/reference/native-sql.html
For your example the code is something like that:
$rsm = new ResultSetMapping();
$query = $this->_em->createNativeQuery('INSERT INTO Invoiceshasitems SET Invoiceitemsid = ?', $rsm);
$query->setParameter(1, $items);
$result = $query->getResult();