Problem with condition and relation

Hey Guys,

I came across a very odd problem with yii concerning relations. Let’s say I want to code a timeline, which shows me only that posts, which are in special folders. One post could be in many folders and I can add folders to get the posts of them in my timeline.

I generated that models:

Post, Folder, User, UserFolder and PostFolder

Now, I do the following:

$m = Post::model()->with(‘folder.user’=>array(‘alias’=>‘v’)->findAll(‘v.userid=?’, array($id));

This works like a charm. It show’s me only that posts, which are in a folder, that I added in the user_folder table. But there is still one problem: Let’s say I am writing one post in two folders: test and test2. I have added only one folder: “test”.

If I print all folders of that post with foreach($m->folder as $f) { echo $f; }

It shows me only that folders, which I added in the UserFolder table. I want to show all folders, in which this post is, but only want to show that posts, which folder I added in UserFolder…

How can I achieve this?

Here an example for better understanding:

User: id: 1, name: max

Folder: id: 1, name: folder1; id: 2, name: folder2

Post: id: 1, text: test;

User_Folder: userid: 1, folderid: 1

Post_Folder: postid: 1, folderid: 1; postid: 1, folderid: 2

$m = Post::model()->with(‘folder.user’=>array(‘alias’=>‘v’)->findAll(‘v.userid=?’, array($id));

So, I am getting all posts of folders, which are connected to the user in User_Folder.

I finally want to show:

Post: text: test, folderid’s: 1 and 2

But it shows:

Post: text: test, folderid’s: 1

Can anyone help me? Thank you!

I’m not sure at all, but this is what I’ve got in my mind:




// Post.php

public function relations()

{

    return array(

        'folder' => array(self::MANY_MANY, 'Folder', 'post_folder(postid, folderid)'),

        'user_folder' => array(self::MANY_MANY, 'Folder', 'post_folder(postid, folderid)'),

        ...

    );

}

...

$m = Post::model()->with('user_folder.user'=>array('alias'=>'v'))

    ->findAll('v.userid=?', array($id));



Not tested. Sorry if it failed to work.

Works perfectly! Thank you :)