PoL, on 24 September 2009 - 01:31 PM, said:
I tink that this problem occurs when you do the model and the crud command...
suppose that the table is called 'leaf', well
You must do the model command this way: model Leaf
and the crud command in the same way: crud Leaf
At least this happens to me once and y discover that I did the model/crud command in diferent way:
table: leaf
model Leaf
crud leaf
Note that I do model Leaf and crud leaf.
This way the error occurs (no matter the of the table, in this case 'leaf')
But whey I did the commnad whit the proper case, all works perfect!
thanks for your reply.
Ive had that problem before too, but this is not the case im working on it, and ive tracked the problem down to the CMysqlSchema
/**
* Collects the table column metadata.
* @param CMysqlTableSchema the table metadata
* @return boolean whether the table exists in the database
*/
protected function findColumns($table)
{
$sql='SHOW COLUMNS FROM '.$table->rawName;
try
{
$columns=$this->getDbConnection()->createCommand($sql)->queryAll();
}
catch(Exception $e)
{
return false;
}
Note that if there is an exception false is returned and so no columns = no table is the error displayed but the real error is lost in the catch, so i echoed aout the Exception and the real exception is:
'CDbException' with message 'CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]:
General error: 2014 Cannot execute queries while other unbuffered
queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code
is only ever going to run against mysql, you may enable query buffering by setting the
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.' in /portal/app/www/yii/framework
/db/CDbCommand.php:318
Stack trace:
#0 /portal/app/www/yii/framework/db/CDbCommand.php(237): CDbCommand->queryInternal('fetchAll', 2)
#1 /portal/app/www/yii/framework/db/schema/mysql/CMysqlSchema.php(106): CDbCommand->queryAll()
#2 /portal/app/www/yii/framework/db/schema/mysql/CMysqlSchema.php(66): CMysqlSchema->findColumns(Object(CMysqlTableSchema))
#3 /portal/app/www/yii/framework/db/schema/CDbSchema.php(73): CMysqlSchema->createTable('leaf')
#4 /portal/app/www/yii/framework/db/ar/CActiveRecord.php(2136): CDbSchema->getTable('leaf')
#5 /portal/app/www/yii/framework/db/ar/CActiveRecord.php(634): CActiveRecordMetaData->__construct(Object(Leaf))
#6 /portal/app/www/ecards/protected/models/Leaf.php(29): CActiveRecord::model('Leaf')
#7 /portal/app/www/ecards/protected/models/CustomObjects/COCategory.php(71): Leaf::model()
#8 /portal/app/www/ecards/protected/controllers/HomeController.php(35): COCategory->getRandomEcard()
#9 /portal/app/www/yii/framework/web/actions/CInlineAction.php(32): HomeController->actionIndex()
#10 /portal/app/www/yii/framework/web/CController.php(300): CInlineAction->run()
#11 /portal/app/www/yii/framework/web/CController.php(278): CController->runAction(Object(CInlineAction))
#12 /portal/app/www/yii/framework/web/CController.php(257): CController->runActionWithFilters(Object(CInlineAction), Array)
#13 /portal/app/www/yii/framework/web/CWebApplication.php(332): CController->run('')
#14 /portal/app/www/yii/framework/web/CWebApplication.php(120): CWebApplication->runController('')
#15 /portal/app/www/yii/framework/base/CApplication.php(133): CWebApplication->processRequest()
#16 /portal/app/www/ecards/index.php(16): CApplication->run()
#17 {main}.
Im working on it right now :S i dont really know how to solve that, perharps if I shoud you my code you guy can help me out?
/**
* returns a CustomObject COEcard representing a random Ecard from this category
* @return COEcard
*/
public function getRandomEcard($limit = 1, $exclude = Array()){
$notIn = "";
if(!empty($exclude)){
$notIn = "AND l.ID NOT IN (".implode(",", array_map("collectIds", $exclude)).") ";
}
$q = "SELECT l.ID FROM NODE n
JOIN (LEAF l, NODE_LEAF nl, LEAF_RESOURCE lr, EXTRA_NODE en)
ON (l.ID = nl.LEAF_ID AND n.ID = nl.NODE_ID AND lr.LEAF_ID = l.ID AND en.NODE_ID = n.ID)
WHERE en.NODE_ID IN (SELECT id FROM Node WHERE node_id = {$this->id}) $notIn
ORDER BY RAND() LIMIT $limit;";
$conn = conn();
$command = $conn->createCommand($q);
$reader=$command->query();//PROBLEM
$cards = Array();
foreach($reader as $row){
$cards[] = Leaf::model()->findByPk($row['ID'])->getCOEcard();//here is where the the problem happens
}
return (count($cards) > 0) ? $cards : null;
}
It looks like it didnt like me hardcoding the SQL and then using a model
[SOLVED]: Well the problem was that I used $reader=$command->query();
when I should have used
$reader=$command->queryColumn();
for some reason it didnt "unbuffered" if I used just query()
So the right code is:
/**
* returns a CustomObject COEcard representing a random Ecard from this category
* @return COEcard
*/
public function getRandomEcard($limit = 1, $exclude = Array()){
$notIn = "";
if(!empty($exclude)){
$notIn = "AND l.ID NOT IN (".implode(",", array_map("collectIds", $exclude)).") ";
}
$q = "SELECT l.ID FROM NODE n
JOIN (LEAF l, NODE_LEAF nl, LEAF_RESOURCE lr, EXTRA_NODE en)
ON (l.ID = nl.LEAF_ID AND n.ID = nl.NODE_ID AND lr.LEAF_ID = l.ID AND en.NODE_ID = n.ID)
WHERE en.NODE_ID IN (SELECT id FROM Node WHERE node_id = {$this->id}) $notIn
ORDER BY RAND() LIMIT $limit;";
$conn = conn();
$command = $conn->createCommand($q);
$reader=$command->queryColumn();//Just the First Column
$cards = Array();
foreach($reader as $row){
$cards[] = Leaf::model()->findByPk($row)->getCOEcard();
}
return (count($cards) > 0) ? $cards : null;
}