Problems saving HABTM data (users_users)...

2008-11-14 Thread wallerdm

Dear All,

I'm still having trouble with self referential HABTM relationships but
I'm almost there. I have quite a neat setup now using an extended
associations behaviour (http://bakery.cakephp.org/articles/view/add-
delete-habtm-behavior) but it's still doing funny things.

Essentially, I have a load of users who can be friends with each
other. When you ask someone to be your friend they instantly get added
as a friend and they can confirm or deny. To facilitate this
relationship I have a join table:

users_users
id | user_id | friend_id | accepted

The extra field "accepted" keeps track of which requests are pending
(0), accepted (1) or denied (2).

Everything works fine the first time you add a friend but when you
start to add more friends, every "accepted" value for every entry
where "user_id" matches the user gets set to 0. It's all very strange.

I'm not sure if anyone will be able to help but thought it worth
posting.

Thanks in advance, Dave.

Users model...

 array('className' => 'User',
  'joinTable' => 
'users_users',
  'foreignKey' => 
'user_id',
  
'associationForeignKey' => 'friend_id',
  'unique' => true,
  'conditions' => '',
  'fields' => '',
  'order' => '',
  'limit' => '',
  'offset' => '',
  'finderQuery' => '',
  'deleteQuery' => '',
  'insertQuery' => ''
),
'admirer' => array('className' => 'User',
  'joinTable' => 
'users_users',
  'foreignKey' => 
'friend_id',
  
'associationForeignKey' => 'user_id',
  'unique' => true,
  'conditions' => '',
  'fields' => '',
  'order' => '',
  'limit' => '',
  'offset' => '',
  'finderQuery' => '',
  'deleteQuery' => '',
  'insertQuery' => ''
)
);

}
?>


Users controller...

.
function friend(){
$temp = $this->Session->read();
$userid = $temp['Auth']['User']['id'];
if($this->params['named']['type']=='reply'){
//update friend request...
$this->User->habtmUpdate('admirer', $userid, 
$this->params['named']
['friendid'], array('accepted'=>$this->params['named']['status']));
if($this->params['named']['status']==1){
//reciprocate friendship...
$this->User->habtmAdd('friend', $userid, 
$this->params['named']
['friendid'], array('accepted'=>1));
$this->Session->setFlash(__('Friendship 
reciprocated', true));
}else{
$this->Session->setFlash(__('Freindship 
ignored', true));
}

$this->redirect(array('controller'=>'users','action'=>'view',
$userid));
}else{
//create new friendship...
$this->User->habtmAdd('friend', $userid, 
$this->params['named']
['friendid'], array('accepted'=>$this->params['named']['status']));
$this->Session->setFlash(__('User has been added as a 
friend',
true));

$this->redirect(array('controller'=>'users','action'=>'view',$this-
>params['named']['friendid']));
}
}
.


URL to add a new friend...

http://localhost/project/users/friend/friendid:6/status:0/type:new


URL to accept a friend request...

http://localhost/project/users/friend/friendid:6/status:1/type:reply


URL to refuse a friend request...

http://localhost/project/users/friend/friendid:6/status:2/type:reply

--~--~-~--~~~---~--~~
You received this message because you are subscribe

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
#x27;The User could not 
be saved. Please,
try again.', true));
}
}
$friends = $this->User->Friend->find('list');
$this->set(compact('friends'));
}

function edit($id = null) {
if (!$id && empty($this->data)) {
$this->Session->setFlash(__('Invalid User', true));
$this->redirect(array('action'=>'index'));
}
if (!empty($this->data)) {
if ($this->User->save($this->data)) {
$this->Session->setFlash(__('The User has been 
saved', true));
$this->redirect(array('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...
>
> > >  > > class User extends AppModel {
>
> > >    var $name = 'User';
> >

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



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...

 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...

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