查找所有 post 并按 post 中的最新消息排序。 Doctrine2 和 ZF2

Find all posts and order by latest message in post. Doctrine2 and ZF2

我有一个问题我想了很久,不知道怎么解决...你能指导我正确的方向或者给我一些解决问题的建议吗?

我有一个咨询table,每一行都有很多消息。我想检索符合特定条件的所有咨询,然后 return 按标签排序的列表,以及 OneToMany 关系中的最后一项...

这是我现在的代码:

public function indexAction()
{
    $em = $this->getEntityManager();
    /** @var EntityRepository $repo */
    $repo = $em->getRepository('Common\Entity\Konsultasjon');
    $meldinger = $repo->findBy([
            'kundeid' => '1',
            'state' => '1',
            'skjult' => '0'
            ],[
                'tag' => 'ASC',
            ]
        );


    return new ViewModel(['meldinger' => $meldinger]);
}

这是我的 Konsultasjon 实体:

<?php

namespace Common\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Konsultasjon
 *
 * @ORM\Table(name="konsultasjon", uniqueConstraints={@ORM\UniqueConstraint(name="id_UNIQUE", columns={"konsultasjonid"})}, indexes={@ORM\Index(name="state", columns={"state"}), @ORM\Index(name="kundeid", columns={"kundeid"}), @ORM\Index(name="tag", columns={"tag"})})
 * @ORM\Entity
 */
