Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Phalcon\Forms\Element\Select mongodb

<?php
namespace app\forms;

use Phalcon\Forms\Form,
    Phalcon\Forms\Element\Select;


class RegionForm extends Form
{
    public function initialize()
    {
        $country = new Select('country', \countries::find(), array(
            'using' => array('id_num','name')
        ));

        $this->add($country);
    }
}
<? $this->forms->set('region', new app\forms\RegionForm()); ?>
<?= $this->forms->get('region')->render('country'); ?>

Result:

PHP Catchable fatal error:  Object of class countries could not be converted to string in ...

Using mongodb. What I'm doing wrong??



289
Accepted
answer
edited Aug '14

I've had the same problem. It seems that documents can't be used with select. You have to modify the data from mongodb before. See below what you can do :

use Models\Profiles;

class SignUpForm extends Form
{
    public function initialize()
    {
        //User profile
        $profiles = Profiles::find(array(array(
            'active' => true,
            'visible' => true),
            'fields' => array(
                '_id',
                'name')
        ));
        $profile = new Select('profile', Profiles::returnArrayForSelect($profiles), array(
            'useEmpty' => true,
            'emptyText' => 'You are ...',
            'emptyValue' => '',
            'class' => 'form-control index-form'
        ));
        $this->add($profile);
    }
}
use Phalcon\Mvc\Collection;

class Profiles extends Collection
{

    public $_id;
    public $active;
    public $name;
    public $visible;

    public static function returnArrayForSelect($obj)
    {
        $array = array();
        foreach ($obj as $v) {
            $array[(string) $v['_id']] = $v['name'];
        }
        return $array;
    }

}

It's now really clean but it works!