doushang1964 2014-09-19 23:21
浏览 46
已采纳

在学说中加载相关实体的子集

I am not sure if what I want to do is possible or not, but I can't find any documentation on it. Hopefully I am just looking in the wrong places.

What I would like to do is load an object that will always load a subset of a related object. some background - I want to keep a history of the contacts that a user deletes, so when a contact is deleted, I set a isDeleted column in the db to true. I will rarely want to load the deleted contacts, so I will worry about that later. But when I load a user, and then load the contacts (in my case by serializing the whole user object), all of the contacts are loaded including the deleted ones. So I would like to only load the contacts that have not been deleted yet. This is what I have right now, and I don't know why it is not working.

use Doctrine\Common\Collections\Criteria,
    Doctrine\Common\Collections\ArrayCollection;

class User{
/**
 * @ORM\OneToMany(targetEntity="Contacts", mappedBy="user", cascade={"persist"}, fetch="EXTRA_LAZY")
 * @ORM\JoinColumn(name="contact_id", referencedColumnName="contact_id")
 */
private $contacts;

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

/**
 * Get contacts
 *
 * @return \Doctrine\Common\Collections\ArrayCollection
 */
public function getContacts()
{
    // This is where I am filtering the contacts
    $criteria = Criteria::create()
    ->where(Criteria::expr()->eq("isDeleted", false));
    return $this->contacts->matching($criteria);
}

And then I am loading and serializing a user using the JMS Serializer and FOSRest in my Controller.

public function getUserAction($slug){
    $data = $this->getDoctrine()->getRepository('MyCoreBundle:User')->find($slug);
    $view = $this->view($data, 200);

    return $this->handleView($view);
}

And the result is the following JSON:

{
userId: 7,
creationDate: "2014-07-28T22:05:43+0000",
isActive: true,
username: "myUser",
contacts: [
    {
        contactId: 23,
        firstName: "Jim",
        lastName: "Bin",
        email: "zzz@zzz.com",
        phone: "1231231231",
        isDeleted: true
    }
]
}

I am not sure why this is still loading the deleted user. If I do the filtering in my controller instead of the Entity class, then the filtering works. But I don't want to have to do that everywhere in my application that a contact may be loaded. If I could get something working in the Entity class, that would be ideal. Has anyone done something similar that could work?

I'm using Symfony 2.3

  • 写回答

2条回答 默认 最新

  • dongyong1942 2014-09-20 01:39
    关注

    I'd highly recommend the Soft-Deleteable extension. You'd have to use a deletedAt field which is null when the object is not deleted, and a timestamp if deleted.

    It goes like this:

    /**
     * @ORM\Entity
     * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
     */
    class User
    {
        /**
         * @ORM\Column(name="deletedAt", type="datetime", nullable=true)
         */
        private $deletedAt;
    
        public function getDeletedAt()
        {
            return $this->deletedAt;
        }
    
        public function setDeletedAt($deletedAt)
        {
            $this->deletedAt = $deletedAt;
        }
    
    }
    

    Have a look at this extension here: Soft-Deleteable Extension

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献