class Konsultasjon
{
/**
 * @var integer
 *
 * @ORM\Column(name="konsultasjonid", type="integer", precision=0, scale=0, nullable=false, unique=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $konsultasjonid;

/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\OneToMany(targetEntity="Common\Entity\Meldinger", mappedBy="konsulatsjon", cascade={"persist"}, fetch="EAGER")
 * @ORM\JoinColumn(name="meldinger", referencedColumnName="meldingerid")
 */
private $meldinger;

/**
 * @var integer
 *
 * @ORM\Column(name="sourceaddr", type="bigint", precision=0, scale=0, nullable=true, unique=false)
 */
private $sourceaddr;

/**
 * @var string
 *
 * @ORM\Column(name="prefix", type="text", precision=0, scale=0, nullable=true, unique=false)
 */
private $prefix;

/**
 * @var string
 *
 * @ORM\Column(name="subprefix", type="text", precision=0, scale=0, nullable=true, unique=false)
 */
private $subprefix;

/**
 * @var integer
 *
 * @ORM\Column(name="campaignid", type="integer", precision=0, scale=0, nullable=true, unique=false)
 */
private $campaignid;

/**
 * @var integer
 *
 * @ORM\Column(name="destinationaddr", type="integer", precision=0, scale=0, nullable=true, unique=false)
 */
private $destinationaddr;

/**
 * @var integer
 *
 * @ORM\Column(name="kundeid", type="integer", precision=0, scale=0, nullable=true, unique=false)
 */
private $kundeid;

/**
 * @var integer
 *
 * @ORM\Column(name="state", type="integer", precision=0, scale=0, nullable=true, unique=false)
 */
private $state;

/**
 * @var integer
 *
 * @ORM\Column(name="status", type="integer", precision=0, scale=0, nullable=true, unique=false)
 */
private $status;

/**
 * @var integer
 *
 * @ORM\Column(name="blocked", type="integer", precision=0, scale=0, nullable=true, unique=false)
 */
private $blocked;

/**
 * @var integer
 *
 * @ORM\Column(name="skjult", type="integer", precision=0, scale=0, nullable=true, unique=false)
 */
private $skjult;

/**
 * @var integer
 *
 * @ORM\Column(name="tag", type="integer", precision=0, scale=0, nullable=false, unique=false)
 */
private $tag;



/**
 * Constructor
 */
public function __construct()
{
    $this->meldinger = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * Get konsultasjonid
 *
 * @return integer 
 */
public function getKonsultasjonid()
{
    return $this->konsultasjonid;
}

/**
 * Set sourceaddr
 *
 * @param integer $sourceaddr
 * @return Konsultasjon
 */
public function setSourceaddr($sourceaddr)
{
    $this->sourceaddr = $sourceaddr;

    return $this;
}

/**
 * Get sourceaddr
 *
 * @return integer 
 */
public function getSourceaddr()
{
    return $this->sourceaddr;
}

/**
 * Set prefix
 *
 * @param string $prefix
 * @return Konsultasjon
 */
public function setPrefix($prefix)
{
    $this->prefix = $prefix;

    return $this;
}

/**
 * Get prefix
 *
 * @return string 
 */
public function getPrefix()
{
    return $this->prefix;
}

/**
 * Set subprefix
 *
 * @param string $subprefix
 * @return Konsultasjon
 */
public function setSubprefix($subprefix)
{
    $this->subprefix = $subprefix;

    return $this;
}

/**
 * Get subprefix
 *
 * @return string 
 */
public function getSubprefix()
{
    return $this->subprefix;
}

/**
 * Set campaignid
 *
 * @param integer $campaignid
 * @return Konsultasjon
 */
public function setCampaignid($campaignid)
{
    $this->campaignid = $campaignid;

    return $this;
}

/**
 * Get campaignid
 *
 * @return integer 
 */
public function getCampaignid()
{
    return $this->campaignid;
}

/**
 * Set destinationaddr
 *
 * @param integer $destinationaddr
 * @return Konsultasjon
 */
public function setDestinationaddr($destinationaddr)
{
    $this->destinationaddr = $destinationaddr;

    return $this;
}

/**
 * Get destinationaddr
 *
 * @return integer 
 */
public function getDestinationaddr()
{
    return $this->destinationaddr;
}

/**
 * Set kundeid
 *
 * @param integer $kundeid
 * @return Konsultasjon
 */
public function setKundeid($kundeid)
{
    $this->kundeid = $kundeid;

    return $this;
}

/**
 * Get kundeid
 *
 * @return integer 
 */
public function getKundeid()
{
    return $this->kundeid;
}

/**
 * Set state
 *
 * @param integer $state
 * @return Konsultasjon
 */
public function setState($state)
{
    $this->state = $state;

    return $this;
}

/**
 * Get state
 *
 * @return integer 
 */
public function getState()
{
    return $this->state;
}

/**
 * Set status
 *
 * @param integer $status
 * @return Konsultasjon
 */
public function setStatus($status)
{
    $this->status = $status;

    return $this;
}

/**
 * Get status
 *
 * @return integer 
 */
public function getStatus()
{
    return $this->status;
}

/**
 * Set blocked
 *
 * @param integer $blocked
 * @return Konsultasjon
 */
public function setBlocked($blocked)
{
    $this->blocked = $blocked;

    return $this;
}

/**
 * Get blocked
 *
 * @return integer 
 */
public function getBlocked()
{
    return $this->blocked;
}

/**
 * Set skjult
 *
 * @param integer $skjult
 * @return Konsultasjon
 */
public function setSkjult($skjult)
{
    $this->skjult = $skjult;

    return $this;
}

/**
 * Get skjult
 *
 * @return integer 
 */
public function getSkjult()
{
    return $this->skjult;
}

/**
 * Set tag
 *
 * @param integer $tag
 * @return Konsultasjon
 */
public function setTag($tag)
{
    $this->tag = $tag;

    return $this;
}

/**
 * Get tag
 *
 * @return integer 
 */
public function getTag()
{
    return $this->tag;
}

/**
 * Add meldinger
 *
 * @param \Common\Entity\Meldinger $meldinger
 * @return Konsultasjon
 */
public function addMeldinger(\Common\Entity\Meldinger $meldinger)
{
    $this->meldinger[] = $meldinger;

    return $this;
}

/**
 * Remove meldinger
 *
 * @param \Common\Entity\Meldinger $meldinger
 */
public function removeMeldinger(\Common\Entity\Meldinger $meldinger)
{
    $this->meldinger->removeElement($meldinger);
}

/**
 * Get meldinger
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getMeldinger()
{
    return $this->meldinger;
}
}

郑重声明,我的 Meldinger 实体:

<?php

namespace Common\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Meldinger
 *
 * @ORM\Table(name="meldinger",options={"collate"="utf8_swedish_ci"}, uniqueConstraints={@ORM\UniqueConstraint(name="id", columns={"meldingerid"})}, indexes={@ORM\Index(name="konsultasjon", columns={"konsulatsjon"}), @ORM\Index(name="state", columns={"state"}), @ORM\Index(name="kundeid", columns={"kundeid"}), @ORM\Index(name="type", columns={"type"}), @ORM\Index(name="dato", columns={"dato"})})
 * @ORM\Entity
 */
class Meldinger
{
    /**
     * @var integer
     *
     * @ORM\Column(name="meldingerid", type="integer", precision=0, scale=0, nullable=false, unique=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $meldingerid;


    /**
     * @var string
     *
     * @ORM\Column(name="sourceaddr", type="text", precision=0, scale=0, nullable=false, unique=false)
     */
    private $sourceaddr;

    /**
     * @var integer
     *
     * @ORM\Column(name="destinationaddr", type="bigint", precision=0, scale=0, nullable=false, unique=false)
     */
    private $destinationaddr;

    /**
     * @var string
     *
     * @ORM\Column(name="prefix", type="text", precision=0, scale=0, nullable=true, unique=false)
     */
    private $prefix;

    /**
     * @var string
     *
     * @ORM\Column(name="message", type="text", precision=0, scale=0, nullable=false, unique=false)
     */
    private $message;

    /**
     * @var integer
     *
     * @ORM\Column(name="refno", type="integer", precision=0, scale=0, nullable=true, unique=false)
     */
    private $refno;

    /**
     * @var integer
     *
     * @ORM\Column(name="retrycount", type="integer", precision=0, scale=0, nullable=true, unique=false)
     */
    private $retrycount;

    /**
     * @var integer
     *
     * @ORM\Column(name="operator", type="integer", precision=0, scale=0, nullable=true, unique=false)
     */
    private $operator;

    /**
     * @var integer
     *
     * @ORM\Column(name="state", type="integer", precision=0, scale=0, nullable=true, unique=false)
     */
    private $state;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="dato", type="datetime", precision=0, scale=0, nullable=false, unique=false)
     */
    private $dato;

    /**
     * @var integer
     *
     * @ORM\Column(name="replypathid", type="integer", precision=0, scale=0, nullable=true, unique=false)
     */
    private $replypathid;

    /**
     * @var integer
     *
     * @ORM\Column(name="replypathvalue", type="integer", precision=0, scale=0, nullable=true, unique=false)
     */
    private $replypathvalue;

    /**
     * @var string
     *
     * @ORM\Column(name="referenceid", type="text", precision=0, scale=0, nullable=true, unique=false)
     */
    private $referenceid;

    /**
     * @var string
     *
     * @ORM\Column(name="requesttype", type="text", precision=0, scale=0, nullable=true, unique=false)
     */
    private $requesttype;

    /**
     * @var integer
     *
     * @ORM\ManyToOne(targetEntity="Konsultasjon", inversedBy="meldinger")
     * @ORM\JoinColumn(name="konsulatsjon", referencedColumnName="konsultasjonid")
     */
    private $konsulatsjon;

    /**
     * @var integer
     *
     * @ORM\Column(name="campaignid", type="integer", precision=0, scale=0, nullable=true, unique=false)
     */
    private $campaignid;

    /**
     * @var integer
     *
     * @ORM\Column(name="brukerid", type="integer", precision=0, scale=0, nullable=true, unique=false)
     */
    private $brukerid;

    /**
     * @var integer
     *
     * @ORM\Column(name="kundeid", type="integer", precision=0, scale=0, nullable=true, unique=false)
     */
    private $kundeid;

    /**
     * @var integer
     *
     * @ORM\Column(name="type", type="integer", precision=0, scale=0, nullable=true, unique=false)
     */
    private $type;

    /**
     * @var integer
     *
     * @ORM\Column(name="pricegroup", type="integer", precision=0, scale=0, nullable=true, unique=false)
     */
    private $pricegroup;

    /**
     * @var string
     *
     * @ORM\Column(name="tilbakemelding", type="text", precision=0, scale=0, nullable=true, unique=false)
     */
    private $tilbakemelding;

    /**
     * @var string
     *
     * @ORM\Column(name="status_kode", type="text", precision=0, scale=0, nullable=true, unique=false)
     */
    private $statusKode;

    /**
     * @var string
     *
     * @ORM\Column(name="status_beskrivelse", type="text", precision=0, scale=0, nullable=true, unique=false)
     */
    private $statusBeskrivelse;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="status_timestamp", type="datetime", precision=0, scale=0, nullable=true, unique=false)
     */
    private $statusTimestamp;


