We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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!

Hi Everyone !

i have same question with different type. how to use with check boxes ? my code below

$postcategory = new Check('postcategory', \postcategory::find(), array( 'using' => array('id','name'), array( 'value' => $name, 'class' => 'checkbox'.$id ) )); $this->add($postcategory);