Monday, April 27, 2009

Re: User Id Question

The problem here is you're calling paginate(). That method calls
find() internally, and it didn't have your conditions. You want to use
one or the other. What you've done is assign the result of find() to
$songs, then set the *view* var $songs to the result of paginate().

If you'd prefer to paginate the results, you can add conditions to
your $paginate class var (no, I have no idea why someone decided to
use the same name for that) but you can't set it in this case because
it's a class var and the user_id in the session would be meaningless.
So, what you'd have to do is leave the conditions out of the array
definition and assign it within your methods.

class SongsController extends UsersController
{
var $name = 'Songs';

var $paginate = array(
'limit' => ...,
'order' => array(
...
),
'fields' => array(
...
),
'recursive' => ...
);

function index()
{
$user_id = $this->Session->read('Auth.User.id');

if (!$user_id)
{
// redirect
}

/* add condition to the class $paginate array
*/
$this->paginate['conditions'] = array(
'Song.user_id' => $user_id
);

$this->set('songs', $this->paginate());
}
}

On Mon, Apr 27, 2009 at 8:08 PM, Teedaddy <bretteegarden@gmail.com> wrote:
>
> Works beautifully Brian... now if it's not much of a bother, how would
> this Auth.User.id method translate to the other functions within my
> songs controller, such as index, edit and view?
>
> I tried adjusting the index function so it only shows the related
> songs of the Auth User like this:
>
> function index() {
>                $user_id = $this->Session->read('Auth.User.id');
>                $this->data['Song']['user_id'] = $user_id;
>                //$this->Song->recursive = 0;
>                $songs = $this->Song->find('list', array('conditions' => array
> ('Song.user_id' => $user_id)));
>                $this->set('songs', $this->paginate());
>        }
>
> it doesn't work, but at least it doesn't cause an error, which is
> progress for me :)
>
>
> On Apr 27, 1:48 pm, brian <bally.z...@gmail.com> wrote:
>> You don't need to have the user ID in the form. Not unless you might
>> have an admin creating songs for another user. You can remove the
>> user_id hidden field and do this:
>>
>> function add()
>> {
>>         /* create var here so it's only read once but
>>          * there's no need to set it for the view
>>          */
>>         $user_id = $this->Session->read('Auth.User.id');
>>
>>         if (!empty($this->data))
>>         {
>>                 /* add current User's ID to data here
>>                  */
>>                 $this->data['Song']['user_id'] = $user_id;
>>                 $this->Song->create();
>>
>>                 if ($this->Song->save($this->data))
>>                 {
>>                         $this->Session->setFlash(__('The Song has been saved', true));
>>                         $this->redirect(array('action'=>'index'));
>>                 }
>>                 else
>>                 {
>>                         $this->Session->setFlash(__('The Song could not be saved. Please,
>> try again.', true));
>>                 }
>>         }
>>
>>         $catalogs = $this->Song->Catalog->find(
>>                 'list',
>>                 array(
>>                         'conditions' => array(
>>                                 'Catalog.user_id' => $user_id
>>                         )
>>                 )
>>         );
>>         $this->set(compact('catalogs'));
>>
>> }
>>
>> I'm assuming that Catalog has a user_id field. If that's not the case,
>> post your Catalog and Song models.
>>
>> On Mon, Apr 27, 2009 at 1:36 PM, Teedaddy <bretteegar...@gmail.com> wrote:
>>
>> > Here is my add function for my song_conntroller:
>>
>> > function add() {
>> >                if (!empty($this->data)) {
>> >                        $this->Song->create();
>> >                        if ($this->Song->save($this->data)) {
>> >                                $this->Session->setFlash(__('The Song has been saved', true));
>> >                                $this->redirect(array('action'=>'index'));
>> >                        } else {
>> >                                $this->Session->setFlash(__('The Song could not be saved. Please,
>> > try again.', true));
>> >                        }
>> >                }
>> >                $this->set('user_id', $this->Session->read('Auth.User.id'));
>> >                //$users = $this->Song->User->find('list');
>> >                $catalogs = $this->Song->Catalog->find('list');
>> >                $this->set(compact('catalogs'));
>> >        }
>>
>> > I was able to get the User ID to pass to the Add Form with your help
>> > but where I am confused, is how to get the $catalogs = find array to
>> > return only the current Auth.User.id related records.
>>
>> > I think if I can get this down, I will be well on my way to finishing
>> > my project (crossing fingers)!
>>
>> > I appreciate your help greatly....
>>
>> > On Apr 27, 12:27 pm, Teedaddy <bretteegar...@gmail.com> wrote:
>> >> Sorry, I am assuming it is a similar method since the htlmlink for
>> >> "New Song" would need to contain the user_id for which I create a new
>> >> song record. Maybe this is just my old procedural way of thinking
>> >> hanging on here.
>>
>> >> Am I assuming correctly that all these user id dependencies need to be
>> >> programmed into the controllers and not the views or links?
>>
>> >> I know enough PHP to be dangerous and am not that far along with just
>> >> a procedural approach but I love the concept of MCV and cakePHP so I
>> >> want to learn this way of developing from this point forward.
>>
>> >> Maybe my lack of PHP mastery is making this much harder for me than it
>> >> should be.
>>
>> >> Thanks again.
>>
>> >> On Apr 27, 12:19 pm, brian <bally.z...@gmail.com> wrote:
>>
>> >> > OK, well that's different than adding the User.id to links. For what
>> >> > you want to do, you need only add it to the conditions of your find.
>> >> > There's no need for it to be in the link.
>>
>> >> > Ditto for editing a record: just check the session and compare it to
>> >> > the user_id in your data. If it doesn't match, throw up a flash msg
>> >> > and redirect.
>>
>> >> > When in the controller, use $this->Session->read(...) and, in the
>> >> > view, $session->read(...)
>>
>> >> > On Mon, Apr 27, 2009 at 1:07 PM, Teedaddy <bretteegar...@gmail.com> wrote:
>>
>> >> > > I basically want all my controller actions to be user specific once a
>> >> > > user is logged in. I'm getting close and these suggestions have been
>> >> > > very helpful.
>>
>> >> > > The main task would be to assure that all related records that are
>> >> > > returned, found, added, etc, are tied to a specific user.id.
>>
>> >> > > For instance, the project I am working on is a song catalog. The
>> >> > > hierarchy is UserGroups -> Users -> Catalogs -> Songs.
>>
>> >> > > Individual users can have many Catalogs but when the view for Songs is
>> >> > > displayed or one tries to add a new song, all the records in the DB
>> >> > > are displayed or all Catalogs from all Users are available in the
>> >> > > Catalog dropdown form item. I only want the selection choices for
>> >> > > Catalog to be those of the logged in user. Right now (with Baked views
>> >> > > and controllers) every Catalog belonging to every user is displayed as
>> >> > > a possible choice. I also need to make sure only related records
>> >> > > (keyed by user.id) are shown in all cases.
>>
>> >> > > Please forgive me if I am not making this as understandable as it
>> >> > > needs to be. Thanks so much for your help.
>>
>> >> > > On Apr 27, 10:17 am, brian <bally.z...@gmail.com> wrote:
>> >> > >> Are you using Auth? What's the route for this URL? This should work:
>>
>> >> > >> $html->link(
>> >> > >>         'edit profile',
>> >> > >>         array(
>> >> > >>                 'controller' => 'users',
>> >> > >>                 'action' => 'edit',
>> >> > >>                 'user_id' => $session->read('Auth.User.id')
>> >> > >>         ),
>> >> > >>         array('title' => 'whatever')
>> >> > >> )
>>
>> >> > >> If you don't have a route that specifically passes user_id, you can also do:
>>
>> >> > >> $html->link(
>> >> > >>         'edit profile',
>> >> > >>         array(
>> >> > >>                 'controller' => 'users',
>> >> > >>                 'action' => 'edit',
>> >> > >>                 $session->read('Auth.User.id')
>> >> > >>         ),
>> >> > >>         array('title' => 'whatever')
>> >> > >> )
>>
>> >> > >> On Mon, Apr 27, 2009 at 10:34 AM, Teedaddy <bretteegar...@gmail.com> wrote:
>>
>> >> > >> > Thanks for the response, but this still doesn't answer my question of
>> >> > >> > how to include this variable in a $htmlhelper link, mainly need to
>> >> > >> > know the syntax. thanks so much
>>
>> >> > >> > On Apr 26, 10:36 pm, NegoBlack® <paulomoacir.jun...@gmail.com> wrote:
>> >> > >> >> I use to do this on my app_controller.php:
>>
>> >> > >> >> public function beforeFilter(){
>>
>> >> > >> >>     if ($this->Auth->user()){
>> >> > >> >>         $this->set("userInfo", $this->Auth->user())
>> >> > >> >>     }
>>
>> >> > >> >> }
>>
>> >> > >> >> Then I can use this var on my views...
>>
>> >> > >> >> On Apr 25, 7:57 pm, Teedaddy <bretteegar...@gmail.com> wrote:
>>
>> >> > >> >> > Could someone be so kind to tell me the easiest way to pass the
>> >> > >> >> > current logged in "Auth" user id in $htmlhelper links?
>>
>> >> > >> >> > Thank you so much, just learning all this MVC method and have been
>> >> > >> >> > through Auth tutorials numerous times but I am missing this somehow.
>>
>> >> > >> >> > Bret
> >
>

--~--~---------~--~----~------------~-------~--~----~
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: