PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, February 15, 2022

[FIXED] Symfony2 - Sort Multi-dimensional Array Recursive

 February 15, 2022     doctrine, doctrine-orm, php, sorting, symfony     No comments   

Issue

My FAQ-System is able to have SubFAQ's, every FAQ and SubFAQ can be liked (likecount).

I managed to sort all FAQ's (parents) by the likecount in my Controller with:

        $faqs = $this->getDoctrine()// DATENBANK LADEN UND DIREKT SORTIEREN
    ->getRepository('AppBundle:Faq')
        ->findBy(array('parent' => null), array('likecount' => 'desc'));

The problem is, that the SubFAQ's are still not sorted by the likecount.

Dump for Parents (FAQ)

  "faqs" => array:13 [▼
0 => Faq {#1025 ▼
  -id: 23
  -question: "ajaja"
  -answer: "ajaja"
  -username: "Robert"
  -userid: 1
  -createdat: DateTime {#1023 ▶}
  -likecount: 4
  -tags: PersistentCollection {#1040 ▶}
  -categoryid: Category {#1015 ▶}
  -children: PersistentCollection {#1042 ▶}
  -parent: null
  -faqcatid: Category {#1015 ▶}

Dump for Children (SubFAQ)

    0 => Faq {#1349 ▼
  -id: 72
  -question: "1"
  -answer: "1"
  -username: "Robert"
  -userid: 1
  -createdat: DateTime {#1351 ▶}
  -likecount: null
  -tags: PersistentCollection {#1095 ▶}
  -categoryid: null
  -children: PersistentCollection {#1372 ▶}
  -parent: Faq {#469 ▶}
  -faqcatid: null

I include a 'subfaq.html.twig' in every FAQ and passing him the children. It includes itself again in the subfaq.html.twig, in case there are Subs in Subs.

{{ include('default/subfaq.html.twig', {'faqs' : faq.children}) }}

How could I solve this?

Thank you!


Solution

You can order associations using annotation:

/** @Entity **/
class Faq
{
    // ...

    /**
     * @ManyToMany(targetEntity="SubFaq")
     * @OrderBy({"likecount" = "ASC"})
     **/
    private $subfaqs;
}


Answered By - Stephan Vierkant
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing