Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 建立一对多的关系_Php_Mysql_Activerecord_Yii - Fatal编程技术网

Php 建立一对多的关系

Php 建立一对多的关系,php,mysql,activerecord,yii,Php,Mysql,Activerecord,Yii,我正在尝试使用活动记录在Yii建立多人关系 我有三张桌子 侧面图 个人资料 外形描述 类别 类别识别码 类别名称 个人资料类别 个人资料 类别识别码 我的模型是Profile、Category和ProfileCategory 我正在尝试使用category_id运行一个查询,该查询将调出该类别中的所有配置文件 这是类别模型中的信息 public function relations() { // NOTE: you may need to adjust the relation name

我正在尝试使用活动记录在Yii建立多人关系

我有三张桌子

侧面图 个人资料 外形描述

类别 类别识别码 类别名称

个人资料类别 个人资料 类别识别码

我的模型是Profile、Category和ProfileCategory

我正在尝试使用category_id运行一个查询,该查询将调出该类别中的所有配置文件

这是类别模型中的信息

public function relations()
{
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(
        'profiles'=>array(
            self::MANY_MANY,
            'Profile',
            'profile_category(category_id, profile_id)',
        ),
        'profile_category'=>array(
            self::HAS_MANY,
            'ProfileCategory',
            'category_id',
        ),
    );
}
剖面模型

public function relations()
{
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(
    'categories'=>array(
            self::MANY_MANY,
            'Category',
            'profile_category(profile_id, category_id)'
        ),
        'profileCategory'=>array(
            self::HAS_MANY,
            'ProfileCategory',
            'profile_id'
        ),
   );
}
ProfileCategory模型

public function relations()
{
    return array(
        'category'=>array(
            self::BELONGS_TO,
            'Category',
            'category_id',
        ),
        'profile'=>array(
            self::BELONGS_TO,
            'Profile',
            'profile_id',
        ),
    );
}
控制器

public function actionResults()
{   
    $category=$_POST['terms'];
    $dataProvider=new CActiveDataProvider(
        'Profile',
        array(
            'criteria'=>array(
                'with'=>array('profile_category'),
                'condition'=>'display=10 AND profile_category.category_id=1',
                'order'=>'t.id DESC',
                'together'=>true,
            ),
        )
    );
    $this->render('results',array(
        'dataProvider'=>$dataProvider,
    ));
}
看法



有什么想法吗?非常感谢。视图中没有显示任何内容

您必须在配置文件模型中设置名为profileCategory(非profile_category)的属性:

    'profileCategory'=>array(
        self::HAS_MANY,
        'ProfileCategory',
        'profile_id'
    ),
您可以将其与和条件一起使用,如下所示:

       'criteria'=>array(
            'with'=>array('profileCategory'),
            'condition'=>'display=10 AND profileCategory.category_id=1',
            'order'=>'t.id DESC',
            'together'=>true,
        ),

最好先用逻辑id来建模关系

有一个例子

类别模型 “配置文件”,“配置文件类别(配置文件id,…)”

剖面模型 “类别”,“配置文件类别(类别id,…)”

它实际上是数据库模型的一部分
许多self::属于_,数据库只有PK(PrimaryKeys)

这样的代码将被正确执行

print_r(Profile::model()->findByPk(5)->categories);
print_r(Category::model()->findByPk(8)->profiles);
public function relations()
{
    return array(
        'profiles'=>array(
            self::MANY_MANY,
            'Profile',
            'profile_category(profile_id, category_id)'
        ),
        'profile_category'=>array(
            self::HAS_MANY,
            'ProfileCategory',
            'category_id',
        ),
    );
}
public function relations()
{
    return array(
        'categories'=>array(
            self::MANY_MANY,
            'Category',
            'profile_category(category_id, profile_id)'
        ),
        'profile_category'=>array(
            self::HAS_MANY,
            'ProfileCategory',
            'profile_id'
        ),
    );
}
print_r(Profile::model()->findByPk(5)->categories);
print_r(Category::model()->findByPk(8)->profiles);