Select tag "using" with Concat


Nov '14

Jan '15

3

929

1


Nhi
5.3k

If you have a table user with some filed id, lastname, firstname, etc and you want to display fullname when render to be view, you can used code below. I assume you have a user model

In folder Form create it

    $this->add(new Select('user', User::find(["columns" => "id,
            CONCAT(latsname, ' : ', firstname) as fullname"]),
            array(
                'using'   => ['id', 'fullname'],
                'required' => true
            )
        ));

Then in view you need just call it

   {{ form.render('user') }}

Also understand form you can see example vokuro here


Thien
31.3k

nice tips


Nhi
5.3k
edited Jan '15
Jan '15

That's a nice shortcut, Nhi.

But it will not friendly for the database if you render many of forms like this. You shouldn't use this technique on a "busy" page, website or serving app which has a lot of traffic or contains many similar forms of other users.


Nhi
5.3k

@Tung

Yes, You have any idea for it ?

@Nhi: No, it's depend on the context. You could use this technique on page which has only one form for one user or less frequent access page / features. This might save your dev time.

In summary, to get more performance or serve more concurrent users, you should prevent execute SQL as much as possible, try to get data in batch instead of do a lot of separate "find" which execute SQL.