Yii 中元表的模型

发布于 2024-11-17 00:21:26 字数 291 浏览 4 评论 0原文

我刚刚开始在 Yii 中开发我的新项目,我对 Yii 以及框架和 MVC 结构都很陌生。

我想获得一些关于制作元表和该表模型的建议。假设我想为一个用户存储多个聊天句柄。我创建了一个表来存储用户详细信息,例如 user_id、user_name、email.. 另一个表是我的元表,其结构为 id、user_id、key、value

I我想将这两个表关联到一个模型中,以便我可以访问存储在元表中的键值,例如 $user->yahoo。

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

滿滿的愛 2024-11-24 00:21:26

只需为元表和主表创建一个模型即可。然后,如果您与该表有外键关系,您将能够将元表模型作为主表的属性进行访问。为了让事情更清楚:

假设我们有这两个表:

Customer
-ID
-姓名
-地址
-姓名
- 电子邮件

customer_meta
-ID
-客户 ID
-元密钥
-meta_value

您将使用 Gii 为这两个表生成两个模型。然后,如果您使用 MySQL 和 InnoDB 表并创建外键关系 ebtween customer.id -> customer_meta.customer_id 您将能够访问客户模型中的元数据,如下所示:

// 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 />';
}

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 />';
}
任谁 2024-11-24 00:21:26

请参阅: http://www.yiiframework.com/doc/guide/1.1 /en/database.arr

它引导您完成与您想要执行的操作类似的基本示例。请注意,如果您使用外键设置数据库,Gii 工具将在您的模型中为您创建此关系。如果您使用MySql、MySqlWorkbench或其他类似工具可以帮助管理外键,您只需要确保您使用的是InnoDB类型表。

see: http://www.yiiframework.com/doc/guide/1.1/en/database.arr

It walks you through a basic example similar to what you want to do. Note that if you set up your database with foreign keys, the Gii tools will create this relation for you in your model. If you are using MySql, MySqlWorkbench or other similar tools can be helpful for managing foreign keys, you just need to make sure you are using InnoDB type tables.

枫以 2024-11-24 00:21:26

使用 getter 和 setter。定义一个 getYahoo 方法,并在该方法中查找表以检索所需的值

use getters and setters. Define a getYahoo method and in that method do de lookup table to retrieve the value that you want

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文