dousu1916 2014-07-30 18:22
浏览 153

如何在Yii中使用jSON为对象创建其他必需字段

I have a project to create a flexible information system. In this project I use a relational database (MySQL) and the Yii framework. Sometimes I have objects that can collect different information according to the id of the objecttype.

Basically, I have an object, which has an objecttype. In the objecttype table i have an info field which will contain a jSON with the description of the additional fields that I will require from the user at the moment of the new object creation.

http://www.bedoya.co/screenshots/object-objecttype-relation.png

Ok, now, with PHP I will collect the contents from objecttype_info (stored there as a jSON) and I will generate an array similar to this one:

<?php
    $x = array(
        'name'      => 'Cool name to put in the fieldset label of this object type',
        'fields'        => array( // List of additional fields that describe this object
            'field1'    => array(
                'label'         => 'Field1 label',
                'type'          => 'text',
                'htmlOptions'   => array(
                    'class'     => 'field1-class'
                    'id'        => 'field1-id'
                    'required'  => true
                )
            ),
            'campo2'    => array(
                'label'         => 'Field2 label',
                'type'          => 'number',
                'htmlOptions'   => array(
                    'class'     => 'field2-class'
                    'id'        => 'field2-id'
                    'required'  => false
                )
            ),
        )
    );

Now, I get the required values with the easy function:

<?php
    $x = ObjectType::model()->findByPk( $objecttype_id )->attributes[ 'objecttype_info' ];
?>

I don't know how to set the validation rules for the fields obtained from the objecttype. Any ideas? Am I doing this right?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!
    • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
    • ¥15 求daily translation(DT)偏差订正方法的代码
    • ¥15 js调用html页面需要隐藏某个按钮
    • ¥15 ads仿真结果在圆图上是怎么读数的
    • ¥20 Cotex M3的调试和程序执行方式是什么样的?
    • ¥20 java项目连接sqlserver时报ssl相关错误
    • ¥15 一道python难题3
    • ¥15 牛顿斯科特系数表表示