    /**
     * Get meldingerid
     *
     * @return integer 
     */
    public function getMeldingerid()
    {
        return $this->meldingerid;
    }

    /**
     * Set sourceaddr
     *
     * @param string $sourceaddr
     * @return Meldinger
     */
    public function setSourceaddr($sourceaddr)
    {
        $this->sourceaddr = $sourceaddr;

        return $this;
    }

    /**
     * Get sourceaddr
     *
     * @return string 
     */
    public function getSourceaddr()
    {
        return $this->sourceaddr;
    }

    /**
     * Set destinationaddr
     *
     * @param integer $destinationaddr
     * @return Meldinger
     */
    public function setDestinationaddr($destinationaddr)
    {
        $this->destinationaddr = $destinationaddr;

        return $this;
    }

    /**
     * Get destinationaddr
     *
     * @return integer 
     */
    public function getDestinationaddr()
    {
        return $this->destinationaddr;
    }

    /**
     * Set prefix
     *
     * @param string $prefix
     * @return Meldinger
     */
    public function setPrefix($prefix)
    {
        $this->prefix = $prefix;

        return $this;
    }

    /**
     * Get prefix
     *
     * @return string 
     */
    public function getPrefix()
    {
        return $this->prefix;
    }

    /**
     * Set message
     *
     * @param string $message
     * @return Meldinger
     */
    public function setMessage($message)
    {
        $this->message = $message;

        return $this;
    }

