CreateCommand()->execute() then using findAll from an AR class throws error

When i try to run


Yii::app()->db->createCommand("SQL STATEMENT HERE")->execute();

Then right after i am doing:


CoreMembers::model()->updateByPk(1, array(something here....));

It throws an error that the table core_members of the AR class CoreMembers does not exists?

Any one know anything about it?

I used this workaround. Don’t know what’s causing the problem.




  Yii::app()->getDb()->createCommand($sql)->execute();

  Yii::app()->getDb()->setActive(false);



/Tommy

Thanks, Will try that. Though hopefully Qiang will be able to take a look at it.

Nope that did not work. When i do:


Yii::app()->db->createCommand("SQL STATEMENT HERE")->execute();

Yii::app()->db->setActive(false);

CoreMembers::model()->updateByPk(1, array(something here....));

I get:


CDbConnection is inactive and cannot perform any DB operations.

and when i do:


Yii::app()->db->createCommand("SQL STATEMENT HERE")->execute();

Yii::app()->db->setActive(true);

CoreMembers::model()->updateByPk(1, array(something here....));




The table "core_pfields_groups" for active record class "CoreProfileFieldGroups" cannot be found in the database.

Can’t seem to find the thing causing it.

Currently this works by doing:




Yii::app()->db->createCommand("SQL STATEMENT HERE")->execute();

Yii::app()->db->setActive(false);

Yii::app()->db->setActive(true);

CoreMembers::model()->updateByPk(1, array(something here....));

But this is a really bad approach and a bad solution. Hopefully this can be addressed to make it more efficient then closing and opening the connection.

What is the SQL statement you execute in the first line?

In my case:




$sql =

  "UPDATE `{$this->dbName}`.`Item` SET `InProgress` = 1 WHERE `DepartureTime` <= NOW();".

  "UPDATE `{$this->dbName}`.`Item` SET `Active` = 0, `InProgress` = 0  WHERE `ArrivalTime` <= NOW();"

  ;



(Performance is not an issue. Most probably I’ve also tried a single SQL statement.)

I unsuccessfully tried to find the older post mentioning an uncaught second exception might be throwed with no visible error message. IIRC something like "unbuffered queries". Perhaps relevant in this case too?

/Tommy

I checked again and it seems like my problem was caused by the two SQL statements in a row. I guess this is not the expected usage in the general case since SQL syntax may vary.

Anyway, I got this exception on the subsequent AR access unless I disactivated the connection first.




Description

The table "Item" for active record class "Item" cannot be found in the database.

Source File

/srv/www/htdocs/Yii/framework/db/ar/CActiveRecord.php(2244)

...



Yii 1.0.9 r1396

/Tommy

I personally do:


Yii::app()->db->createCommand("ALTER TABLE " . $table . " ADD {$field} {$type} {$default}")->execute();

Yii::app()->db->createCommand("OPTIMIZE TABLE " . $table)->execute();

And i get the same error as Tommy just with different table names.

I just checked in a fix into 1.0 branch. Could you try and see if the issue is gone? Thanks.

Did you upload it or do i need to checkout ?

This is what you need: http://code.google.com/p/yii/source/browse/branches/1.0/framework/db/CDbCommand.php?spec=svn1450&r=1450

Yes that did it.

Works for me too.

Strangely I don’t see an error message if I have a syntax error in the second SQL statement in a call to execute(). The statement just seemingly don’t run.

/Tommy

That didn’t help me… :confused: the same exception.