Run the find on the join model. But, first, you should use the Cake
default naming. The model should be ProjectUser and the table
projects_users.
Put this in your Project model:
public function getAvailableProjects($user_id)
{
return $this->ProjectUser->find(
'all',
array(
'fields' => array(
'DISTINCT(ProjectUser.project_id)'
),
'conditions' => array(
'NOT' => array(
'ProjectUser.user_id' => $user_id
)
),
'contain' => array('Project')
)
);
}
If you're not already using ContainableBehavior, play around with
'recursive' (1 should be sufficient).
On Apr 7, 9:47 am, Renato de Freitas Freire <[email protected]>
wrote:
> Thanks for the answer...
>
> but, is the NOT IN the best way to do this?
>
> I mean... cake doesnt have an own way to retrieve all no releated data?
>
> Other question:
> This way, cake will take the array and compare with each record on my db?
> Cause it may take too long to exectue, since the system will have a lot of
> data...
>
> --
> Renato de Freitas Freire
> [email protected]
>
> On Tue, Apr 6, 2010 at 1:18 PM, Jeremy Burns <[email protected]> wrote:
> > There is a reference to "NOT IN" in the guide - I am sure you could adapt
> > that?
>
> >http://book.cakephp.org/view/1030/Complex-Find-Conditions
>
> > Jeremy Burns
> > [email protected] <[email protected]>
>
> > On 6 Apr 2010, at 17:13, Renato de Freitas Freire wrote:
>
> > Hi all.
> > Sorry if its a noob question, but I cant find it anywhere...
> > So...
>
> > I have this two tables:
>
> > Users:
> > id - int
> > username - varchar
> > password - varchar
> > created - datetime
>
> > Projects:
> > id - int
> > name - varchar
> > created - datetime
>
> > and this HABTM table:
>
> > User_Projects:
> > id - int
> > user_id - int
> > project_id - int
>
> > My system is already getting all realted data...
> > If a user wants to know what projects he can work, I will retrieve his data
> > automagically with cake...
> > ok...
> > but the user can chose in what project he will work today..
> > so, i need to show all projects and let user chose one or more to work..
>
> > I am showing the users projects and all the projects, so he can chose...
> > but in my list appears all projects, even if the user is already working on
> > it...
>
> > what i need to do is to get only the projects that are not selected
> > already.. only the projects the user is NOT working at the moment...
>
> > So.. i have something like this:
>
> > User ID: 1
> > Username: Morfer
>
> > Projects in progress:
>
> > id name
> > 1 Project 1
> > 5 Project 5
> > 8 Project 8
>
> > Projects you can work:
> > id name
> > 1 Project 1
> > 2 Project 2
> > 3 Project 3
> > 4 Project 4
> > 5 Project 5
> > 6 Project 6
> > 7 Project 7
> > 8 Project 8
> > 9 Project 9
> > 10 Project 10
>
> > And i need to show this:
>
> > User ID: 1
> > Username: Morfer
>
> > Projects in progress:
>
> > id name
> > 1 Project 1
> > 5 Project 5
> > 8 Project 8
>
> > Projects you can work:
> > id name
> > 2 Project 2
> > 3 Project 3
> > 4 Project 4
> > 6 Project 6
> > 7 Project 7
> > 9 Project 9
> > 10 Project 10
>
> > In my controller, im getting the data:
>
> > // Get users data and related projects
> > $user = $this->User->read(null, $id);
> > $this->set('user', $user);
>
> > // Get projects data
> > $projects = $this->Project->find("all");
> > $this->set('projects', $projects);
>
> > Can anyone help me?
>
> > Thanks
> > --
> > Renato de Freitas Freire
> > [email protected]
>
> > Check out the new CakePHP Questions sitehttp://cakeqs.organd help others
> > with their CakePHP related questions.
>
> > You received this message because you are subscribed to the Google Groups
> > "CakePHP" group.
> > To post to this group, send email to [email protected]
> > To unsubscribe from this group, send email to
> > [email protected]<cake-php%[email protected]>For
> > more options, visit this group at
> >http://groups.google.com/group/cake-php?hl=en
>
> > Check out the new CakePHP Questions sitehttp://cakeqs.organd help
> > others with their CakePHP related questions.
>
> > You received this message because you are subscribed to the Google Groups
> > "CakePHP" group.
> > To post to this group, send email to [email protected]
> > To unsubscribe from this group, send email to
> > [email protected]<cake-php%[email protected]>For
> > more options, visit this group at
> >http://groups.google.com/group/cake-php?hl=en
>
>
Check out the new CakePHP Questions site http://cakeqs.org and help others with
their CakePHP related questions.
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
To unsubscribe, reply using "remove me" as the subject.