相关疑难解决方法(0)

如何排序findAll Doctrine的方法

我一直在阅读Doctrine的文档,但是我找不到一种方法来对findAll()结果进行排序.

我正在使用symfony2 + doctrine,这是我在Controller中使用的语句:

$this->getDoctrine()->getRepository('MyBundle:MyTable')->findAll();

但我希望结果按升序用户名排序.

我一直试图以这种方式传递数组作为参数:

findAll( array('username' => 'ASC') );

但它不起作用(它也没有抱怨).

有没有办法在不构建DQL查询的情况下执行此操作?

symfony doctrine-orm

101
推荐指数
8
解决办法
15万
查看次数

"usort"一个Doctrine\Common\Collections\ArrayCollection?

在各种情况下,我需要Doctrine\Common\Collections\ArrayCollection根据对象中的属性对其进行排序.如果没有找到立即执行的方法,我会这样做:

// $collection instanceof Doctrine\Common\Collections\ArrayCollection
$array = $collection->getValues();
usort($array, function($a, $b){
    return ($a->getProperty() < $b->getProperty()) ? -1 : 1 ;
});

$collection->clear();
foreach ($array as $item) {
    $collection->add($item);
}
Run Code Online (Sandbox Code Playgroud)

我认为这不是最好的方法,你必须将所有内容复制到本机PHP数组并返回.我想知道是否有更好的方式来"使用"a Doctrine\Common\Collections\ArrayCollection.我想念任何文件吗?

php doctrine-orm

49
推荐指数
3
解决办法
3万
查看次数

如何在OneToMany/ManyToOne上订购

我有一个产品类,其上有许多字段,用于ManyToMany,例如成分,大小,种类等.共有大约14个不同的字段并非所有字段都与每个产品相关.

我有这样的映射设置

Class product {
/**
 * @var Species[]
 * @ORM\ManyToMany(targetEntity="Species")
 * @ORM\JoinTable(name="product_species",
 *      joinColumns={@ORM\JoinColumn(name="productId", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="speciesId", referencedColumnName="id")}
 *      )
 * @ORM\OrderBy({"name" = "asc"})
 */
private $species;
Run Code Online (Sandbox Code Playgroud)

这适用于许多人/多人.

问题出在我的product_ingredients表中我需要添加一个额外的字段,这意味着需要从ManyToMany切换到OneToMany/ManyToOne所以就像这样

/**
     * @var ProductIngredient[]
     *
     * @ORM\OneToMany(targetEntity="ProductIngredient", mappedBy="product")
     * @ORM\JoinColumn(name="productId", referencedColumnName="id")
     */
    private $ingredients;
Run Code Online (Sandbox Code Playgroud)

现在我的ProductIngredient实体看起来像这样

 /**
     * @var IngredientType
     * @ORM\ManyToOne(targetEntity="IngredientType", fetch="EAGER")
     * @ORM\JoinColumn(name="ingredientTypeId", referencedColumnName="id")
     */
    private $ingredientType;


    /**
     * @var Ingredient
     *
     * @ORM\ManyToOne(targetEntity="Ingredient", inversedBy="products", fetch="EAGER")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="ingredientId", referencedColumnName="id")
     * })
     */
    private $ingredient;

    /**
     * @var …
Run Code Online (Sandbox Code Playgroud)

symfony doctrine-orm

30
推荐指数
2
解决办法
4万
查看次数

标签 统计

doctrine-orm ×3

symfony ×2

php ×1