Ha Ha!
Started again from scratch using the Cake Bake console and it all
seems to be working. A few errors again when adding friends but should
be fixable.
In case it's of use to anyone here's how it's all set up now...
DATABASE...
Users table... id | username | password
Profiles table... id | user_id | forename | surname | etc
Statuses table... id | user_id | status_text | created_at
Users_users table... id | user_id | friend_id | approved
USER MODEL...
<?php
class User extends AppModel {
var $name = 'User';
var $validate = array(
'username' => array('alphanumeric'),
'password' => array('alphanumeric')
);
//The Associations below have been created with all possible keys,
those that are not needed can be removed
var $hasOne = array(
'Profile' => array('className' => 'Profile',
'foreignKey' =>
'user_id',
'dependent' =>
false,
'conditions' =>
'',
'fields' => '',
'order' => ''
)
);
var $hasMany = array(
'Status' => array('className' => 'Status',
'foreignKey' =>
'user_id',
'dependent' =>
false,
'conditions' =>
'',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' =>
'',
'finderQuery'
=> '',
'counterQuery'
=> ''
)
);
var $hasAndBelongsToMany = array(
'friend' => array('className' => 'User',
'joinTable' => 'users_users',
'foreignKey' => 'user_id',
'associationForeignKey' =>
'friend_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
}
?>
USERS CONTROLLER...
<?php
class UsersController extends AppController {
var $name = 'Users';
var $helpers = array('Html', 'Form');
function index() {
$this->User->recursive = 0;
$this->set('users', $this->paginate());
}
function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid User.', true));
$this->redirect(array('action'=>'index'));
}
$this->set('user', $this->User->read(null, $id));
}
function add() {
if (!empty($this->data)) {
$this->User->create();
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));
}
}
$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...
>
> > > <?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 [email protected]
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
-~----------~----~----~----~------~----~------~--~---