Ok, quindi ho creato una libreria ORM per PHP. Utilizza la sintassi in questo modo:
* (supponiamo che $ business_locations sia un array) *
Business::type(Business:TYPE_AUTOMOTIVE)->
size(Business::SIZE_SMALL)->
left_join(BusinessOwner::table(), BusinessOwner::business_id(), SQL::OP_EQUALS, Business::id())->
left_join(Owner::table(), SQL::OP_EQUALS, Owner::id(), BusinessOwner::owner_id())->
where(Business::location_id(), SQL::in($business_locations))->
group_by(Business::id())->
select(SQL::count(BusinessOwner::id());
Che può anche essere rappresentato come:
$query = new Business();
$query->set_type(Business:TYPE_AUTOMOTIVE);
$query->set_size(Business::SIZE_SMALL);
$query->left_join(BusinessOwner::table(), BusinessOwner::business_id(), SQL::OP_EQUALS, $query->id());
$query->left_join(Owner::table(), SQL::OP_EQUALS, Owner::id(), BusinessOwner::owner_id());
$query->where(Business::location_id(), SQL::in($business_locations));
$query->group_by(Business::id());
$query->select(SQL::count(BusinessOwner::id());
Ciò produrrebbe una query come:
SELECT COUNT('business_owners'.'id')
FROM 'businesses'
LEFT JOIN 'business_owners'
ON 'business_owners'.'business_id' = 'businesses'.'id'
LEFT JOIN 'owners'
ON 'owners'.'id' = 'business_owners'.'owner_id'
WHERE 'businesses'.'type' = 'automotive'
AND 'businesses'.'size' = 'small'
AND 'businesses'.'location_id' IN ( 1, 2, 3, 4 )
GROUP BY 'businesses'.'id'
Tieni presente che la sintassi potrebbe non essere corretta in modo prefetto (l'ho solo scritta in cima alla mia testa)
- In ogni caso, cosa ne pensi di questo stile di query?
- È il primo metodo o il secondo migliore / più chiaro / più pulito / etc?
- Che cosa faresti per migliorarlo?