/** * Sets up a query object so results appear as an indexed array, useful for any * place where you would want a list such as for populating input select boxes. * * When calling this finder, the fields passed are used to determine what should * be used as the array key, value and optionally what to group the results by. * By default the primary key for the model is used for the key, and the display * field as value. * * The results of this finder will be in the following form: * * ``` * [ * 1 => 'value for id 1', * 2 => 'value for id 2', * 4 => 'value for id 4' * ] * ``` * * You can specify which property will be used as the key and which as value * by using the `$options` array, when not specified, it will use the results * of calling `primaryKey` and `displayField` respectively in this endpoint: * * ``` * $endpoint->find('list', [ * 'keyField' => 'name', * 'valueField' => 'age' * ]); * ``` * * Results can be put together in bigger groups when they share a property, you * can customize the property to use for grouping by setting `groupField`: * * ``` * $endpoint->find('list', [ * 'groupField' => 'category_id', * ]); * ``` * * When using a `groupField` results will be returned in this format: * * ``` * [ * 'group_1' => [ * 1 => 'value for id 1', * 2 => 'value for id 2', * ] * 'group_2' => [ * 4 => 'value for id 4' * ] * ] * ``` * * @param \Muffin\Webservice\Query $query The query to find with * @param array $options The options for the find * @return \Muffin\Webservice\Query The query builder */ public function findList(Query $query, array $options) { $options += ['keyField' => $this->primaryKey(), 'valueField' => $this->displayField(), 'groupField' => null]; $options = $this->_setFieldMatchers($options, ['keyField', 'valueField', 'groupField']); return $query->formatResults(function ($results) use($options) { return $results->combine($options['keyField'], $options['valueField'], $options['groupField']); }); }