I have a category table with id, name, id_parent structure. I'm using atk4 and I like to show dropdown with indented subcategories. So:
home
---cat1
---cat2
------subcat2.1
------subcat2.2
---cat3 etc
I did build something working however like to see how this can be improved. Currently I have recursive sql queries based on the hasMany(). It feels like this can be done without re-quering. And main worry is that I now have the styling defined in the model but I don't know how to move out. I tried to learn myself controller functionality but didn't manage so far. My code so far:
The model
<?php
class Model_Category extends Model_Table {
  public $table='category';
  function init() {
    parent::init();
    $this->addField('id_parent');
    $this->addField('name');
    $this->hasMany('Category','id_parent');
  }
  function tree($prefix='') {
    $r=array();
    $childs=$this->ref('Category');
    foreach($childs as $child) {
      $r[$childs->id]=$prefix.$childs['name'];
      $r=array_merge($r,$childs->tree($prefix.'---'));
    }
    return $r;
  }
}
On the page:
$f=$this->add('Form');
$m=$this->add('Model_Category');
$m->load(1); // start recursive from home category
$f->addField('dropdown','category')->setValueList($m->tree());
 
    