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