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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