iPlease excuse if this is a very dumb question, I’ve been pulling at hair for two days on this and I just cannot seem to ‘see’ the solution.

Now, I’m teaching myself PHP / WordPress out of pure boredom and even though the following makes sense, I just can’t seem to find a resolution for it.

To start off with, I have a custom collection implementation similar to http://bit.ly/1wtoCQQ, I’m not going to post the code as it’s to extensive.

I also created an POCO which represents my term taxonomy in WordPress (I know, WordPress has functions, but I’m learning WordPress from an OO point of view, so bear with me), which looks like:

class wg_category extends wg_domainobject {
    public $children;
    public $count;
    public $description;
    public $id;
    public $imageurl;
    public $name;
    public $parent;
    public $slug;
    public $taxonomy;
    public $taxonomy_id;    
    public $term_group;
    public $url;    
    public function __construct(array $children = null, $count = 0, $description = '', $id = 0, $imageurl = '', $name = '', $parent = 0, $slug = '', $taxonomy = '', $taxonomy_id = 0, $term_group = 0, $url = '') {
        $this->children = $children;
        $this->count = $count;
        $this->description = $description;
        $this->id = $id;
        $this->imageurl = $imageurl;
        $this->name = $name;
        $this->parent = $parent;        
        $this->slug = $slug;
        $this->taxonomy = $taxonomy;
        $this->taxonomy_id = $taxonomy_id;
        $this->term_group = $term_group;  
        $this->url = $url;        
    }
}

This is very simple, I extend a domain object class and in my functions.php I simply call my autoloader class (stating for brevity).

Now, I want to build up a hierarchical instance of my term taxonomy, where each term and children, will be represented by my POCO class.

I’ve tried writing recursive methods for this but I just cannot get the children attached correctly, which is the reason for this post. After some digging, I stumbled upon a post here on stackoverflow by a user who has the following function, which works pretty damn well, however, he’s method relies on anonymous classes which I just cannot seem to override:

private function sort_terms_hierarchicaly(Array &$cats, Array &$into, $parentId = 0)
{
    foreach ($cats as $i => $cat) {
        if ($cat->parent == $parentId) {
            $into[$cat->term_id] = $cat;
            unset($cats[$i]);
        }
    }
    foreach ($into as $topCat) {
        $topCat->children = array();
        $this->sort_terms_hierarchicaly($cats, $topCat->children, $topCat->term_id);
    }
}

The implementation in my code, currently looks like this:

$c = new wg_collection();   
$categories = get_terms('wpbdp_category', array('hide_empty' => false));
$hierarchy = array();
$this->sort_terms_hierarchicaly($categories, $hierarchy);

For this to actually work the way I want, I know that I have to replace $hierarchy = array() with an instance of my POCO class $hierarchy = new entitieswg_category()

In the function, I now need to modify the two foreach() loops to add the hierarchy accordingly, but this is where I’m having difficulty trying to get my POCO populated correctly.

This is what I’ve tried, and it doesn’t work the way I want:

I modified my POCO class to accept a single array as parameter value, as I know PHP allows you to ‘reflect’ array values to members:

public function __construct(Array $properties=array()) {
    foreach($properties as $key => $value) {
        $this->{$key} = $value;
    }
}

Then, I modified my function to look something like:

private function sort_terms_hierarchicaly(Array &$cats, entitieswg_category &$into, $parentId = 0)
{
    foreach ($cats as $i => $cat) {
        if ($cat->parent == $parentId) {
            $into = new entitieswg_category($cat);
            unset($cats[$i]);
        }
    }
    foreach ($into as $topCat) {
        $topCat->children = new entitieswg_category();
        $this->sort_terms_hierarchicaly($cats, $topCat->children, $topCat->term_id);
    }
}

I’m sure there’s something I’m missing and I’d appreciate any guidance to resolve this. I know I’m ‘re-inventing’ the wheel, but this challenge has now gotten the better of me even though I’m sure what I’m trying to achieve is possible?

Read more here: Term taxonomy into custom collection of objects


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Related Wordpress search:

, , , ,

Wordpress related questions and answers: