抱歉,我是 yii Web 框架的初学者
我想知道如何关联两个表。
这里,
假设,我有 2 张 table
第一个表 = 成员(id、用户名、密码)
第二个表 = Product(id,member_id,product_name,product_detail)
当我已经发布表单时,我有表单发布产品,我想向 member_id 显示该内容
登录。
这是我的简单代码
在 model/Member.php 中
<?php
class Member extends CActiveRecord {
public static function model($c = __CLASS__) {
return parent::model($c);
}
public function tableName(){
return "members";
}
}
?>
在 model/Product.php 中
<?php
class Product extends CActiveRecord {
public static function model($c = __CLASS__) {
return parent::model($c);
}
public function tableName() {
return "products";
}
public function relations() {
return array(
"members" => array(self::BELONGS_TO, "Member", "members_id")
);
}
}
?>
如何在controller、script.js中编写代码并在 View 上显示值?
最佳答案
<?php
//Get product by primary key.
$product_id = 20;
$data = Product::model()->findByPk($product_id);//SELECT * FROM product WHERE id = 20;
//get username and password from table member.
$username = $data->members->username;
$password = $data->members->password;
//"members" is the alias from function relations() in your model Product
//I mean this "members" => array(self::BELONGS_TO, "Member", "members_id")
//and like this select fields from "producto"
$product_name = $data->product_name;
$product_detail = $data->product_detail;
关于php - 如何关联Yii框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24363726/