0

私は通常、コード内に itens ステータスを保存します (テーブルがあるので保存しましょう: 学生、学生のステータスを保存するには、フィールド (students.status) を使用します)。

ただし、ユーザーを一覧表示するたびに、ステータス コード (1 または 0 など) は表示されません。I need show: 登録済みまたはキャンセル済み。

リストするときに簡単に確認できますが、何度も行う必要があるとしましょう。それを行うのに役立つものはありますか?ユーザーをリストするすべてのページ、またはユーザーまたはそれらのアイテムに付属するドロップダウンメニューを追加/編集する場合でも、多くの作業を節約できます。

モデルの関連付けを確認しましたが、たとえば、ユーザー ステータスが保存された別のテーブルがある場合に、見つけた解決策が機能します (正直なところ、作成したくありません)。

ありがとう。

4

4 に答える 4

1

ステータスを登録またはキャンセルできる場合は、テーブル スキーマで enum データ型を使用できます。

このように、ドロップダウンを設定するための enum データ型からステータスのリストを取得できます。

$status = $this->Model->getEnumValues('status');

この前に、appModel.php に次のコードを追加する必要があります。

function getEnumValues($columnName=null)
{
    if ($columnName==null) { return array(); } //no field specified

    //Get the name of the table
    $db =& ConnectionManager::getDataSource($this->useDbConfig);
    $tableName = $db->fullTableName($this, false);

    //Get the values for the specified column (database and version specific, needs testing)
    $result = $this->query("SHOW COLUMNS FROM {$tableName} LIKE '{$columnName}'");

    //figure out where in the result our Types are (this varies between mysql versions)
    $types = null;
    if     ( isset( $result[0]['COLUMNS']['Type'] ) ) { $types = $result[0]['COLUMNS']['Type']; } //MySQL 5
    elseif ( isset( $result[0][0]['Type'] ) )         { $types = $result[0][0]['Type'];         } //MySQL 4
    else   { return array(); } //types return not accounted for

    //Get the values
    $values = explode("','", preg_replace("/(enum)\('(.+?)'\)/","\\2", $types) );

    //explode doesn't do assoc arrays, but cake needs an assoc to assign values
    $assoc_values = array();
    foreach ( $values as $value ) {
        //leave the call to humanize if you want it to look pretty
        $assoc_values[$value] = Inflector::humanize($value);
    }
    return $assoc_values;
}

これがうまくいくことを願っています。ありがとう

于 2013-10-16T00:25:53.917 に答える
1

これは、あなたが望むことを行う方法を説明しています: http://www.dereuromark.de/2010/06/24/static-enums-or-semihardcoded-attributes/

于 2013-10-15T23:35:11.290 に答える
0

そのためのヘルパー関数を作成することになりました(最善の方法かどうかはわかりませんが、うまく機能しています)

MyHelper.php

class MyHelper extends AppHelper {

        public $helpers = array();

        public $list = array(0 => 'Cancelled', 1 => 'Registered');
        public function __construct(View $View, $settings = array()) {
                parent::__construct($View, $settings);
        }

        public function beforeRender($viewFile) {

        }

        public function afterRender($viewFile) {

        }

        public function beforeLayout($viewLayout) {

        }

        public function afterLayout($viewLayout) {

        }

        public function translate($id){
                return $this->list[$id];
        }

}

view.ctp では、アイテム ID を表示する代わりに、translate 関数を呼び出して適切な名前を返します。ドロップダウン メニューを作成するには、選択オプションで配列リストを呼び出すだけです。

<?php
echo "User Status: " . $this->My->translate($this->User->status);

echo $this->Form->input('tipo', array('type' => 'select', 'options' => $this->My->list));
?>

enum も使用する必要があったと思います。指定された関数を使用すると、成功することもありました。助けてくれてありがとう。

于 2013-10-16T20:33:37.897 に答える
0

モデルの関連付けを確認することをお勧めします。あなたの場合、studentsテーブルとstudent_statuses、ID とステータス名を含むテーブルがあります。次に、学生を検索するときに、対応するStudentStatus行を含めることもできます。

この関係は次のようになります。

<?php
class Student extends AppModel {
    public $belongsTo = array('StudentStatus');
}

<?php
class StudentStatus extends AppModel {
    public $hasMany = array('Student');
}

生徒を探すときは…</p>

<?php
class StudentsController extends AppController {
    public function index() {
        $this->set('students', $this->Student->find('all'));
    }
}

次のような結果が得られます。

Array
(
    [0] => Array
        (
            [Student] => Array
                (
                    [id] => 1
                    [name] => Martin Bean
                    [student_status_id] => 1
                    [created] => 2013-09-29 21:12:42
                    [modified] => 2013-09-29 21:12:42
                )

            [StudentStatus] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [name] => Approved
                            [created] => 2013-09-23 18:26:06
                            [modified] => 2013-10-01 18:53:16
                        )

                )

        )

)

したがって、次のようにビューで学生のステータスを印刷できます。

<?php foreach ($students as $student): ?>
<p>
  <?php echo h($student['Student']['name']); ?>
  (<?php echo h($student['StudentStatus']['name']); ?>)
</p>
<?php endforeach; ?>

ステータス用のテーブルを作成したくないとおっしゃっていますが、問題があるのは、それがリレーショナル データベースのしくみであり、慣例でもあるためです。そうすることで、多くの頭痛の種から解放されます。また、将来ステータスを追加する必要があると判断した場合にも、拡張可能です。今はそう思っていないかもしれませんが、信じてください。いつの日かそうなるでしょう。

于 2013-10-17T12:40:55.960 に答える