Doctrine error select in select subquery - php

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

Related

How can i return an array in php function

I need a return array in function. When i was use that returning this Array ( ):
function menuOlustur($ana_kategoriler){
global $db;
$alt_kategori_durum = '';
$alt_kategori;
foreach ($ana_kategoriler as $kategori) {
$alt_kategori = $db->rawQuery('select * from s_kategoriler where kategori = ' . $kategori['id'] . ' and durum=1 order by sira asc');
}
return $alt_kategori;
}
and i was use this returning one more index in array.
function menuOlustur($ana_kategoriler){
global $db;
$alt_kategori_durum = '';
$alt_kategori;
foreach ($ana_kategoriler as $kategori) {
$alt_kategori[] = $db->rawQuery('select * from s_kategoriler where kategori = ' . $kategori['id'] . ' and durum=1 order by sira asc');
}
return $alt_kategori;
}
My $ana_kategoriler is an array its a calling query like that
$ana_k_cek = $db->rawQuery('select id, kategori, adi_' . $dil . ' as adi,link_' . $dil . ' as link from s_kategoriler where m_id=5 and durum=1 order by sira asc ');
whats can i do ?
EDIT: fixed query, should not be kategori = 1,2,3,4, but kategory IN(1,2,3,4)
I suggest to change your code to:
function menuOlustur($ana_kategoriler){
global $db;
/* first - collect all ids */
$ids = array();
foreach ($ana_kategoriler as $kategori) {
$ids[] = $kategori['id'];
}
/* return empty array if there is no ids, or return result of single query matching all results with given ids */
return count($ids) > 0 ? $db->rawQuery('select * from s_kategoriler where kategori IN( ' . implode(",",$ids) . ') AND durum=1 order by sira asc') : array();
}

send a Nested array json from symfony2 to angular2

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??

Multiple variables in prepared statement PHP

I'm having an issue preparing a SQL statement:
$statement = $conexion->prepare(
'SELECT * FROM celulares
WHERE (MARCA = :marca )
AND
(CATEGORIA = :categoria1 OR CATEGORIA = :categoria2 OR CATEGORIA = :categoria3)
AND
(CATEGORIA2 = :categoria1 OR CATEGORIA2 = :categoria2 OR CATEGORIA2= :categoria3)
AND
(CATEGORIA3 = :categoria1 OR CATEGORIA3 = :categoria2 OR CATEGORIA3 = :categoria3)');
Giving placeholders values with this:
$statement->execute(array(':categoria1' => $categoria1,
':categoria2' => $categoria2,
':categoria3' => $categoria3,
':marca' => $query
));
$query value may variate when my application begins depending on some results:
if ($entrada == "LG") {
if ($query == "") {
$query = "LG";
} else {
$query = $query . ' OR MARCA = "LG" ';
}
}
if ($entrada == "APPLE") {
if ($query == "") {
$query = "APPLE";
} else {
$query = $query . ' OR MARCA = "APPLE" ';
}
}
if ($entrada == "HUAWEI") {
if ($query == "") {
$query = "HUAWEI";
} else {
$query = $query . ' OR MARCA = "HUAWEI" ';
}
}
I tried a lot of things, but none of those worked out it returns an empty array, the only one who works was changing this line of my prepared statement:
WHERE (MARCA = :marca OR MARCA = :marca2 OR MARCA = :marca3 )
And as many "MARCA" as results, i think it's not the best way to do it
UPDATED:
Now trying with IN Statement in my Query (Thanks you all for helping me)
Now it looks like:
$marcas = array("LG", "HUAWEI"); (Static values for test)
$inQuery = implode(',', array_fill(0, count($marcas), '?'));
$statement = $conexion->prepare(
'SELECT * FROM celulares
WHERE (MARCA = IN (' . $inQuery . '))
AND
(CATEGORIA = :categoria1 OR CATEGORIA = :categoria2 OR CATEGORIA = :categoria3)
AND
(CATEGORIA2 = :categoria1 OR CATEGORIA2 = :categoria2 OR CATEGORIA2= :categoria3)
AND
(CATEGORIA3 = :categoria1 OR CATEGORIA3 = :categoria2 OR CATEGORIA3 = :categoria3)');
foreach ($marcas as $k => $marca) {
$statement->bindValue(($k+1), $marca);
}
$statement->bindValue(':categoria1', $categoria1);
$statement->bindValue(':categoria2', $categoria2);
$statement->bindValue(':categoria3', $categoria3);
$statement->execute();
Getting: Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: mixed named and positional parameters
Trying to fix it
You can simplify your query:
SELECT * FROM celulares
WHERE (MARCA = :marca )
AND (:categoria1,:categoria2,:categoria3)
IN (
(CATEGORIA,CATEGORIA2,CATEGORIA3),
(CATEGORIA,CATEGORIA3,CATEGORIA2),
(CATEGORIA2,CATEGORIA,CATEGORIA3),
(CATEGORIA2,CATEGORIA3,CATEGORIA),
(CATEGORIA3,CATEGORIA,CATEGORIA2),
(CATEGORIA3,CATEGORIA2,CATEGORIA)
)
This way you only pass in the categories once, and compare it against the six possible permutations of three categories.
That being said, this is a sign that your database is in very poor shape. Generally speaking having any kind of "column2", "column3" system is a sign that you need to restructure your database - the kind of queries you end up with, like the above, are only going to get worse.
Specifically, in this case, just adding CATEGORIEA4 would increase the amount of permutations you need to define from 6 to 24!!
EDIT: I completely missed the part about :marca and IN - I was too focussed on the bad state of the database with regard to categories, sorry!
Well, i fix it, probably it's not the best way to solve it but i have this now:
I fill array with entries from POST
$query = array();
$index = 0;
foreach ($_POST as $entrada) {
switch($entrada) {
case "SAMSUNG":
$query[] = "SAMSUNG";
break;
case "LG":
$query[] = "LG";
break;
case "APPLE":
$query[] = "APPLE";
break;
case "HUAWEI":
$query[] = "HUAWEI";
break;
}
}
$inQuery = str_repeat('?,', count($query) - 1) . '?';
Here's my new query: Problem was that i was mixing "?" with placeholders (:) which not is recommended
$statement = $conexion->prepare(
"SELECT * FROM celulares
WHERE ( MARCA IN($inQuery))
AND
(CATEGORIA = ? OR CATEGORIA = ? OR CATEGORIA = ?)
AND
(CATEGORIA2 = ? OR CATEGORIA2 = ? OR CATEGORIA2= ?)
AND
(CATEGORIA3 = ? OR CATEGORIA3 = ? OR CATEGORIA3 = ?)");
Then i bindValues like that
$c = 0;
foreach ($query as $q => $queries) {
$c++;
$statement->bindValue(($q+1), $queries);
}
$statement->bindValue($c+1, $categoria1);
$statement->bindValue($c+2, $categoria2);
$statement->bindValue($c+3, $categoria3);
$statement->bindValue($c+4, $categoria1);
$statement->bindValue($c+5, $categoria2);
$statement->bindValue($c+6, $categoria3);
$statement->bindValue($c+7, $categoria1);
$statement->bindValue($c+8, $categoria2);
$statement->bindValue($c+9, $categoria3);
$statement->execute();
$resultados = $statement->fetchAll();
I did many test with a lot of querys and it's working fine, probably it's a "dirty" solution but i'll continue learning
Thanks u all for helping me!

CakePHP custom query paginator: custom paginate function is not called

I build a custom query and tried use the default paginator, like this:
WodsController.php
$userId = $this->Auth->user('id');
$connection = ConnectionManager::get('default');
$result = $connection->execute("SELECT wods.id, wods.titulo , wods.dia , wods.tempo, wods.repeticoes ,userwods.user_id FROM wods
LEFT JOIN userwods ON userwods.wod_id = wods.id WHERE userwods.user_id is null or userwods.user_id=4 order by wods.dia desc limit 50")->fetchAll('assoc');
$results = array();
foreach ($result as $r) {
$entity = $this->Wods->newEntity($r);
array_push($results, $entity);
}
$wods = $this->paginate($results);
$this->set('_serialize', ['wods']);
I got this error "Unable to locate an object compatible with paginate".
Now I'm tryng implement custom query paginator, but it's not working.
I implemented paginate and paginateCount functions in the model.
Wods.php file:
public function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null, $extra = array()) {
$recursive = -1;
$this->useTable = false;
$sql = '';
$sql .= "SELECT wods.id, wods.titulo , wods.dia , wods.tempo, wods.repeticoes ,userwods.user_id FROM wods LEFT JOIN userwods ON userwods.wod_id = wods.id WHERE userwods.user_id is null or userwods.user_id=4 order by wods.dia desc limit ";
// Adding LIMIT Clause
$sql .= (($page - 1) * $limit) . ', ' . $limit;
$results = $this->query($sql);
return $results;
}
public function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
$sql = '';
$sql .= "SELECT wods.id, wods.titulo , wods.dia , wods.tempo, wods.repeticoes ,userwods.user_id FROM wods LEFT JOIN userwods ON userwods.wod_id = wods.id WHERE userwods.user_id is null or userwods.user_id=4 order by wods.dia desc";
$this->recursive = $recursive;
$results = $this->query($sql);
return count($results);
}
In the controller WodsController.php
public function index()
{
$this->Wods->recursive = 0;
$this->paginate = array('Wods'=>array('limit'=>10));
$this->set('wods', $this->paginate('Wods'));
}
But the custom paginator is not called, it continues calling the default paginate function. Why ?
Following dragmosh advise (thanks), I investigate CakePHP ORM custom queries builder.
In this solution I used find() function with specific options, after I called the default paginator:
$query = $this->Wods->find()
->select(['Wods.id', 'Wods.titulo','Wods.dia','Wods.rounds','Wods.tempo','Wods.repeticoes','Userwods.user_id'])
->join([
'table' => 'Userwods',
'alias' => 'Userwods',
'type' => 'LEFT',
'conditions' => 'Userwods.wod_id = Wods.id',
])
->where(function ($exp, $q) {
return $exp->isNull('Userwods.user_id');})
->orWhere(['Userwods.user_id' => 4])
->contain(['Userwods'])
->autoFields(true);
$wods = $this->paginate($query);
$this->set(compact('wods'));
$this->set('_serialize', ['wods']);

Laravel 5 Pagination with Query Builder

I'm making a Laravel Pagination based from my query result and be rendered in my view. I'm following this guide http://laravel.com/docs/5.1/pagination but I get an error:
Call to a member function paginate() on a non-object
I'm using query builder so I think that should be ok? Here's my code
public function getDeliveries($date_from, $date_to)
{
$query = "Select order_confirmation.oc_number as oc,
order_confirmation.count as cnt,
order_confirmation.status as stat,
order_confirmation.po_number as pon,
order_summary.date_delivered as dd,
order_summary.delivery_quantity as dq,
order_summary.is_invoiced as iin,
order_summary.filename as fn,
order_summary.invoice_number as inum,
order_summary.oc_idfk as ocidfk,
order_summary.date_invoiced as di
FROM
order_confirmation,order_summary
where order_confirmation.id = order_summary.oc_idfk";
if (isset($date_from)) {
if (!empty($date_from))
{
$query .= " and order_summary.date_delivered >= '".$date_from."'";
}
}
if (isset($date_to)) {
if (!empty($date_to))
{
$query .= " and order_summary.date_delivered <= '".$date_to."'";
}
}
$query.="order by order_confirmation.id ASC";
$data = DB::connection('qds106')->select($query)->paginate(15);
return $data;
}
However when I remove the paginate(15); it works fine.
Thanks
in the doc at this page: http://laravel.com/docs/5.1/pagination
we can see that we are not forced to use eloquent.
$users = DB::table('users')->paginate(15);
but, be sure you don't make a groupBy in your query because, the paginate method uses it.
after i'm no sure you can use paginate with query builder ( select($query) )
--- edit
You can create collection an use the paginator class :
$collection = new Collection($put_your_array_here);
// Paginate
$perPage = 10; // Item per page
$currentPage = Input::get('page') - 1; // url.com/test?page=2
$pagedData = $collection->slice($currentPage * $perPage, $perPage)->all();
$collection= Paginator::make($pagedData, count($collection), $perPage);
and in your view just use $collection->render();
public function getDeliveries($date_from, $date_to)
{
$query="your_query_here";
$deliveries = DB::select($query);
$deliveries = collect($deliveries);
$perPage = 10;
$currentPage = \Input::get('page') ?: 1;
$slice_init = ($currentPage == 1) ? 0 : (($currentPage*$perPage)-$perPage);
$pagedData = $users->slice($slice_init, $perPage)->all();
$deliveries = new LengthAwarePaginator($pagedData, count($deliveries), $perPage, $currentPage);
$deliveries ->setPath('set_your_link_page');
return $deliveries;
}
You set it by using the custom pagination..
$query = "Your Query here";
$page = 1;
$perPage = 5;
$query = DB::select($query);
$currentPage = Input::get('page', 1) - 1;
$pagedData = array_slice($query, $currentPage * $perPage, $perPage);
$query = new Paginator($pagedData, count($query), $perPage);
$query->setPath('Your Url');
$this->data['query'] = $query;
return view('Your_view_file', $this->data, compact('query'));
Here you can specify the path by using the setpath().
In your View
#foreach($query as $rev)
//Contents
#endforeach
<?php echo $Reviews->appends($_REQUEST)->render(); ?>
The appends will append the data.
Thank you.
this is the way i did, it use query builder and get the same result with pagination
$paginateNumber = 20;
$key = $this->removeAccents(strip_tags(trim($request->input('search_key', ''))));
$package_id = (int)$request->input('package_id', 0);
$movieHasTrailer = MovieTrailer::select('movie_id')->where('status','!=','-1')->distinct('movie_id')->get();
$movieIds = array();
foreach ($movieHasTrailer as $index => $value) {
$movieIds[] = $value->movie_id;
}
$keyparams = array();
$packages = Package::select('package_name','id')->get();
$whereClause = [
['movie.status', '!=', '-1'],
['movie_trailers.status', '!=', '-1']
];
if(!empty($key)){
$whereClause[] = ['movie.title', 'like', '%'.$key.'%'];
$keyparams['search_key'] = $key;
}
if($package_id !== 0){
$whereClause[] = ['movie.package_id', '=', $package_id];
$keyparams['package_id'] = $package_id;
}
$movies = DB::table('movie')
->leftJoin('movie_package','movie.package_id','=','movie_package.id')
->leftJoin('movie_trailers','movie.id','=','movie_trailers.movie_id')
->where($whereClause)
->whereIn('movie.id',$movieIds)
->select('movie.*','movie_package.package_name','movie_trailers.movie_id as movie_id',
DB::raw('count(*) as total_trailers, movie_id')
)
->groupBy('movie.id')
->paginate($paginateNumber);
If you need to hydrate, you can do this...
$pages = DB::table('stuff')
->distinct()
->paginate(24, ['stuff.id']);
$stuffs = Stuff::hydrate($pages->items());
return view('stuff.index')->with('stuffs', $stuffs)->with('pages', $pages)
$stuffs will contain your model object, $pages will contain your pagination, probably not the most efficient, but it works.
// import those Class into your Laravel Controller
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\Input;
use Illuminate\Database\Eloquent\Collection;
// In your public function
$query = DB::select(DB::raw("SELECT * FROM your_table"));
$collection = new Collection($query);
// Paginate
$perPage = 10; // Item per page
$currentPage = Input::get('page') - 1;
$pagedData = $collection->slice($currentPage * $perPage, $perPage)->all();
$pagination = new Paginator($pagedData, $perPage);
return response()->json([
$pagination
], 200);

Categories