开发者

Change the value of status column when adding a friend with Doctrine

I'm starting now with Doctrine 2 and Zend Framework, and I'm creat开发者_运维百科ing a system of friendship among the users. In the table Friends of my database, I have the following columns:

User_id | Friend_id | Status

Where status is a number that can be -1, 0 or 1.

I'm using the relationship many-to-many, more specifically, the same shown in the documentation of Doctrine: http://www.doctrine-project.org/docs/orm/2.0/en/reference/association-mapping.html#many-to-many-self-referencing

But I (unfortunately) do not know how to modify the value of status column of the table friends. :(

How to proceed?

EDIT: The status column is inside the friends table, and not in the users table.


In this case you need to manually create the join table as an Entity:

<?php

/**
 * @Entity
 */
class UserFriend
{   

    const STATUS_NORMAL = 1;
    const STATUS_BANNED = 2;
    const STATUS_ANOTHER_STATUS = 4;

    /**
     * @ManyToOne(targetEntity="User", inversedBy="userFriends")
     */
    private $user;

    /**
     * @ManyToOne(targetEntity="User")
     */
    private $friend;

    /**
     * @Column(type="integer");
     */
    private $status = self::STATUS_NORNAL;

    public function __construct(User $user, User $friend, $status)
    {
        $this->setUser($user);
        $this->setFriend($friend);
        $this->setStatus($status);
    }

    // ... getters and setters


}

/**
 * @Entity
 */
class User
{

    /**
     * @OneToMany(targetEntity="UserFriend", mappedBy="user")
     */
    private $userFriends;

    public function __construct()
    {
        $this->userFriends = new ArrayCollection();
    }

    public function addFriend(User $friend, $status)
    {
        $this->userFriends->add(new UserFriend($this, $friend, $status));
    }

    public function getFriends()
    {
        $friends = array();
        foreach($this->userFriends as $userFriend){
            $friends[] = $userFriend->getFriend();
        }

        return $friends;
    }

}

...That is a rough guide as to something you might consider.


class User
{
    private $status;

    public function setStatus($status) {
         $this->status = $status;
         return $this;
    }
}

add this to your User entity


I've managed to solve this problem, thanks to @Cobby for the explication about the entity for the Join Table.

I'm still new to Doctrine, so surely this code can be improved, but it is what I have so far, and I hope it is useful to someone.

User Repository:

<?php 
class UserRepository extends EntityRepository {

    public function getFriends( \Entity\User $user, $status = Friends::STATUS_ACCEPTED )
    {
        return $this->getEntityManager()->createQuery( 'SELECT f FROM Entities\Friends f WHERE f.user = ?0 AND f.status = ?1' )
        ->setParameters( array( $user->getId(), 2 => $status ) )
        ->getResult();
    }
}

Friendship Entity:

<?php
/**
 * Friend Entity
 * 
 * @Entity
 * @Table(
 *      name="friendship",
 *      uniqueConstraints={
 *          @UniqueConstraint(name="UNIQ_FRIENDSHIP", columns={"user_id", "friend_id"})
 *      }
 * )
 */
class Friendship
{

    const STATUS_REQUESTED  =  0;
    const STATUS_ACCEPTED   =  1;
    const STATUS_REJECTED   = -1;
    const STATUS_BLOCKED    = -2;
    const STATUS_IGNORED    = -3;

    /**
     * @Id @Column(type="integer")
     * @GeneratedValue
     */
    private $id;

    /** @Column(type="integer") */
    private $status;

    /**
     * @ManyToOne(targetEntity="User", inversedBy="userFriends")
     */
    private $user;

    /**
     * @ManyToOne(targetEntity="User")
     */
    private $friend;

    /* ... */

Method inside some service class:

/**
 * Creates a friendship between two users
 * @param User $user
 * @param User $friend
 * @param integer $status
 * 
 * @return Friends
 */
public function createOrUpdateFriendship( User $user, User $friend, $status )
{
    //friendshipt already exists?
    $friendship = $this->getRepository( 'Friends' )->findOneBy(array(
        'user' => $user->getId(),
        'friend' => $friend->getId()
    ));

    if( $friendship ) {

        $friendship->setStatus($status);

    } else {

        $entityManager = $this->getEntityManager();
        $friendship = new Friends( $user, $friend, $status );

        $entityManager->persist( $friendship );
        $entityManager->flush();
    }

    return $friendship;
}

/* */
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