    /**
     * Get message
     *
     * @return string 
     */
    public function getMessage()
    {
        return $this->message;
    }

    /**
     * Set refno
     *
     * @param integer $refno
     * @return Meldinger
     */
    public function setRefno($refno)
    {
        $this->refno = $refno;

        return $this;
    }

    /**
     * Get refno
     *
     * @return integer 
     */
    public function getRefno()
    {
        return $this->refno;
    }

    /**
     * Set retrycount
     *
     * @param integer $retrycount
     * @return Meldinger
     */
    public function setRetrycount($retrycount)
    {
        $this->retrycount = $retrycount;

        return $this;
    }

    /**
     * Get retrycount
     *
     * @return integer 
     */
    public function getRetrycount()
    {
        return $this->retrycount;
    }

    /**
     * Set operator
     *
     * @param integer $operator
     * @return Meldinger
     */
    public function setOperator($operator)
    {
        $this->operator = $operator;

        return $this;
    }

    /**
     * Get operator
     *
     * @return integer 
     */
    public function getOperator()
    {
        return $this->operator;
    }

    /**
     * Set state
     *
     * @param integer $state
     * @return Meldinger
     */
    public function setState($state)
    {
        $this->state = $state;

        return $this;
    }

    /**
     * Get state
     *
     * @return integer 
     */
    public function getState()
    {
        return $this->state;
    }

