Saturday, October 2, 2010

Re: unbind USER model inside the USER controller

On Sat, Oct 2, 2010 at 1:27 PM, Mariano C. <mariano.calandra@gmail.com> wrote:
> Then, I have a User model and a Book Model I bind it as:
> User HABTM Book
> Book HABTM User
>
> So I have books_users table inside my DB and CAKEPHP will
> automatically create BooksUser virtual model.
>
> Now if I want to know about a user's book collection I call method
> getCollection inside User controller. This work QUITE perfectly and
> will return me a data set like:
> Array
> (
>    [0] => Array
>        (
>            [BooksUser] => Array
>                (
>                    [id] => 4ca75919
>                )
>
>            [Book] => Array
>                (
>                    [id] => 6a915e32
>                    [title] => the Dubliners
>                )
>
>          [User] => Array
>                (
>                    [0] => Array
>                        (
>                            [id] => 4ca3183d
>                            [BooksUser] => Array
>                                (
>                                    [id] => 4ca75919
>                                )
>
>                        )
>  );
>
> This return me what I need, but there is even [User] information.
> Inside this array there will be all users that own that book, this
> will mean a lot of unuseful information and a lot of unuseful load.
> How can I unbind it?
> The real problem is that I need to unbind model User from User
> controller, I can do that? Otherwise, what can I do?

No need to unbind.

$data = $this->User->find(
'first',
array(
'conditions' => array(
'User.id' => $id
),
'fields' => array('...'),
'contain' => array(
'Book'
)
)
);

That should get you just the Books for this User, and not all the
Users for each Book.

> PS: I have thing to remove [User] using PHP unset() function. In this
> way there will be less data send to view, but I think that simply
> unbinding model will increase performance?

Sure, it'd save having to fetch the extra data, allocate memory, etc.

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 cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: