ArunKolhapur
ArunKolhapur

Reputation: 6915

Lithium select options from mysql data

I have a MySQL table categories with following columns:

I retrieved the data using

$categories = Categories::find('all');

Now, I want to create a <select> tag with options whose value is $category->id and title is $category->name using form helper.

How to do that?

Upvotes: 0

Views: 216

Answers (2)

Housni
Housni

Reputation: 983

To complement Mehdi's answer, supposing you didn't want the field Categories.name to appear in your select list and instead, you wanted the field Categories.code to appear as the value of the select list, you could do this:

<?php

namespace app\models;

class Categories extends \lithium\data\Model {

    protected $_meta = array('title' => 'code');
}
?>

Now, if you tried using the solution Mehdi gave you, you'd see Categories.code appear as the value instaed of Categories.name.

Also, from the source of lithium/data/Model.php regarding the title key:

title: The field or key used as the title for each record. Defaults to 'title' or 'name', if those fields are available.

Upvotes: 1

Mehdi Lahmam B.
Mehdi Lahmam B.

Reputation: 2270

try $categories = Categories::find('list');

Upvotes: 3

Related Questions