Doese Andwhere Ar Method Runs?

Hi

I’ve a problem with this code:


$dbConn = new CDbConnection($dbRif, 'user', 'pass');

$dbConn->active = true;

$comando = $dbConn->createCommand('SELECT Count(*) FROM carriera WHERE matr = 1 AND aa_iscr = :anno');

$comando->bindValue(':anno', $analisi->anno);

if (cds_active) {

    $comando->andWhere('cds_id=1026');

}

$tab = $comando->queryRow(false);

$risultato = $tab[0];

All runs correctly but the “andWhere”; this row appears to be ignored. What’s wrong?

thanks


if (cds_active) {

If cds_active is a variable, you need the $ at the front of it.

Sure, it’s just a rewriting mistake, but it’s not the reason of the problem. I’ve tested the code without it too.

Try turning on web page logging and check the query that’s being executed. It should help you to narrow the problem down.




		'log'=>array(

			'class'=>'CLogRouter',

			'routes'=>array(

				array(

					'class'=>'CFileLogRoute',

					'levels'=>'error, warning',

				),

				array(

					'class'=>'CWebLogRoute',

				),

			),

		),



I am sure you have noticed this but I would like to mention it anyway:

This is available only since the very latest version of the framework 1.1.13!!!

Other ideas could be to activate db profiling so you can see what is the actual query being executed and double check


if (cds_active) {

The log returns the query exactly as written in the CreateCommand row. The "andWhere" additional condition was not inserted.

More, I’ve inserted the code


echo $comando->where;

before and after the andWhere row. The first echo returns null string, the second "cds_id=1026".

The version of Yii is 1.1.13

Try building the command using the array functionality instead of as a string.

Docs here.

I can’t use this method because I read the SQL string from a db table.

However, trying with this code


$comando = $dbConn->createCommand()->select('Count(*)')

->from('carriera')

->where('matr = 1 AND aa_iscr = :anno', array(':anno' => $analisi->anno));

the echo command for "$comando->where" says


matr = 1 AND aa_iscr = :anno

… then, after the "andWhere" row, says


(matr = 1 AND aa_iscr = :anno) AND (cds_id=1026)

and it seems ok!

But…

when executing the query it returns an error


CDbCommand ha riportato un errore nell'esecuzione della query SQL: SQLSTATE[HY000]: General error: 942 OCIStmtExecute: ORA-00942: tabella o vista inesistente

(ext\pdo_oci\oci_statement.c:148). The SQL statement executed was: SELECT Count(*)

FROM "carriera"

WHERE matr = 1 AND aa_iscr = :anno

I can’t understand why.

Does anyone has ideas on how to solve the problem?