0

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);
Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171
Nilam Doctor
  • 491
  • 7
  • 18

2 Answers2

3

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

Mehdi Lahmam B.
  • 2,240
  • 16
  • 22
0

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'));
Nilam Doctor
  • 491
  • 7
  • 18
  • Pls RTFM! find('list') doesn't need all that jazz to return a key/value pairs for populating a form select... Moreover, what you wrote is exactly what I explained in my answer – Mehdi Lahmam B. Jan 08 '13 at 12:32