I have a MySQL table categories with following columns:

  • id
  • name

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?

有帮助吗?

解决方案

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

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top