2011-06-24 60 views
1

我剛剛開始在Yii中開發我的新項目,我對Yii以及框架和MVC結構都很陌生。Yii中元表的模型

我想得到一些關於爲該表製作元表和模型的建議。假設我喜歡爲用戶存儲多個聊天手柄。我創建了一個表,用於存儲用戶的細節,如USER_ID,USER_NAME,電子郵件 ..和另一個表這是我的元表結構ID,USER_ID,鍵,值

我想這兩個關聯一個模型中的表格,以便我可以訪問存儲在元表中的鍵值,如$ user-> yahoo。

在此先感謝。

+1

的http://計算器.com/questions/3309084/yii-using-relation-has-one-to-get-data-from-the-the-related-table-to-display-in-lis; http://stackoverflow.com/questions/3579852/yii-question-about-relations-function-in-ar-model-many-to-one-relationship – hakre

回答

0

只需爲元表和主表創建一個模型。然後,如果您有與該表格的外鍵關係,則可以作爲主表格的屬性訪問元表格模型。爲了更清楚地thigs:

說我們有這兩個表:

客戶 -id -name -address -name -email

customer_meta -id -customer_id -meta_key -meta_value

您將使用Gii爲這兩個表生成兩個模型。然後,如果你使用MySQL和InnoDB表,並創建一個foriegn密鑰關係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 />'; 
} 
-1

使用getter和setter。定義getYahoo方法和該方法做去查找表檢索值要

0

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

它會引導你相似,你想要做什麼一個基本的例子。請注意,如果您使用外鍵設置數據庫,則Gii工具將在您的模型中爲您創建此關係。如果您使用的是MySql,MySqlWorkbench或其他類似的工具可以幫助您管理外鍵,您只需確保使用InnoDB類型的表。