1

我在 2012 年 11 月读了一本书 Pactpub Web Application Development with Yii and PHP。面对这样的问题,我无法理解使用关系()背后的逻辑。这里是数据库中的图表:

您需要在模型中插入代码:

问题模型:

...
'requester' => array(self::BELONGS_TO, 'User', 'requester_id'),
'owner' => array(self::BELONGS_TO, 'User', 'owner_id'),
'project' => array(self::BELONGS_TO, 'Project', 'project_id'),
);
...

项目型号:

...
'issues' => array(self::HAS_MANY, 'Issue', 'project_id'),
'users' => array(self::MANY_MANY, 'User', 'tbl_project_user_assignment(project_id, user_id)'),
...

我不能理解,我们添加?如果模型问题了解一切,那么模型项目 - 我不明白我们正在添加。帮助理解...

4

1 回答 1

0

如果模型问题了解一切,那么模型项目 - 我不明白我们正在添加

在某些情况下,您已经有一个项目,并且您希望找到该项目的所有问题和合作伙伴用户。

$project = Project::model()->findByPK(1); // get project id=1

$issues = $project->issues; // get all of issues of project id=1, the result would be array
$users = $project->issues; // get all of users of project id=1, the result would be array

$project = Project::model()->with('issues', 'users')->findAll(); // get all of projects which has issue and user

//you have a user name ABC, and you want to find all of projects which contains a issue from owner has that user name.

$projects = Project::model()->with(array(
            'issues' => array(
                'alias' => 'issue',
                //'condition' => '',
                //'params' => array(),
                'with' => array(
                    'owner'=>array(
                        'alias' => 'user',
                        'condition' => 'username =:username',
                        'params' => array(':username'=>'ABC'),
                    )

                )
            ),

        ))->findAll();

有很多方法可以让您将它们与多种关系和条件混合在一起。上述示例之一将生成一些我不想自己处理的大型 SQL SELECT 查询:)

AR 关系详情

于 2013-08-21T12:46:34.570 回答