What's the recommended way to populate the options for a Phalcon\Forms\Element\Select object with data from Phalcon\Mvc\Model?
For example, say I have 2 models, User and Email and they're related like so:
class User extends \Phalcon\Mvc\Model {
public function initialize() {
$this->hasMany("user_id", "Email", "user_id", array('alias'=>'emails'));
}
}
....
class Email extends \Phalcon\Mvc\Model {
public function initialize() {
$this->hasOne("user_id", "User", "user_id", array('alias'=>'user'));
}
}
And I want to make a select form element with all the user's emails as options. If you use Phalcon\Forms\Element\Select, you can pass it an assoc array to populate all the options, but in the manual I didn't see a way to pull an assoc array out of the model. So do I have to do something like this every time I want to fill them in?
class SomeForm extends Phalcon\Forms\Form {
public function initialize() {
$user = User::find(25);
$emails = $user->getEmails(array(
'columns' => array('email_id', 'email')
));
$assoc_emails = array();
foreach($emails as $e) {
$assoc_emails[$e['email_id']] = $e['email'];
}
$this->add(new Phalcon\Forms\Element\Select('email', $assoc_emails));
}
I'm sure I'm doing something wrong here. Is it the way I'm pulling from the model? Or is there an easier way to set the select options? Is there something like Symfony's Entity field type that I should be using instead?