Symfony 3 Doctrine Order By Annotation 不起作用



我有3个实体。我正在尝试获取数据,但是我无法订购结果

Island.php

namespace MyGameBundleEntity;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMapping as ORM;
/**
 * Island
 *
 *
 * @ORMTable(name="islands")
 * @ORMEntity(repositoryClass="MyGameBundleRepositoryIslandRepository")
 */
class Island
{
/**
 * @var int
 *
 * @ORMColumn(name="id", type="integer")
 * @ORMId
 * @ORMGeneratedValue(strategy="AUTO")
 */
private $id;
/**
 * @var int
 *
 * @ORMColumn(name="x", type="integer")
 */
private $x;
/**
 * @var int
 *
 * @ORMColumn(name="y", type="integer")
 */
private $y;
/**
 * @var Player
 *
 * @ORMManyToOne(targetEntity="MyGameBundleEntityPlayer", inversedBy="islands")
 * @ORMJoinColumn(name="player_id", nullable=false)
 */
private $player;
/**
 * @var IslandResource[]
 *
 * @ORMOneToMany(targetEntity="MyGameBundleEntityIslandResource", mappedBy="island")
 */
private $resources;
/**
 * @var IslandBuilding[]
 *
 * @ORMOneToMany(targetEntity="MyGameBundleEntityIslandBuilding", mappedBy="island")
 */
private $buildings;
/**
 * @var IslandTroop[]
 *
 * @ORMOneToMany(targetEntity="MyGameBundleEntityIslandTroop", mappedBy="island")
 */
private $troops;
/**
 * @var TroopProcess[]
 *
 * @ORMOneToMany(targetEntity="MyGameBundleEntityTroopProcess", mappedBy="island")
 */
private $process;

public function __construct()
{
    $this->resources = new ArrayCollection();
    $this->buildings = new ArrayCollection();
    $this->troops = new ArrayCollection();
    $this->process = new ArrayCollection();
}
/**
 * Get id
 *
 * @return int
 */
public function getId()
{
    return $this->id;
}
/**
 * Set x
 *
 * @param integer $x
 *
 * @return Island
 */
public function setX($x)
{
    $this->x = $x;
    return $this;
}
/**
 * Get x
 *
 * @return int
 */
public function getX()
{
    return $this->x;
}
/**
 * Set y
 *
 * @param integer $y
 *
 * @return Island
 */
public function setY($y)
{
    $this->y = $y;
    return $this;
}
/**
 * Get y
 *
 * @return int
 */
public function getY()
{
    return $this->y;
}
/**
 * @return Player
 */
public function getPlayer()
{
    return $this->player;
}
/**
 * @param Player $player
 */
public function setPlayer(Player $player)
{
    $this->player = $player;
}
/**
 * @return IslandResource[]
 */
public function getResources()
{
    return $this->resources;
}
/**
 * @param IslandResource[] $resources
 */
public function setResources(array $resources)
{
    $this->resources = $resources;
}
/**
 * @return IslandBuilding[]
 */
public function getBuildings()
{
    return $this->buildings;
}
/**
 * @param IslandBuilding[] $buildings
 */
public function setBuildings(array $buildings)
{
    $this->buildings = $buildings;
}
/**
 * @return IslandTroop[]
 */
public function getTroops()
{
    return $this->troops;
}
/**
 * @param IslandTroop[] $troops
 */
public function setTroops(array $troops)
{
    $this->troops = $troops;
}
/**
 * @return TroopProcess[]
 */
public function getProcess()
{
    return $this->process;
}
/**
 * @param TroopProcess[] $process
 */
public function setProcess(array $process)
{
    $this->process = $process;
}
}

building.php

<?php
namespace MyGameBundleEntity;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMapping as ORM;
/**
 * Building
 *
 * @ORMTable(name="buildings")
 * @ORMEntity(repositoryClass="MyGameBundleRepositoryBuildingRepository")
 */
class Building
{
/**
 * @var int
 *
 * @ORMColumn(name="id", type="integer")
 * @ORMId
 * @ORMGeneratedValue(strategy="AUTO")
 */
private $id;
/**
 * @var BuildingCostResource[]
 *
 * @ORMOneToMany(targetEntity="MyGameBundleEntityBuildingCostResource", mappedBy="building")
 */
private $costs;
/**
 * @var BuildingCostTime
 *
 * @ORMOneToOne(targetEntity="MyGameBundleEntityBuildingCostTime", mappedBy="building")
 */
private $timeCost;
/**
 * @var IslandBuilding[]
 *
 * @ORMOneToMany(targetEntity="MyGameBundleEntityIslandBuilding", mappedBy="building")
 * @ORMOrderBy({"id" = "DESC"})
 */
private $islandBuildings;
/**
 * @var string
 *
 * @ORMColumn(name="name", type="string", length=255, unique=true)
 */
private $name;
public function __construct()
{
    $this->costs = new ArrayCollection();
    $this->islandBuildings = new ArrayCollection();
}

/**
 * Get id
 *
 * @return int
 */
public function getId()
{
    return $this->id;
}
/**
 * Set name
 *
 * @param string $name
 *
 * @return Building
 */
public function setName($name)
{
    $this->name = $name;
    return $this;
}
/**
 * Get name
 *
 * @return string
 */
public function getName()
{
    return $this->name;
}
/**
 * @return BuildingCostResource[]
 */
public function getCosts()
{
    return $this->costs;
}
/**
 * @param BuildingCostResource[] $costs
 */
public function setCosts(array $costs)
{
    $this->costs = $costs;
}
/**
 * @return BuildingCostTime
 */
public function getTimeCost()
{
    return $this->timeCost;
}
/**
 * @param BuildingCostTime $timeCost
 */
public function setTimeCost(BuildingCostTime $timeCost)
{
    $this->timeCost = $timeCost;
}
/**
 * @return IslandBuilding[]
 */
public function getIslandBuildings()
{
    return $this->islandBuildings;
}
/**
 * @param IslandBuilding[] $islandBuildings
 */
public function setIslandBuildings(array $islandBuildings)
{
    $this->islandBuildings = $islandBuildings;
}
}

