I did a script that stores a set of filter values in a database.
The user selects some values from the form and saves the filter setup to my MySQL database.
Once he has added a filter setup he can access it over a link. The result is taken from the MySQL database
and then it is deserialized, passed to a raw MongoDB query.
The question here, is this an acceptable way to do this? The script works properly.
Example object
O:8:"stdClass":1:{s:4:"zone";a:1:{s:3:"$in";a:1:{i:0;s:14:"items.name";}}}
// adds a predefined filter to the database
public function storeFilter()
{
$builder = new \stdClass();
$inputArray = Input::toArray(); // Get all the user input
if (array_key_exists('filters', $inputArray)) {
$builder->filters = ['$in' => $inputArray['filters']];
}
// Serialize
$serialize = serialize($builder);
// Store filter
$filter = Filters::firstOrNew([
'md' => md5($serialize), // m5 the content to access it quickly over a route [display-finder/{md5}]
'object' => $serialize,
'user_id' => $user->id,
]);
$filter->save();
}
public function displayResults()
{
$filter = Filters::where('md', '=', $id)->first(); // get the filter setting based on the md5
$deserialize = unserialize($filter->object);
$resultList = $this->searchWithFilter($deserialize); // do a raw mongodb query
return view('_dev.someView', compact('resultList', 'user'));
}
//
protected function searchWithFilter($array)
{
$models = PredefinedFilter::raw(function ($collection) use (&$array) {
return $collection->find(
$array, ["typeMap" => ['root' => 'array', 'document' => 'array']])
->toArray();
});
return $models;
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire