使用CakePHP 2.x列出文件夹和子子文件夹的内容

时间:2016-01-21 15:34:32

标签: php mysql cakephp parent-child cakephp-2.x

我想在php mysql中获取文件夹和子文件夹内容的内容。 我已经构建了一个表来维护文件夹结构,如下所示

image1

该文件夹的媒体内容的另一个表,如下所示

Image2

在第一个表格中,我使用parent_id来存储父文件夹ID。

  • 0表示父文件夹ID。
  • 假设我在根目录中有1,2,3个文件夹
  • 4,5里面1。
  • 6,7 in 2
  • 等。

媒体内容表正在存储每个folder_id的内容。

现在我需要在CakePHP中显示文件夹结构和内容。

我已经进入了第一级列表。如下

$folderArr = $this->ProjectFolder->find('all',
                        array(                              
                            'conditions'=>
                                array(
                                        'ProjectFolder.is_delete'=>'0',
                                        'ProjectFolder.parent_id'=>$pid,
                                        'ProjectFolder.user_id'  => $userId
                                    ),
                                )
                 );


    $folderContent = $this->ProjectMediaContent->find('all',
                        array(                              
                            'conditions'=>
                                array(                                          
                                        'ProjectMediaContent.project_folder_id'=>$pid,                                          
                                    ),  
                            )
                 );
    //$log = $this->ProjectMediaContent->getDataSource()->getLog(false, false);
    //debug($log);
    //exit;
    $content  = Array();

    if(!empty($folderContent)){
        $mediapath = $thumbpath = '';
        foreach($folderContent as $mediaContent){
            if(!empty($mediaContent['ProjectMediaContent']['media'])){
                $mediapath = Router::url('/', true).'files/folder/'.$mediaContent['ProjectMediaContent']['media'];
                $thumbpath = Router::url('/', true).'files/folder/thumbs/'.$mediaContent['ProjectMediaContent']['media'];
            }else{
                $mediapath = $mediaContent['ProjectImage']['actual_url'];
                $thumbpath = $mediaContent['ProjectImage']['thumb_url'];
            }
            $parentFolderContent[] = array(
                "mediaId"       => $mediaContent['ProjectMediaContent']['project_media_content_id'],                
                "mediaType"     => $mediaContent['ProjectMediaContent']['media_type'],
                'socialPlatForm'=> $mediaContent['ProjectImage']['image_source'],
                'actualPath'    => $mediapath,
                'thumbPath'     => $thumbpath,
                'socialId'      => $mediaContent['ProjectImage']['social_id'],              
            );
        }
    }
    if(!empty($folderArr)){ 
        foreach($folderArr as $val){
            //print_r($this->getFolderListing($val['ProjectFolder']['project_folder_id'],$userId));die('jj');
            $content[] = array(
                               'Folderid'   => $val['ProjectFolder']['project_folder_id'],
                               'FolderName' => $val['ProjectFolder']['folder_name'],

                              );
        }
    }   

项目文件夹模型如下

class ProjectFolder extends AppModel{
   public $name            = 'ProjectFolder';
   public $primaryKey      = 'project_folder_id';
   public $displayField    = 'folder_name';
   public $actsAs           = array('Containable');

   public $belongsTo = array(
      'User' => array(
        'className' => 'User',
        'foreignKey' => 'user_id'
     ),
   );
   public $hasMany = array(
      'ProjectMediaContent' => array(
         'className' => 'ProjectMediaContent',
         //'dependent' => true,
         //'exclusive' => true,
      )
  );

}

ProjectMediaContent模型如下

class ProjectMediaContent extends AppModel{
    public $name            = 'ProjectMediaContent';
    public $primaryKey      = 'project_media_content_id';

    //public $useTable = 'project_medias';
    public $belongsTo = array(
      'ProjectFolder' => array(
          'className' => 'ProjectFolder',
          'foreignKey'=>'project_folder_id'
      ),
     'ProjectImage' => array(
        'className' => 'ProjectImage',
        'foreignKey'=>'project_image_id'
    )
  );
}

1 个答案:

答案 0 :(得分:0)

以下内容应该提取您需要的所有数据:

$this->ProjectFolder->contain(array(
    'ProjectMediaContent'=>'ProjectImage'
));

$folderArr = $this->ProjectFolder->find('theaded', array(
    'conditions'=> array(
        'ProjectFolder.is_delete'=>'0',
        'ProjectFolder.user_id'  => $userId
    ),
));

我会将$folderArr传递给视图,而不是重新安排控制器中的数据。