db icon indicating copy to clipboard operation
db copied to clipboard

How to set a PDO statement attribute?

Open aliechti opened this issue 8 years ago • 12 comments

I have to use the each method to select in batches. The problem is, that I have to delete some columns in this foreach, which generates a table lock. Now I have to set the cursor PDO::ATTR_CURSOR to forward only PDO::CURSOR_FWDONLY. Is this even possible by now?

$simpleQuery = SimpleObject::find();
// need something like: $simpleQuery->setPdoStmtAttr(\PDO::ATTR_CURSOR, \PDO::CURSOR_FWDONLY);
foreach ($simpleQuery->each() as $simpleObject) {
    // fancy things...
}

Funding

  • You can sponsor this specific effort via a Polar.sh pledge below
  • We receive the pledge once the issue is completed & verified
Fund with Polar

aliechti avatar Oct 20 '15 08:10 aliechti

Yes. You can access PDO instance via connection class.

samdark avatar Oct 20 '15 08:10 samdark

Yes but the PDO::ATTR_CURSOR attribute must be set on the pdo statement object.

Exception 'PDOException' with message 'SQLSTATE[IMSSP]: The given attribute is only supported on the PDOStatement object.'

aliechti avatar Oct 20 '15 08:10 aliechti

Command::$pdoStatement is public.

samdark avatar Oct 20 '15 08:10 samdark

And how do I use it in my snipped?

aliechti avatar Oct 20 '15 08:10 aliechti

Same as you'd use it with PDO.

samdark avatar Oct 20 '15 08:10 samdark

So it is not possible with the each statement?

aliechti avatar Oct 20 '15 08:10 aliechti

Currently not.

samdark avatar Oct 20 '15 09:10 samdark

Hmm I just looked into it, but I don't know where I should put the change in.

1.Loop the Attributes through the Query

$query->setPdoAttributes([\PDO::ATTR_CURSOR => \PDO::CURSOR_FWDONLY])->each();

2.Make the method Query::createCommand return the same Command for every call, save it in a private/protected variable and just return it if called twice.

$query->command->setPdoAttributes([\PDO::ATTR_CURSOR => \PDO::CURSOR_FWDONLY]);
$query->each();

aliechti avatar Oct 20 '15 11:10 aliechti

#1 looks better.

samdark avatar Oct 20 '15 11:10 samdark

https://github.com/yiisoft/yii2/pull/9973

samdark avatar Apr 23 '19 21:04 samdark

yiisoft/yii2#9973

We implement this change?

terabytesoftw avatar Mar 05 '20 17:03 terabytesoftw

Yes. That's useful.

samdark avatar Mar 12 '20 15:03 samdark