IslandBuilding.php

<?php
namespace MyGameBundleEntity;
use DoctrineORMMapping as ORM;
/**
 * IslandBuilding
 *
 * @ORMTable(name="island_buildings")
 * @ORMEntity(repositoryClass="MyGameBundleRepositoryIslandBuildingRepository")
*/
class IslandBuilding
{
/**
 * @var int
 *
 * @ORMColumn(name="id", type="integer")
 * @ORMId
 * @ORMGeneratedValue(strategy="AUTO")
 */
private $id;
/**
 * @var int
 *
 * @ORMColumn(name="level", type="integer")
 */
private $level;
/**
 * @var Island
 *
 * @ORMManyToOne(targetEntity="MyGameBundleEntityIsland", inversedBy="buildings")
 * @ORMJoinColumn(name="island_id", nullable=false)
 */
private $island;
/**
 * @var Building
 *
 * @ORMManyToOne(targetEntity="MyGameBundleEntityBuilding", inversedBy="islandBuildings")
 * @ORMJoinColumn(name="building_id", nullable=false)
 */
private $building;
/**
 * @var BuildingProcess
 *
 * @ORMOneToOne(targetEntity="MyGameBundleEntityBuildingProcess", mappedBy="islandBuilding")
 */
private $process;

/**
 * Get id
 *
 * @return int
 */
public function getId()
{
    return $this->id;
}
/**
 * Set level
 *
 * @param integer $level
 *
 * @return IslandBuilding
 */
public function setLevel($level)
{
    $this->level = $level;
    return $this;
}
/**
 * Get level
 *
 * @return int
 */
public function getLevel()
{
    return $this->level;
}
/**
 * @return Island
 */
public function getIsland()
{
    return $this->island;
}
/**
 * @param Island $island
 */
public function setIsland(Island $island)
{
    $this->island = $island;
}
/**
 * @return Building
 */
public function getBuilding()
{
    return $this->building;
}
/**
 * @param Building $building
 */
public function setBuilding(Building $building)
{
    $this->building = $building;
}
/**
 * @return BuildingProcess
 */
public function getProcess()
{
    return $this->process;
}
/**
 * @param BuildingProcess $process
 */
public function setProcess(BuildingProcess $process)
{
    $this->process = $process;
}
}

在我的方法中,代码为

$island = $this->getDoctrine()->getRepository(Island::class)->find($this->getIslandId());
return $this->render('buildings/index.html.twig', [
    'buildings' => $island->getBuildings()
]);

我想在岛上以岛上的身份在岛上以降序排序的所有建筑物名称。这返回一个无序的建筑物名称。

您可以在BuildingRepository中创建一个名为findByIsland的函数,该函数包含以下代码:

public function findByIsland($island) {
    $qb = $this->createQueryBuilder('b');
    $qb->where('b.island = :island')
        ->orderBy('b.name', 'DESC')
        ->setParameter('island', $island);
    return $qb->getQuery()->getResult();
}

然后在您的代码中您可以调用新的回购方法:

return $this->render('buildings/index.html.twig', [
     'buildings' => $this->getDoctrine()->getRepository(Building::class)->findByIsland($island)
]);

在您的示例上,param buildings传递给twig包含 IslandBuilding对象。

要获取IslandBuilding对象,您可以在岛屿建筑存储库中添加功能 myGameBundle repository sepository IslandBuildingRepository

您可以使用JOIN获取建筑物信息,而orderBy可以按照其名称对建筑物进行排序:

public function findByIsland( $island ) {
    $qb = $this->createQueryBuilder( 'ib' )
               ->join( 'ib.building', 'b', 'WITH', 'ib.building = b.id' )
               ->where( 'ib.island = :island' )
               ->orderBy( 'b.name', 'ASC' )
               ->setParameter( 'island', $island );
    return $qb->getQuery()->getResult();
}

在您的方法调用该函数上使用:

$islandBuildings = $this->getDoctrine()->getRepository(IslandBuilding::class)->findByIsland($this->getIslandId());
return $this->render('buildings/index.html.twig', [
   'islandBuildings' => $islandBuildings
]);

最新更新