2016-01-21 69 views
0

我想在php mysql中獲取文件夾和子文件夾內容的內容。 我建一個表,以保持文件夾結構如下使用CakePHP 2.x列出文件夾和子文件夾的內容

image1

而另一臺爲文件夾中的媒體內容,這是如下

Image2

在第一個表我保留parent_id以存儲父文件夾標識。

  • 0代表父文件夾ID。
  • 假設我有在根文件夾1,2,3
  • 4,5內部1.
  • 6,7-內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' 
    ) 
); 
} 
+0

那麼你的問題是什麼?採取[StackOverflow旅遊](http://stackoverflow.com/tour)和閱讀[如何問](http://stackoverflow.com/help/how-to-ask)! – Ben

+0

抱歉,問的方式不好。我是新來的,只是試圖獲取每個文件夾和子文件夾以及這些內容。 –

+0

「ProjectFolder」與「ProjectMediaContent」相關嗎? 'hasOne','hasMany'? –

回答

0

下應該取你需要的所有數據:

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

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

我會通過$folderArr,而不是重新排列控制器中的數據。

相關問題