Question

I want to create a dropdown for types collection from MongoDB, in my MongoDB collection:

section has types and controller code with view code. I want to create a form->select from the output of the controller.

    types
    {
      "_id": ObjectId("5082c6109d5d0c640c000000"),
      "name": "JACKET CLUSTER FRONT"
    }
    {
      "_id": ObjectId("5082c62b9d5d0c440c00006e"),
      "name": "JACKET CLUSTER FRONT"
    }
    {
      "_id": ObjectId("5082c62b9d5d0c440c00006f"),
      "name": "TITLE WITHOUT SYMBOL"
    }
    {
      "_id": ObjectId("5082c62b9d5d0c440c000070"),
      "name": "FRONTISPIECE"
    }
*/

// in my controller
// -----------
    $types = Types::all(array('order'=>'_id'));
    $vtype = array($types)
    return compact('vtypes');

// in my view
// ------------------
    echo $this->form->select('types',$vtypes);
Was it helpful?

Solution

find('list') returns a key/value array, useful for any use where you would want a list such as for populating input select boxes.

$types = Types::find('list')

//returns
Array
(
[5082c6109d5d0c640c000000] => 'JACKET CLUSTER FRONT',
[5082c62b9d5d0c440c00006e] => 'JACKET CLUSTER FRONT',
[5082c62b9d5d0c440c00006f] => 'TITLE WITHOUT SYMBOL',
...
)

This finder looks for $_meta['title'] of your model, which is by default name if this field is available, and $_meta['key'] for the id, which should be _id in your case if your schema is correct

OTHER TIPS

Finally, I used this to achieve the result.

Types::meta('key', '_id');
Types::meta('title', 'filename');

$types = Types::find('list',array(
'fields'=>array('id','filename'),
'order'=>'id'));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top