yi_peppe
(Peppedantini)
April 24, 2013, 11:05am
1
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
Keith
(Kburton)
April 24, 2013, 11:11am
2
if (cds_active) {
If cds_active is a variable, you need the $ at the front of it.
yi_peppe
(Peppedantini)
April 24, 2013, 11:22am
3
Sure, it’s just a rewriting mistake, but it’s not the reason of the problem. I’ve tested the code without it too.
Keith
(Kburton)
April 24, 2013, 11:45am
4
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',
),
),
),
bettor
(Live Webscore)
April 24, 2013, 11:58am
5
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) {
yi_peppe
(Peppedantini)
April 24, 2013, 2:20pm
6
Keith:
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',
),
),
),
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
Keith
(Kburton)
April 24, 2013, 2:37pm
7
Try building the command using the array functionality instead of as a string.
Docs here.
yi_peppe
(Peppedantini)
April 24, 2013, 4:04pm
8
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.
yi_peppe
(Peppedantini)
April 29, 2013, 6:47am
9
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?