与Doctrine添加朋友时,更改状态列的值

| 我现在开始使用Doctrine 2和Zend Framework,并且正在创建用户之间的友谊系统。 在“数据库的朋友”表中,我具有以下列:
User_id | Friend_id | Status
其中status是可以为-1、0或1的数字。 我使用的是多对多关系,更确切地说,是Doctrine文档中显示的关系:http://www.doctrine-project.org/docs/orm/2.0/en/reference/association-mapping .html#many-to-many-self-referencing 但是我(不幸的是)不知道如何修改表好友的status列的值。 :( 如何进行? 编辑:状态列在friends表中,而不在users表中。     
已邀请:
在这种情况下,您需要手动将联接表创建为实体:
<?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;
    }

}
...这是您可能要考虑的事情的粗略指南。     
class User
{
    private $status;

    public function setStatus($status) {
         $this->status = $status;
         return $this;
    }
}
将此添加到您的用户实体     
我已经设法解决了这个问题,这要归功于@Cobby关于连接表实体的详细说明。 我仍然对Doctrine还是陌生的,因此可以肯定地可以改进此代码,但这是我到目前为止所拥有的,我希望它对某人有用。 用户存储库:
<?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();
    }
}
友情实体:
<?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;

    /* ... */
一些服务类中的方法:
/**
 * 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;
}

/* */
    

要回复问题请先登录注册