Skip to content

Instantly share code, notes, and snippets.

@frazr
Created September 18, 2016 19:41
Show Gist options
  • Save frazr/e99e3262d1d7a74875b955c60a621273 to your computer and use it in GitHub Desktop.
Save frazr/e99e3262d1d7a74875b955c60a621273 to your computer and use it in GitHub Desktop.
/* FilesTable */
public function initialize(array $config)
{
parent::initialize($config);
$this->table('files');
$this->displayField('name');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->hasMany('SubFolders', [
'className' => 'Files',
'foreignKey' => 'parent_folder_id',
]);
$this->belongsTo('ParentFolders', [
'className' => 'Files',
'foreignKey' => 'parent_folder_id',
]);
$this->belongsTo('MimeTypes', [
'foreignKey' => 'mime_type_id',
'joinType' => 'INNER'
]);
$this->belongsToMany('Messages', [
'foreignKey' => 'file_id',
'targetForeignKey' => 'message_id',
'joinTable' => 'files_messages'
]);
$this->belongsToMany('Users', [
'foreignKey' => 'file_id',
'targetForeignKey' => 'user_id',
'joinTable' => 'files_users'
]);
}
/* Fetching */
public function index()
{
$this->viewBuilder()->template('home');
$this->loadModel('Files');
$folders = $this->Files->find()->where([
'Files.is_dir' => 1,
'Files.parent_folder_id IS NULL'
])->order([
'Files.filename' => 'ASC'
])->contain(['SubFolders']);
debug($folders->toArray());
$this->set(compact('folders'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment