Getting DB entries based on an array of id's...

2008-11-06 Thread wallerdm

Dear All,

Just a quick question really (hopefully).

I have a load of images stored in a table, each with an ID. Users can
choose to bookmark images and this is also stored in a table:

--Users
id - INT
username - VARCHAR

--Images
id - INT
title - VARCHAR
url - VARCHAR

--Bookmarks
id - INT
user_id - INT
image_id - INT

I now have all of my models set up, including all of the
relationships, and have all of the data reading correctly. What I'm
trying to do now is list (as part of the Users controller) all of the
image titles that they have bookmarked.

My users controller now has an array that contains the id's of all the
bookmarked images and I now want to get the data for the images that
have been bookmarked but the find request has stumped me.

class UsersController extends AppController {
var $name = 'Users';
var $uses = array('User','Image');
var $helpers = array('Html', 'Form');

function view($id=null){
$this-set('userdata', $this-User-findById($id));
$temp = $this-User-findById($id);
$bookmarklist = array();
foreach($temp['Bookmark'] as $bookmark){
$bookmarklist[] = $bookmark['image_id'];
}
$this-set('bookmarkdata', $this-Image-find(ARGHHH, WHAT
GOES HERE));
}
}

I hope I've explained myself okay. Any help is greatly appreciated.

Thanks,

Dave.
--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Saving self-referential-HABTM relationships...

2008-09-30 Thread wallerdm

Thanks Bernardo - that makes sense. I think I might drop the concept
of admirers and have an accepted state for friendships.

This all works well when displaying user data, a user model
automatically has data about previous friendships but I still can't
add new friends. The best I can get when I play with the code is a
drop of all entries in the users_users and in insert of the new
friendship.

Any ideas or tutorials that might help me out?

Dave.

On Sep 29, 6:23 pm, Bernardo Vieira [EMAIL PROTECTED] wrote:
 I imagine it should work if you use different join tables for each HABTM
 relationship. Regardless of cakephp this is what I imagine you'd need in
 this situation (imagine what would happen if a user is both a friend and
 an admirer of another user).

         var $hasAndBelongsToMany = array(
                         'Friend' = array('className' = 'User',
                                                 'joinTable' = 
 'friends_users',
                                                 'foreignKey' = 'friend_id',
                                                 'associationForeignKey' = 
 'user_id'
                         ),

                         'Admirer' = array('className' = 'User',
                                                 'joinTable' = 
 'admirers_users',
                                                 'foreignKey' = 'admirer_id',
                                                 'associationForeignKey' = 
 'user_id'
                         )
         );

 wallerdm wrote:
  Dear All,

  Forgive me if this is covered elsewhere. I'm playing with cakePHP as
  an alternative to Rails and am getting to grips with it by creating a
  really simple social network app.

  I've got some nice authentication and user profiles sorted and that's
  all displaying great but now I've come to add the add as a friend
  functionality and I'm struggling.

  Every user in the users table has an id and I also have a table
  set up, users_users, to hold the self referential join. This table
  has a an id, a user_id and a friend_id.

  My user model contains the following...

  ?php
  class User extends AppModel {

     var $name = 'User';
     var $hasOne = 'Profile';
     var $hasMany = array('Status' = array('order' = 'Status.created
  DESC' ));

     var $hasAndBelongsToMany = array(
                     'Friend' = array('className' = 'User',
                                             'joinTable' = 'users_users',
                                             'foreignKey' = 'user_id',
                                             'associationForeignKey' = 
  'friend_id'
                     ),
                     'Admirer' = array('className' = 'User',
                                             'joinTable' = 'users_users',
                                             'foreignKey' = 'friend_id',
                                             'associationForeignKey' = 
  'user_id'
                     )
     );

  }
  ?

  And my users_controller contains...

  ?php
  class UsersController extends AppController {

  .
     function addfriend($friend_id = null) {
             $user_id = $this-Session-read('User');
             $this-data['User']['id'] = $user_id['id'];
             $this-data['Friend']['id'] = $friend_id;
             print_r($this-data);
             if($this-User-saveAll($this-data)){
                     echo 'saved';
             }else{
                     echo 'failed';
             }
     }
  .

  }
  ?

  Now I know that I've got the data element wrong for the database save
  because it isn't working and the SQL that's generated when you add a
  friend is incorrect.

  If you're logged in as a user with id 1 and want to become friends
  with the user with id 4 (http://localhost/network/users/addfriend/4),
  you get...

  data = Array ( [User] = Array ( [id] = 1) [Friend] = Array ( [id]
  = 4 ) )

  ...and the SQL from the CakePHP debug panel is...

  UPDATE `users` SET `id` = 1 WHERE `users`.`id` = 1

  Is anyone able to help me get this whole self-referential-HABTM
  relationship thing sorted?

  Thanks in advance.

  Dave.
--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Saving self-referential-HABTM relationships...

2008-09-30 Thread wallerdm
('action'='index'));
} else {
$this-Session-setFlash(__('The User could not 
be saved. Please,
try again.', true));
}
}
if (empty($this-data)) {
$this-data = $this-User-read(null, $id);
}
$friends = $this-User-Friend-find('list');
$this-set(compact('friends'));
}

function delete($id = null) {
if (!$id) {
$this-Session-setFlash(__('Invalid id for User', 
true));
$this-redirect(array('action'='index'));
}
if ($this-User-del($id)) {
$this-Session-setFlash(__('User deleted', true));
$this-redirect(array('action'='index'));
}
}

}
?


