Complex select Retrun unexpected result


(alexsultana) #1

Hello. I try to make a complex select. I need to take data from many columns.
My code looks like this :

$fields = “d.nume_ro, cd.id_plan_disciplina, cd.id_contract, uf.id_user, cd.platit AS disc_platit,” .
" pd.sem, pd.categ AS pd_categ, cd.nota, cd.notas1, cd.notas2, cd.notas3," .
" c.grupa, c.sitScolStartAnUniv, c.taxa, c.platit1, c.platit2, u.nume, u.initiala_tatalui, u.prenume";

$sql = $db->createCommand("SELECT $fields\n" .
  "FROM contracte_discipline	cd\n" .
  "JOIN planuri_discipline		pd ON (pd.id=cd.id_plan_disciplina)\n" .
  "JOIN discipline				d  ON (d.id=pd.id_disciplina)\n" .
  "JOIN contracte				c  ON (c.id=cd.id_contract)\n" .
  (!empty($idDomeniu) || !empty($idPrgStudii)
    ?"LEFT JOIN planuri_grupuri pg ON (pg.id=pd.id_grup)\n" .
    "LEFT JOIN planuri			p  ON (p.id=pg.idPlan)\n"
    :"") .
  "JOIN users_facultati		uf ON (uf.id=c.id_user_facultate) AND (uf.cicluStudii='{$cicluStudii}')\n" .
  "JOIN users AS				u  ON (u.id=uf.id_user)\n" .
  "LEFT JOIN grupe			g  ON (g.grupa=c.grupa) AND (g.id_fac=uf.id_fac) AND (g.id_domeniu=uf.id_domeniu)\n" .
  "WHERE (pd.id_disciplina=" . (0 + $idDisciplina) . ") " . (!empty($semestru) ?" AND (pd.sem='" . (2 * ($anStudii - 1) + $semestru) . "')" :"") .
  " AND (c.an_univ='{$anUniv}') AND (c.semnat=1) AND (u.id IS NOT NULL)" .
  " AND ((c.sitScolEndAnUniv<>'exmatriculat' AND c.sitScolEndAnUniv<>'retrStudii') OR c.sitScolEndAnUniv IS NULL)" .
  (!empty($idDomeniu) ?" AND (p.id_domeniu='{$idDomeniu}')" :"") .
  (!empty($idPrgStudii) ?" AND (pg.id_specializare_ancestor IN (0,{$idPrgStudii}))" :"") .
  " AND (g.serie = '{$serie}')" .
  (!empty($grupa) ?" AND (g.grupa = '{$grupa}')" :"") . "\n" .
  "ORDER BY g.serie, c.grupa, u.nume, initiala_tatalui, prenume")->execute();

  	 return $this->render('index', ['studenti'=>$sql]);

But my return is 148, not an object .

Anyone has any idea how can I make this select
?


(tri - Tommy Riboe) #2

Query instead of execute?

https://www.yiiframework.com/doc/api/2.0/yii-db-command


(alexsultana) #3

I changed execute with queryAll. Thank You!!!