dpvomqeu396484 2011-06-24 10:13
浏览 25
已采纳

Yii元表的模型

I just started developing my new project in Yii, I am new to Yii as well as to frameworks ans MVC structure.

I would to get some advice on making meta tables and model for that table. Suppose I like to store several chat handles for a user. I have created a table to store the user details like user_id, user_name, email.. and another table which is my meta table with structure id, user_id, key, value

I would like to associate these two tables in one model so that I can access the value of key stored in the meta table like $user->yahoo.

Thanks in advance.

  • 写回答

3条回答 默认 最新

  • dongshi2836 2012-01-20 11:42
    关注

    Simply create a model for the meta table as well as the primary table. Then if you have a forign key relation to this table you will be able to access the meta table model as a property of the primary table. To make thigs clearer:

    Say we have these two tables:

    Customer -id -name -address -name -email

    customer_meta -id -customer_id -meta_key -meta_value

    you would generate two models using Gii for both of these tables. Then if you use MySQL and InnoDB tables and create a foriegn key relation ebtween customer.id -> customer_meta.customer_id you will be able to access the meta data in the customer model as such:

    // this will echo get the first meta value
    $model = new customer;
    $customer = $model->loadModel( 3 );
    echo $customer->customer_meta[0]->meta_value;
    
    // or loop through the meta data
    foreach( $customer->customer_meta as $meta ) {
    
       echo 'Name: '.$meta->meta_key.' Value: '.$meta->meta_key.'<br />';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器