Yii Framework Forum: CreateCommand()->execute() then using findAll from an AR class throws error - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

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

#1 User is offline   Vince. 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 432
  • Joined: 29-December 08
  • Location:Los Angeles,CA

Posted 13 October 2009 - 01:08 PM

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?
Vince Gabriel, Lead Developer.
Posted ImagePosted ImagePosted Image
0

#2 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 13 October 2009 - 01:14 PM

View PostVince., on 13 October 2009 - 01:08 PM, said:

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
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#3 User is offline   Vince. 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 432
  • Joined: 29-December 08
  • Location:Los Angeles,CA

Posted 13 October 2009 - 01:29 PM

Thanks, Will try that. Though hopefully Qiang will be able to take a look at it.
Vince Gabriel, Lead Developer.
Posted ImagePosted ImagePosted Image
0

#4 User is offline   Vince. 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 432
  • Joined: 29-December 08
  • Location:Los Angeles,CA

Posted 14 October 2009 - 03:05 AM

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.
Vince Gabriel, Lead Developer.
Posted ImagePosted ImagePosted Image
0

#5 User is offline   Vince. 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 432
  • Joined: 29-December 08
  • Location:Los Angeles,CA

Posted 14 October 2009 - 03:25 AM

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.
Vince Gabriel, Lead Developer.
Posted ImagePosted ImagePosted Image
0

#6 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,857
  • Joined: 04-October 08
  • Location:DC, USA

Posted 14 October 2009 - 06:50 AM

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

#7 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 14 October 2009 - 10:11 AM

View Postqiang, on 14 October 2009 - 06:50 AM, said:

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
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#8 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 14 October 2009 - 08:11 PM

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
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#9 User is offline   Vince. 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 432
  • Joined: 29-December 08
  • Location:Los Angeles,CA

Posted 15 October 2009 - 03:04 AM

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.
Vince Gabriel, Lead Developer.
Posted ImagePosted ImagePosted Image
0

#10 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,857
  • Joined: 04-October 08
  • Location:DC, USA

Posted 15 October 2009 - 10:13 AM

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

#11 User is offline   Vince. 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 432
  • Joined: 29-December 08
  • Location:Los Angeles,CA

Posted 15 October 2009 - 10:27 AM

Did you upload it or do i need to checkout ?
Vince Gabriel, Lead Developer.
Posted ImagePosted ImagePosted Image
0

#12 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,857
  • Joined: 04-October 08
  • Location:DC, USA

Posted 15 October 2009 - 10:41 AM

This is what you need: http://code.google.c...=svn1450&r=1450
0

#13 User is offline   Vince. 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 432
  • Joined: 29-December 08
  • Location:Los Angeles,CA

Posted 15 October 2009 - 11:01 AM

Yes that did it.
Vince Gabriel, Lead Developer.
Posted ImagePosted ImagePosted Image
0

#14 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 15 October 2009 - 11:39 AM

View PostVince., on 15 October 2009 - 11:01 AM, said:

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
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#15 User is offline   saulius 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 12-March 10

Posted 08 April 2010 - 06:06 AM

View Postqiang, on 15 October 2009 - 10:41 AM, said:



That didn't help me.. :/ the same exception.
1

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users