2017-04-07 6 views
1

でDQLクエリの結果を取得します。私はそのようなネストされた方法で結果を生成することをそれを達成:私はこれを行うことができますか、私は自分自身を実装する必要があり、内部Doctrineのメカニズムが存在する場合教義は、私は次のような問題を持っているネストされた形

[ 
    [ 
    "mmsi"=>"^somevalue^" 
    points=>[ 
     "longtitude":"^longtitude_value^", 
     "latitude":"^latitude_value^", 
     "time":"^time_value^" 
    ], 
    .... 
    ], 
    .... 
] 

あなたはどんな考えを持っていますか?

Vesselエンティティは次のとおりです。

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* @ORM\Entity 
* @ORM\Table(name="vessel",indexes={ 
* @ORM\Index(name="mmsi",columns={"mmsi"}) 
* }) 
*/ 
class Vesel 
{ 
    /** 
    * @ORM\Column(type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    * @var integer 
    */ 
    private $id; 

    /** 
    * @ORM\Column(type="integer",name="mmsi") 
    * @var integer 
    */ 
    private $mmsi; 

    /** 
    * @ORM\OneToMany(targetEntity="VesselMoveStatus",mappedBy="vesel") 
    * @var \Doctrine\Common\Collections\Collection 
    */ 
    private $veselMoveStatuses; 

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

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

    /** 
    * Set mmsi 
    * 
    * @param integer $mmsi 
    * 
    * @return Vesel 
    */ 
    public function setMmsi($mmsi) 
    { 
     $this->mmsi = $mmsi; 

     return $this; 
    } 

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

    /** 
    * Add veselMoveStatus 
    * 
    * @param \AppBundle\Entity\VesselMoveStatus $veselMoveStatus 
    * 
    * @return Vesel 
    */ 
    public function addVeselMoveStatus(\AppBundle\Entity\VesselMoveStatus $veselMoveStatus) 
    { 
     $this->veselMoveStatuses[] = $veselMoveStatus; 

     return $this; 
    } 

    /** 
    * Remove veselMoveStatus 
    * 
    * @param \AppBundle\Entity\VesselMoveStatus $veselMoveStatus 
    */ 
    public function removeVeselMoveStatus(\AppBundle\Entity\VesselMoveStatus $veselMoveStatus) 
    { 
     $this->veselMoveStatuses->removeElement($veselMoveStatus); 
    } 

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

そしてVesselMoveStatus実体に関連している:

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use AppBundle\Entity\Vesel; 

/** 
* @ORM\Entity(repositoryClass="AppBundle\Repository\VeselRouteRepository") 
* @ORM\Table(name="vessel_position_status",indexes={ 
* @ORM\Index(name="position",columns={"long","lat"}) 
* }) 
*/ 
class VesselMoveStatus 
{ 
    /** 
    * @ORM\Column(type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id=null; 

    /** 
    * @ORM\ManyToOne(targetEntity="Vesel",inversedBy="veselMoveStatuses") 
    * @ORM\JoinColumn(name="vesel_id", referencedColumnName="id") 
    * @var Vesel 
    */ 
    private $vesel=null; 

    /** 
    * @ORM\Column(name="status",type="integer") 
    * @var integer|null 
    */ 
    private $status=null; 

    /** 
    * @ORM\Column(name="speed",type="integer") 
    * @var integer|null 
    */ 
    private $speed=null; 

    /** 
    * @ORM\Column(name="long",type="float") 
    * @var float|null 
    */ 
    private $logtitude=null; 

    /** 
    * @ORM\Column(name="lat",type="float") 
    * @var float|null 
    */ 
    private $latitude=null; 

    /** 
    * @ORM\Column(name="course",type="integer") 
    * @var integer|null 
    */ 
    private $course=null; 

    /** 
    * @ORM\Column(name="heading",type="integer") 
    * @var integer|null 
    */ 
    private $heading=null; 

    /** 
    * @ORM\Column(name="rotation",type="integer") 
    * @var integer|null 
    */ 
    private $rotation=null; 

    /** 
    * @ORM\Column(name="timestamp",type="datetime") 
    * @var Datetime|null 
    */ 
    private $timestamp=null; 

    public function __construct(
      Vesel $vesel=null, 
      $status=null, 
      $speed=null, 
      $long=null, 
      $lat=null, 
      $course=null, 
      $heading=null, 
      $rotation=null, 
      $timestamp=null 
    ){ 
     $this->setVesel($vesel) 
      ->setStatus($status) 
      ->setSpeed($speed) 
      ->setLogtitude($long) 
      ->setLatitude($lat) 
      ->setCourse($course) 
      ->setHeading($heading) 
      ->setRotation($rotation) 
      ->setTimestamp($timestamp); 
    } 

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

    /** 
    * Set mmsi 
    * 
    * @param integer $mmsi 
    * 
    * @return VesselMoveStatus 
    */ 
    public function setMmsi($mmsi) 
    { 
     $this->mmsi = $mmsi; 

     return $this; 
    } 

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