On Sep 30, 8:47 am, wallerdm [EMAIL PROTECTED] wrote:
 Thanks Bernardo - that makes sense. I think I might drop the concept
 of admirers and have an accepted state for friendships.

 This all works well when displaying user data, a user model
 automatically has data about previous friendships but I still can't
 add new friends. The best I can get when I play with the code is a
 drop of all entries in the users_users and in insert of the new
 friendship.

 Any ideas or tutorials that might help me out?

 Dave.

 On Sep 29, 6:23 pm, Bernardo Vieira [EMAIL PROTECTED] wrote:

  I imagine it should work if you use different join tables for each HABTM
  relationship. Regardless of cakephp this is what I imagine you'd need in
  this situation (imagine what would happen if a user is both a friend and
  an admirer of another user).

          var $hasAndBelongsToMany = array(
                          'Friend' = array('className' = 'User',
                                                  'joinTable' = 
  'friends_users',
                                                  'foreignKey' = 'friend_id',
                                                  'associationForeignKey' = 
  'user_id'
                          ),

                          'Admirer' = array('className' = 'User',
                                                  'joinTable' = 
  'admirers_users',
                                                  'foreignKey' = 
  'admirer_id',
                                                  'associationForeignKey' = 
  'user_id'
                          )
          );

  wallerdm wrote:
   Dear All,

   Forgive me if this is covered elsewhere. I'm playing with cakePHP as
   an alternative to Rails and am getting to grips with it by creating a
   really simple social network app.

   I've got some nice authentication and user profiles sorted and that's
   all displaying great but now I've come to add the add as a friend
   functionality and I'm struggling.

   Every user in the users table has an id and I also have a table
   set up, users_users, to hold the self referential join. This table
   has a an id, a user_id and a friend_id.

   My user model contains the following...

   ?php
   class User extends AppModel {

      var $name = 'User';
      var $hasOne = 'Profile';
      var $hasMany = array('Status' = array('order' = 'Status.created
   DESC' ));

      var $hasAndBelongsToMany = array(
                      'Friend' = array('className' = 'User',
                                              'joinTable' = 'users_users',
                                              'foreignKey' = 'user_id',
                                              'associationForeignKey' = 
   'friend_id'
                      ),
                      'Admirer' = array('className' = 'User',
                                              'joinTable' = 'users_users',
                                              'foreignKey' = 'friend_id',
                                              'associationForeignKey' = 
   'user_id'
                      )
      );

   }
   ?

   And my users_controller contains...

   ?php
   class UsersController extends AppController {

   .
      function addfriend($friend_id = null) {
              $user_id = $this-Session-read('User');
              $this-data['User']['id'] = $user_id['id'];
              $this-data['Friend']['id'] = $friend_id;
              print_r($this-data);
              if($this-User-saveAll($this-data)){
                      echo 'saved';
              }else{
                      echo 'failed';
              }
      }
   .

   }
   ?

   Now I know that I've got the data element wrong for the database save
   because it isn't working and the SQL that's generated when you add a
   friend is incorrect.

   If you're logged in as a user with id 1 and want to become friends
   with the user with id 4 (http://localhost/network/users/addfriend/4),
   you get...

   data = Array ( [User] = Array ( [id] = 1) [Friend] = Array ( [id]
   = 4 ) )

   ...and the SQL from the CakePHP debug panel

Saving self-referential-HABTM relationships...

2008-09-29 Thread wallerdm

Dear All,

Forgive me if this is covered elsewhere. I'm playing with cakePHP as
an alternative to Rails and am getting to grips with it by creating a
really simple social network app.

I've got some nice authentication and user profiles sorted and that's
all displaying great but now I've come to add the add as a friend
functionality and I'm struggling.

Every user in the users table has an id and I also have a table
set up, users_users, to hold the self referential join. This table
has a an id, a user_id and a friend_id.

My user model contains the following...

?php
class User extends AppModel {

var $name = 'User';
var $hasOne = 'Profile';
var $hasMany = array('Status' = array('order' = 'Status.created
DESC' ));

var $hasAndBelongsToMany = array(
'Friend' = array('className' = 'User',
'joinTable' = 'users_users',
'foreignKey' = 'user_id',
'associationForeignKey' = 
'friend_id'
),
'Admirer' = array('className' = 'User',
'joinTable' = 'users_users',
'foreignKey' = 'friend_id',
'associationForeignKey' = 
'user_id'
)
);

}
?

And my users_controller contains...

?php
class UsersController extends AppController {

.
function addfriend($friend_id = null) {
$user_id = $this-Session-read('User');
$this-data['User']['id'] = $user_id['id'];
$this-data['Friend']['id'] = $friend_id;
print_r($this-data);
if($this-User-saveAll($this-data)){
echo 'saved';
}else{
echo 'failed';
}
}
.

}
?

Now I know that I've got the data element wrong for the database save
because it isn't working and the SQL that's generated when you add a
friend is incorrect.

If you're logged in as a user with id 1 and want to become friends
with the user with id 4 (http://localhost/network/users/addfriend/4),
you get...

data = Array ( [User] = Array ( [id] = 1) [Friend] = Array ( [id]
= 4 ) )

...and the SQL from the CakePHP debug panel is...

UPDATE `users` SET `id` = 1 WHERE `users`.`id` = 1

Is anyone able to help me get this whole self-referential-HABTM
relationship thing sorted?

Thanks in advance.

Dave.

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---