按某些字段排序的迭代学说集合

发布于 2024-08-08 22:15:26 字数 324 浏览 6 评论 0原文

我需要这样的东西:

        $products = Products::getTable()->find(274);
        foreach ($products->Categories->orderBy('title') as $category)
        {
            echo "{$category->title}<br />";
        }

我知道这是不可能的,但是......我怎样才能在不创建 Doctrine_Query 的情况下做这样的事情?

谢谢。

I need something like this:

        $products = Products::getTable()->find(274);
        foreach ($products->Categories->orderBy('title') as $category)
        {
            echo "{$category->title}<br />";
        }

I know is it not possible, but... How can I do something like this without creating a Doctrine_Query?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜雨飘雪 2024-08-15 22:15:26

您还可以执行以下操作:

$this->hasMany('Category as Categories', array(...
             'orderBy' => 'title ASC'));

在架构文件中,它看起来像:

  Relations:
    Categories:
      class: Category
      ....
      orderBy: title ASC

You can also do:

$this->hasMany('Category as Categories', array(...
             'orderBy' => 'title ASC'));

In your schema file it looks like:

  Relations:
    Categories:
      class: Category
      ....
      orderBy: title ASC
死开点丶别碍眼 2024-08-15 22:15:26

我只是在看同样的问题。您需要将 Doctrine_Collection 转换为数组:

$someDbObject = Doctrine_Query::create()...;
$children = $someDbObject->Children;
$children = $children->getData(); // convert from Doctrine_Collection to array

然后您可以创建一个自定义排序函数并调用它:

// sort children
usort($children, array(__CLASS__, 'compareChildren')); // fixed __CLASS__

其中compareChildren 看起来像:

private static function compareChildren($a, $b) {
   // in this case "label" is the name of the database column
   return strcmp($a->label, $b->label);
}

I was just looking at the same problem. You need to convert the Doctrine_Collection into an array:

$someDbObject = Doctrine_Query::create()...;
$children = $someDbObject->Children;
$children = $children->getData(); // convert from Doctrine_Collection to array

Then you can create a custom sort function and call it:

// sort children
usort($children, array(__CLASS__, 'compareChildren')); // fixed __CLASS__

Where compareChildren looks something like:

private static function compareChildren($a, $b) {
   // in this case "label" is the name of the database column
   return strcmp($a->label, $b->label);
}
や三分注定 2024-08-15 22:15:26

您可以使用集合迭代器:

$collection = Table::getInstance()->findAll();

$iter = $collection->getIterator();
$iter->uasort(function($a, $b) {
  $name_a = (int)$a->getName();
  $name_b = (int)$b->getName();

  return $name_a == $name_b ? 0 : $name_a > $name_b ? 1 : - 1;
});        

foreach ($iter as $element) {
  // ... Now you could iterate sorted collection
}

如果您想使用 __toString 方法对集合进行排序,会更容易:

foreach ($collection->getIterator()->asort() as $element) { /* ... */ }

You could use collection iterator:

$collection = Table::getInstance()->findAll();

$iter = $collection->getIterator();
$iter->uasort(function($a, $b) {
  $name_a = (int)$a->getName();
  $name_b = (int)$b->getName();

  return $name_a == $name_b ? 0 : $name_a > $name_b ? 1 : - 1;
});        

foreach ($iter as $element) {
  // ... Now you could iterate sorted collection
}

If you want to sort collection using __toString method, it will be much easier:

foreach ($collection->getIterator()->asort() as $element) { /* ... */ }
守护在此方 2024-08-15 22:15:26

您可以向 Colletion.php 添加排序函数:

public function sortBy( $sortFunction )
{
    usort($this->data, $sortFunction);
}  

按用户年龄对 Doctrine_Collection 进行排序,如下所示:

class ExampleClass
{

    public static function sortByAge( $a , $b )
    {
         $age_a = $a->age;
         $age_b = $b->age;

         return $age_a == $age_b ? 0 : $age_a > $age_b ? 1 : - 1;
    }    

    public function sortExample()
    {
         $users = User::getTable()->findAll();
         $users ->sortBy('ExampleClass::sortByAge');

         echo "Oldest User:";
         var_dump ( $users->end() );
    }

}

You might add a sort function to Colletion.php :

public function sortBy( $sortFunction )
{
    usort($this->data, $sortFunction);
}  

Sorting a Doctrine_Collection of users by their age would look like this:

class ExampleClass
{

    public static function sortByAge( $a , $b )
    {
         $age_a = $a->age;
         $age_b = $b->age;

         return $age_a == $age_b ? 0 : $age_a > $age_b ? 1 : - 1;
    }    

    public function sortExample()
    {
         $users = User::getTable()->findAll();
         $users ->sortBy('ExampleClass::sortByAge');

         echo "Oldest User:";
         var_dump ( $users->end() );
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文