Make a Model Searchable by Tags
To make a model searchable by tags, we'll make use of the callback
option for $searchConditions
to specify a function that can generate advanced SQL procedurally.
First, define a new search condition:
The callback
option can take any PHP callable
, or a simple string referencing a public static method on the model class. This method will be called for each term in the search query and should return an SQL expression to match records that match this term. The ultimate results list gets sorted by how many terms are matched.
While it is possible to implement joins via $searchConditions
, in this example we are instead going to "unroll" any matching tags ahead of time into a list of tagged record IDs while the search query is being prepared:
Last updated