    /**
     * Set dato
     *
     * @param \DateTime $dato
     * @return Meldinger
     */
    public function setDato($dato)
    {
        $this->dato = $dato;

        return $this;
    }

    /**
     * Get dato
     *
     * @return \DateTime 
     */
    public function getDato()
    {
        return $this->dato;
    }

    /**
     * Set replypathid
     *
     * @param integer $replypathid
     * @return Meldinger
     */
    public function setReplypathid($replypathid)
    {
        $this->replypathid = $replypathid;

        return $this;
    }

    /**
     * Get replypathid
     *
     * @return integer 
     */
    public function getReplypathid()
    {
        return $this->replypathid;
    }

    /**
     * Set replypathvalue
     *
     * @param integer $replypathvalue
     * @return Meldinger
     */
    public function setReplypathvalue($replypathvalue)
    {
        $this->replypathvalue = $replypathvalue;

        return $this;
    }

    /**
     * Get replypathvalue
     *
     * @return integer 
     */
    public function getReplypathvalue()
    {
        return $this->replypathvalue;
    }

    /**
     * Set referenceid
     *
     * @param string $referenceid
     * @return Meldinger
     */
    public function setReferenceid($referenceid)
    {
        $this->referenceid = $referenceid;

        return $this;
    }

    /**
     * Get referenceid
     *
     * @return string 
     */
    public function getReferenceid()
    {
        return $this->referenceid;
    }

    /**
     * Set requesttype
     *
     * @param string $requesttype
     * @return Meldinger
     */
    public function setRequesttype($requesttype)
    {
        $this->requesttype = $requesttype;

        return $this;
    }

    /**
     * Get requesttype
     *
     * @return string 
     */
    public function getRequesttype()
    {
        return $this->requesttype;
    }

    /**
     * Set konsulatsjon
     *
     * @param integer $konsulatsjon
     * @return Meldinger
     */
    public function setKonsulatsjon($konsulatsjon)
    {
        $this->$konsulatsjon = $konsulatsjon;

        return $this;
    }

    /**
     * Get konsulatsjon
     *
     * @return integer 
     */
    public function getKonsulatsjon()
    {
        return $this->$konsulatsjon;
    }

    /**
     * Set campaignid
     *
     * @param integer $campaignid
     * @return Meldinger
     */
    public function setCampaignid($campaignid)
    {
        $this->campaignid = $campaignid;

        return $this;
    }

    /**
     * Get campaignid
     *
     * @return integer 
     */
    public function getCampaignid()
    {
        return $this->campaignid;
    }

    /**
     * Set brukerid
     *
     * @param integer $brukerid
     * @return Meldinger
     */
    public function setBrukerid($brukerid)
    {
        $this->brukerid = $brukerid;

        return $this;
    }

    /**
     * Get brukerid
     *
     * @return integer 
     */
    public function getBrukerid()
    {
        return $this->brukerid;
    }

    /**
     * Set kundeid
     *
     * @param integer $kundeid
     * @return Meldinger
     */
    public function setKundeid($kundeid)
    {
        $this->kundeid = $kundeid;

        return $this;
    }

    /**
     * Get kundeid
     *
     * @return integer 
     */
    public function getKundeid()
    {
        return $this->kundeid;
    }

    /**
     * Set type
     *
     * @param integer $type
     * @return Meldinger
     */
    public function setType($type)
    {
        $this->type = $type;

        return $this;
    }

    /**
     * Get type
     *
     * @return integer 
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * Set pricegroup
     *
     * @param integer $pricegroup
     * @return Meldinger
     */
    public function setPricegroup($pricegroup)
    {
        $this->pricegroup = $pricegroup;

        return $this;
    }

    /**
     * Get pricegroup
     *
     * @return integer 
     */
    public function getPricegroup()
    {
        return $this->pricegroup;
    }

    /**
     * Set tilbakemelding
     *
     * @param string $tilbakemelding
     * @return Meldinger
     */
    public function setTilbakemelding($tilbakemelding)
    {
        $this->tilbakemelding = $tilbakemelding;

        return $this;
    }

    /**
     * Get tilbakemelding
     *
     * @return string 
     */
    public function getTilbakemelding()
    {
        return $this->tilbakemelding;
    }

    /**
     * Set statusKode
     *
     * @param string $statusKode
     * @return Meldinger
     */
    public function setStatusKode($statusKode)
    {
        $this->statusKode = $statusKode;

        return $this;
    }

    /**
     * Get statusKode
     *
     * @return string 
     */
    public function getStatusKode()
    {
        return $this->statusKode;
    }

    /**
     * Set statusBeskrivelse
     *
     * @param string $statusBeskrivelse
     * @return Meldinger
     */
    public function setStatusBeskrivelse($statusBeskrivelse)
    {
        $this->statusBeskrivelse = $statusBeskrivelse;

        return $this;
    }

    /**
     * Get statusBeskrivelse
     *
     * @return string 
     */
    public function getStatusBeskrivelse()
    {
        return $this->statusBeskrivelse;
    }

    /**
     * Set statusTimestamp
     *
     * @param \DateTime $statusTimestamp
     * @return Meldinger
     */
    public function setStatusTimestamp($statusTimestamp)
    {
        $this->statusTimestamp = $statusTimestamp;

        return $this;
    }

    /**
     * Get statusTimestamp
     *
     * @return \DateTime 
     */
    public function getStatusTimestamp()
    {
        return $this->statusTimestamp;
    }
}

虽然希望有一个正确的方法来解决这个问题(在我的数据库中对其进行排序),但这里有一个对其进行排序的技巧:

/**
 * @param \Common\Entity\Meldinger[] $meldinger
 * @return \Common\Entity\Meldinger[]
 */
public function sortMeldinger(array $meldinger)
{
    $sort = array();
    /**
     * @var \Common\Entity\Konsultasjon $v
     */
    foreach ($meldinger as $k => $v) {
        $sort['tag'][$k] = $v->getTag();
        /** @var \Common\Entity\Meldinger $last */
        $last = $v->getMeldinger()->last();
        $sort['dato'][$k] = $last->getDato(); //end($v['meldinger'])['dato'];
    }
    array_multisort($sort['tag'], SORT_ASC, $sort['dato'], SORT_ASC, $meldinger);

    return $meldinger;
}

这是按我的消息日期排序的,但我也可以按 ID 排序...

而不是基础 entity repository, I would write a DQL 的内置 findBy() 方法来完成这样的工作。

例如:

$em = $this->getEntityManager();
$dql = 'SELECT K, M
        FROM Common\Entity\Konsultasjon K
        LEFT JOIN Common\Entity\Meldinger M
        WHERE K.kundeid = :kid AND
              K.state = :st AND
              K.skjult = :skj
        ORDER BY K.tag DESC,
                 M.dato DESC';

$query = $em->createQuery($dql);
$query->setParameter('kid', 1)
      ->setParameter('st', 0)
      ->setParameter('skj', 0);

 $pagination = new \Doctrine\ORM\Tools\Pagination\Paginator($query);
 $adapter    = new \DoctrineORMModule\Paginator\Adapter\DoctrinePaginator($pagination);
 $paginator  = new \Zend\Paginator\Paginator($adapter);

 $paginator->setCurrentPageNumber(1)
           ->setDefaultItemCountPerPage(50);

 return new ViewModel(['paginator' => $paginator]);

在这里使用分页器并将其传递到视图层是一项要求,因为您在两个表中都有很多记录。将此逻辑移动到自定义存储库(例如 KonsultasjonRepository)也是一种很好的做法。

希望对您有所帮助。