Let me start a rough example of a Search Model and see if it is what you want. Looking at the data, my first comment is that your 'criteria' values are what the Model should deal with. Your 'config' are really for View code to deal with. There should be a separation there because, for example, the public and admin search forms can use the same model, but the admin search might use more criteria in the Model that the public search does not use.thinsoldier wrote:I'm mostly focused on just taking criteria, discarding any disallowed criteria, and generating sql from the criteria. The super long term goal is to have the querying of data availalbe through a single central object that can be used easily with minimum php code or exposed at its own REST url (probaly only responding to GET requests) for js-based sites or native apps to consume.
Code: Select all
$instructions = [ 'criteria'=>[ 'not-expired' => 1, 'not-hidden' => 1, 'application-completed' => 1, 'category' => 'commercial sale', 'island' => 420, 'property-type' => [71,23,44,11] ], 'config'=>[ 'format' => 'json', 'paginate' => 1, 'paginate-limit' => 20, 'paginate-offset' => 60 ] ]; $data = new ListingsQuery( $instructions );
Not sure what you mean "return the same data as"? Do you mean the form?thinsoldier wrote:would hopefully return the same data as
[syntax]
http://
example.com/
datasource.php?not-expired¬-hidden&application-completed&category=commercial+sale&island=420&property-type=71,23,44,11&json&paginate&pag-limit=20&pag-offset=60
[/syntax]
Anyway, here is a quick Model class if you wanted to code it directly. This version uses direct setters. It could also have a more declarative interface.
Code: Select all
class SearchModel
{
protected $db;
protected $not_expired = 0;
protected $not_hidden = 0;
protected $application_completed = 0;
protected $category = 'commercial sale';
protected $island = '';
protected $property_types = array();
protected $offset = 0;
protected $limit = 0;
public function __construct ($db)
{
$this->db = $db;
}
public function setNotExpired ($not_expired)
{
$this->not_expired= $not_expired;
}
public function not-hidden ($not_hidden)
{
$this->not_hidden = $not_hidden;
}
public function applicationCompleted ($application_completed)
{
$this->application_completed = $application_completed;
}
public function category($category='')
{
$this->category = $category ? $category : 'commercial sale';
}
public function island($island='')
{
$this->island = $island;
}
public function propertyTypes ($property_types)
{
if (is_string($property_types)) {
$property_types = explode(',', $property_types);
}
$this->property_types = $property_types;
}
public function limit ($offset, $limit=20)
{
$this->offset = $offset;
$this->limit = $limit;
}
public function search ()
{
$sql = 'SELECT id,address,city,state,zip,price,realtor FROM properties WHERE ';
// add constraints based on settings here
// execute query
// fetch rows
return $rows;
}
}