我在2012年11月用Yii和PHP读了一本书Pactpub Web应用程序开发。面对这样的问题,我无法理解使用关系()背后的逻辑。这里是数据库中的图表表格:
您需要在模型中插入代码:
问题模型:
...
'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)'),
...
我无法理解我们添加?如果模型问题了解一切,那么模型项目 - 我不明白我们正在添加。帮助理解......
答案 0 :(得分: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查询:)