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.

Volt Select not showing

Hi all :-)

I want to ask about volt. One of field in the Booking table is memberid, which is a foreign key from Member table.

My Booking controller with newAction :

public function newAction() {

    $this->view->members = Member::find();
}

in Members, I already add this function :

public function __toString() { return $this->firstname . " " . $this->surname; }

I want to ask, how to use Select with volt, that show the member's name?

I already tried this :

{% for member in members %}

    {{ select('fieldMemberid', members, 'using': ['id','member']) }}

{% endfor %}

but not working. The html result give the value, but not showing members name.

If I remove the ' ' sign on member, it generates error like this :

Parameter 'attribute' must be a string

0 [internal function]: Phalcon\Mvc\Model->readAttribute(Object(Tennisclub\Models\Member))

1 [internal function]: Phalcon\Tag\Select::_optionsFromResultset(Object(Phalcon\Mvc\Model\Resultset\Simple), Array, NULL, '\r\n')

2 [internal function]: Phalcon\Tag\Select::selectField(Array, NULL)

I tried without select, and it work perfect.

{% for member in members %}

    {{ member }}

{% endfor %}

If I use .phtml, the working code is :

<select name="memberid" id="fieldMemberid" class="form-control"> <?php foreach ($members as $member) {echo "<option value='".$member->getID()."'>$member</option>";}?> </select>

Can somebody help me to solve this problem? Thank you so much :-)



25.5k
Accepted
answer

I have not tested this but you are trying to create a compound field (new field that is with the __toString) and offer it back to your view. I don't think that it will work.

What you could do is either:

  • You can use the selectStatic instead of select.

Create an array in your controller and pass it in the view. The array will have id as the key and the calculated name as the value. Then you use selectStatic as follows:

{{ selectStatic('fieldMemberid', members) }}

In your code you are looping the resultset of members (what comes from Member::find()) and trying to create a select in the loop. This will result in one select box per member. Not sure if that is what you want to achieve.

  • Create a calculated field in your model like this:
class Member extends Model
{
    public $fullName;

    public function afterFetch()
    {
        $this->fullName = $this->firstname . ' ' . $this->surname;
    }
}

and then in volt you need to use this:

{{ select('fieldMemberid', members, 'using': ['id', 'fullName']) }}


842

Thank you so much Nikolaus :-) It's work.