質問

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