    /** 
    * 
    * @param integer $status 
    * @return \AppBundle\Entity\VesselMoveStatus 
    */ 
    public function setStatus($status) 
    { 
     $this->status=$status; 
     return $this; 
    } 

    /** 
    * 
    * @return \AppBundle\Entity\integer|NULL 
    */ 
    public function getStatus() 
    { 
     return $this->status; 
    } 

    /** 
    * Set speed 
    * 
    * @param integer $speed 
    * 
    * @return VesselMoveStatus 
    */ 
    public function setSpeed($speed) 
    { 
     $this->speed = $speed; 

     return $this; 
    } 

    /** 
    * Get speed 
    * 
    * @return float 
    */ 
    public function getSpeed() 
    { 
     return $this->speed/10; 
    } 

    /** 
    * Set logtitude 
    * 
    * @param integer $logtitude 
    * 
    * @return VesselMoveStatus 
    */ 
    public function setLogtitude($logtitude) 
    { 
     $this->logtitude = $this->sanitizeGpsCoordinate($logtitude); 

     return $this; 
    } 

    /** 
    * Get logtitude 
    * 
    * @return float 
    */ 
    public function getLogtitude() 
    { 
     return $this->logtitude; 
    } 

    /** 
    * Set latitude 
    * 
    * @param integer $latitude 
    * 
    * @return VesselMoveStatus 
    */ 
    public function setLatitude($latitude) 
    { 
     $this->latitude = $this->sanitizeGpsCoordinate($latitude); 

     return $this; 
    } 

    /** 
    * Get latitude 
    * 
    * @return float 
    */ 
    public function getLatitude() 
    { 
     $latitude=$this->latitude; 
     return $latitude; 
    } 

    /** 
    * Set course 
    * 
    * @param integer $course 
    * 
    * @return VesselMoveStatus 
    */ 
    public function setCourse($course) 
    { 
     $this->course = $course; 

     return $this; 
    } 

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

    /** 
    * Set heading 
    * 
    * @param integer $heading 
    * 
    * @return VesselMoveStatus 
    */ 
    public function setHeading($heading) 
    { 
     $this->heading = $heading; 

     return $this; 
    } 

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

    /** 
    * Set rotation 
    * 
    * @param integer $rotation 
    * 
    * @return VesselMoveStatus 
    */ 
    public function setRotation($rotation) 
    { 
     $this->rotation = $rotation; 

     return $this; 
    } 

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

    /** 
    * Set timesptamp 
    * 
    * @param string $timesptamp 
    * 
    * @return VesselMoveStatus 
    */ 
    public function setTimestamp($timesptamp) 
    { 
     $this->timestamp = date_create_from_format("Y-m-d H:i:s.u",$timesptamp); 
     return $this; 
    } 

    /** 
    * Get timesptamp 
    * 
    * @return \DateTime 
    */ 
    public function getTimestamp() 
    { 
     return $this->timestamp; 
    } 

    /** 
    * Set vesel 
    * 
    * @param \AppBundle\Entity\Vesel $vesel 
    * 
    * @return VesselMoveStatus 
    */ 
    public function setVesel(\AppBundle\Entity\Vesel $vesel = null) 
    { 
     $this->vesel = $vesel; 

     return $this; 
    } 

    /** 
    * Get vesel 
    * 
    * @return \AppBundle\Entity\Vesel 
    */ 
    public function getVesel() 
    { 
     return $this->vesel; 
    } 

    /** 
    * Sometimes a GPS Coordinate may have the following format: 
    * 1,234532 if inserted as is then itn WONT be retreived correctly. 
    * Please use this method to sanitize the gps coordinate on setter method. 
    * 
    * @param string | float $coordinate 
    * @return number 
    */ 
    private function sanitizeGpsCoordinate($coordinate) 
    { 
     if(is_string($coordinate)) 
     { 
      $coordinate=str_replace(',','.',$coordinate); 
     } 

     return (float)$coordinate; 
    } 
} 

答えて

2

あなたはデータを変換したい方法は標準ではありませんので、実行する教義では一般的なものはありませんそう。しかし、あなたが望む目的を達成することはまだ非常に簡単です。最も簡単な解決策は、結果を変換するために閉鎖を使用することである私見しかし

$result = $query->getResult(); 
$formatter = function($row) { 
    return [ 
     "mmsi" => $row['mmsi'] 
     "points" => [ 
     "longtitude" => $row['logtitude'], 
     "latitude" => $row['latitude'], 
     "time" => $row['timestamp'] 
     ] 
    ]; 
}; 
return array_map($formatter, $result); 

最終アレイは点ごとに一つの要素(veselMoveStatuses)を有するであろう。実際に必要なのは、1つの要素につき1つの要素しか持たない場合は、SELECTを変更してそれに応じてフォーマッタを適合させることを検討してください。

->select('v,m') 

結果はVeselの配列がすでにロードされてveselMoveStatuses協会を持つオブジェクトになります(this docを参照してください - セクションでは、「CmsUserを取得し、彼が持っているすべての電話番号に参加フェッチ」)。

関連する問題