Friday, August 24, 2012

Re: How to retrieve this kind of data?

I think for what you want to do you should be selecting the Listings
and including the Users for each:

$selected = $this->Listing->find(
'all',
array(
'conditions' => array(
'Listing.id' => $id
),
'contain' => array(
'User'
)
)
);

Even better, put the code in the Listing model:

$selected = $this->Listing->fetchUsers($id);

public function fetchUsers($id = null)
{
if (empty($id)) return null;

return $this->find(
'all'
array(
'conditions' => array(
$this->alias.'.id' => $id
),
'contain' => array(
'User'
)
)
);
}

Also, when using 'contain' you don't need to include 'recursive'.

On Thu, Aug 23, 2012 at 12:55 PM, Fábio <fhcs@live.com> wrote:
> I'm trying to obtain the list of users assigned to the "listing" model to
> use this as selected values for checkboxes in edit form.
>
> The code below obtains a list of users, but its giving me all the users and
> i want only the selected ones. The condition in listing model makes the
> listing that is not the same as the one i am editing not to be returned, but
> the user is still being returned.
>
>> $selected = $this->Listing->User->find('all', array(
>> 'recursive' => 1,
>> 'contain' => array(
>> 'Listing' => array(
>> 'conditions' => array(
>> 'Listing.id' => $id
>> )
>> )
>> ),
>> 'conditions' => array(
>> )
>> ));
>
>
> thanks in advance
>
> --
> You received this message because you are subscribed to the Google Groups
> "CakePHP" group.
> To post to this group, send email to cake-php@googlegroups.com.
> To unsubscribe from this group, send email to
> cake-php+unsubscribe@googlegroups.com.
> Visit this group at http://groups.google.com/group/cake-php?hl=en-US.
>
>

--
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en-US.

No comments: