Doctrine查询库,包含分组和最新记录

时间:2016-04-22 09:47:38

标签: php mysql doctrine-orm doctrine

我有一个表“table_b”,其中包含以下详细信息Table Sample

我想使用doctrine查询具有特定条件的输出。目前我正在使用下面的块进行查询。

$table_a= $em->getRepository('table_a')->findOneBy(['id'=>1]);
foreach($table_a->table_b as $records){
    echo $records->name. " : " . $records->value;
}

它将输出整个ArrayCollection。有没有办法根据最新的“创建日期”查询记录库,这是基于“外键表2”列的分组。

2 个答案:

答案 0 :(得分:1)

如果要使用本机Doctrine查询方法,则应将findOneByorder by参数一起使用。

findOneBy(['id' => 1], ['DateCreated' => 'desc'])

然后,你说结果是一个ArrayCollection,所以使用ArrayCollection::first()方法,你将获得最后创建的元素

编辑

想象一下,你有一个Group实体和一个Member实体。 groups table是您的table_a,成员表是您的table_b

实体描述应该是这样的:

class Group
{
    ...

    /**
     * @ORM\OneToMany(targetEntity="Group", mappedBy="member", cascade={"persist", "remove", "merge"})
     * @ORM\OrderBy({"dateCreated"="DESC"})
     */
    protected $members;

    ...

    public function __construct()
    {
        $members = new ArrayCollection();
    }

    // members handling accessors

    /**
     * @return ArrayCollection
     */
    public function getMembers()
    {
        return $this->members;
    }

    /**
     * @param $members
     *
     * @return $this
     */
    public function setMembers($members)
    {
        $this->members = new ArrayCollection();

        return $this->addMembers($members);
    }

    /**
     * @param $members
     *
     * @return $this
     */
    public function addMembers($members)
    {
        foreach ($members as $member)
        {
            $this->addMember($member);
        }

        return $this;
    }

    /**
     * @param Member $member
     *
     * @return $this
     */
    public function addMember(Member $member)
    {
        $this->members->add($member);
        $member->setGroup($this);

        return $this;
    }

    /**
     * @param Member $member
     *
     * @return $this
     */
    public function removeMember(Member $member)
    {
        if ($this->members->contains($member))
        {
            $this->members->removeElement($member);
        }

        return $this;
    }

    /**
     * @param $members
     *
     * @return $this
     */
    public function removeMembers($members)
    {
        foreach ($members as $member)
        {
            $this->removeMember($member);
        }

        return $this;
    }
}

成员实体:

class Member
{
    /**
     * @ORM\ManyToOne(targetEntity="Group", inversedBy="members")
     * @ORM\JoinColumn(name="group_id", referencedColumnName="id", onDelete="CASCADE")
     */
    protected $group;

    /**
     * @ORM\Column(type="datetime", name="date_created")
     */
    protected $dateCreated;

    /**
     * @return Group
     */
    public function getGroup()
    {
        return $this->group;
    }

    /**
     * @param Group $group
     *
     * @return $this
     */
    public function setGroup(Group $group)
    {
        $this->group = $group;

        return $this;
    }
}

现在,我们有一个小组,其中有dateCreated个有序的成员集合。

示例1:您希望获取为给定组创建的最后一个成员

$group = $em->getRepository(Group::class)->findOneBy(['id' => 1]);
$lastMember = $group->getMembers()->first();

示例2:您希望在2014-01-30:

创建所有成员
$members = $group->getMembers()->filter(function (Member $member) {
    return ($member->getDateCreated->format('Y-m-d') == '2014-01-30');
});

那是所有人!

PS:我还没有测试过这段代码

答案 1 :(得分:0)

在TableRepository中:

public function getLatestRecord()
{
    return $this->getEntityManager()
        ->createQuery('SELECT t FROM MyBundle:MyEntity t GROUP BY t.table2NameField ORDER BY t.created DESC')
        ->setMaxResults(1)
        ->getOneOrNullResult();
}