Stump of a Muffin

CakePHP – TreeBehavior with Containable

CakePHP 2

The TreeBehavior is an awesome feature of CakePHP. I was surprised that there was no way to employ the ContainableBehavior (the ContainableBehavior is another awesome feature of CakePHP). What’s a PHP guy to do? Roll your own.

Here’s a simple swipe at it here:



/**
  *
 * @return array
 */
  public function childrenImages($data) {
    //
    $return  = array();
    //
    if (!$data) {
      //
      return $return;
    }
    //
    $ids    = Hash::extract($data, '{n}.PortfolioCategory.id');
    //
    $Attachment  = ClassRegistry::init('Media.Attachment');
    //
    $silhouettes  = $Attachment->find(
        'all'
        , array(
          'conditions'  => array(
            'model'    => 'SilhouetteCategory'
            , 'foreign_key'  => $ids
            ,
          )
        )
    );
    //
    $silhouettes  = array_combine($ids, $silhouettes);
    //
    $data    = array_combine($ids, $data);
    //
    foreach ($data AS $key => $datum) {
      //
      $datum['Image']  = isset($silhouettes[$key]['Attachment']) ? $silhouettes[$key]['Attachment'] : array();
      //
      $return[$key]  = $datum;
    }
    //
    sort($return);
    //
    return $return;
  }

Post navigation

Comments are closed.