CakePHP - 将项目状态代码关联到状态txt

时间:2013-10-15 19:20:44

标签: php cakephp

我通常在代码中保存itens状态(让我保存我有表:学生,为了保存学生的状态,我使用字段(students.status))。

但是,每次我列出用户时我都不会显示状态代码(例如1或0)。我需要显示:已注册或已取消。

我可以在列出时简单地检查一下,但是,让我说我需要做很多次。 有什么比可以帮我做的吗?会节省很多工作,我会列出用户的每个页面,甚至当我添加/编辑他或这些项目附带的下拉菜单时。

我已经检查了模型关联,但是我找到的解决方案如果我有另一个用户状态保存的表(例如我真的不想创建它)。

感谢。

4 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

如果可以注册或取消状态,则可以在表模式中使用枚举数据类型。

您可以从枚举数据类型中获取状态列表,以便像这样填充下拉列表。

$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;
}

我希望这对你有用。感谢

答案 2 :(得分:0)

我最终为此创建了一个Helper函数(不确定是否最好的方法,它一直正常工作)

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));
?>

我想我也应该使用枚举,并且使用指定的功能,我也会成功。 谢谢你的帮助。

答案 3 :(得分:0)

您可能希望查看模型关联。在您的情况下,我将有一个students表和一个包含ID和状态名称的student_statuses表。然后,在找到学生时,您还可以添加相应的StudentStatus行。

这种关系如下:

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

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

然后找到学生......

<?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; ?>

您说您不想为状态创建表,但是您遇到问题的原因是因为它是关系数据库的工作方式以及约定。这样做可以为您节省很多麻烦,如果您决定将来需要添加其他状态,它也可以扩展。你可能不认为你现在这样做,但请相信我:有一天你会。