Dinamik bir sorgu oluşturmak için işlev argümanları kullanarak

2 Cevap php

I am working on an issue management system, developed in PHP/MySQL. It requires search functionality, where the user will mention the search parameters and based on these parameters the system will return the result set.

Bunu çözmek için ben bir işlevi yazmak çalışıyorum ve tüm kullanıcı seçilen parametreler argüman olarak geçirilir. Argümanlara dayalı dinamik sorgu üretecektir. Zaman zaman (kullanıcı tarafından yapılan tercihlere göre) bu argümanların bir veya daha fazla boş olabilir.

A Sample Query:

    select * from
        tickets 
        inner join ticket_assigned_to 
            on tickets.id=ticket_assigned_to.ticket_id
    where 
        tickets.project_id= in ('')
        and tickets.status in ('')
        and ticket_assigned_to.user_id in ('')
        and tickets.reporter_user_id=''
        and tickets.operator_user_id in ('')
        and tickets.due_date between '' and ''
        and tickets.ts_created between '' and '';
    

Ben de argümanlar ORed'dir veya sorguda ANDed olabilir davalarını gerekir.

Örneğin:

 
    select * 
    from
        tickets 
        inner join ticket_assigned_to 
            on tickets.id=ticket_assigned_to.ticket_id
    where 
        tickets.project_id= in ('')
        and tickets.status in ('')
        or tickets.due_date = ''
        or tickets.ts_created between '' and '';

I am also planning to use the same function at other places in the project also. Like to display all the tickets of a user or all tickets created between given dates and so on...

How to handle this situation?
Should I go with a single function which handles all this or numerous small functions? Need guidance here.

2 Cevap

Ben, doğrudan mysql sorgu içine kullanıcıların serach sorgu iyi bir fikir olduğunu dönüştürme sanmıyorum. Temelde sadece SQL enjeksiyon karşı daha savunmasız hale gelir.

I PHP ActiveRecord gibi ve arama için bir soyutlama katmanı kullanmayı tavsiye belki Sphinx bu {[(2 üzerinden sorgulayabilirsiniz kendi gelişmiş sorgu modu, kullanıcısı var gibi tam metin arama motoru )]} veya Sam | Martha.

Ne sorgu kriterlerini oluşturmak için Specification Pattern kullanmaya ne dersiniz?

Bağlantılar Belirli PHP ama umarım size WHERE kriterlerini kadar bina yaklaşım nasıl ifade edilmez:

LINQ expression trees & the Specification pattern
C# Specification Pattern Example
Google Books result - Jason Sweat's Guide to PHP Design Patterns
Learn the Specification Pattern