Skip to content

Instantly share code, notes, and snippets.

View Chunkford's full-sized avatar

Paul Mumford Chunkford

View GitHub Profile
@patoui
patoui / HasManyUpdator.php
Last active February 7, 2020 13:25
Utility class to perform multiple updates on a has many relationship (thanks to Glen UK on Larachat for finding bugs)
<?php
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\Eloquent\RelationNotFoundException;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class HasManyUpdater
@hest
hest / gist:8798884
Created February 4, 2014 06:08
Fast SQLAlchemy counting (avoid query.count() subquery)
def get_count(q):
count_q = q.statement.with_only_columns([func.count()]).order_by(None)
count = q.session.execute(count_q).scalar()
return count
q = session.query(TestModel).filter(...).order_by(...)
# Slow: SELECT COUNT(*) FROM (SELECT ... FROM TestModel WHERE ...) ...
print q.count()