Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Multiple model support #5

Merged
merged 1 commit into from

2 participants

@nCrazed

"Since behaviors are shared across all the model instances that use them, it’s a good practice to store the settings per alias/model name that is using the behavior."
http://book.cakephp.org/2.0/en/models/behaviors.html#creating-behaviors

@krolow krolow merged commit f0a13ae into krolow:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 30, 2012
  1. @nCrazed
This page is out of date. Refresh to see the latest.
Showing with 10 additions and 10 deletions.
  1. +10 −10 Model/Behavior/UploadBehavior.php
View
20 Model/Behavior/UploadBehavior.php
@@ -5,13 +5,13 @@ class UploadBehavior extends ModelBehavior {
public function setup(Model $model, $config = array()) {
- $this->config = $config;
- $this->types = array_keys($this->config);
+ $this->config[$model->alias] = $config;
+ $this->types[$model->alias] = array_keys($this->config[$model->alias]);
- foreach ($this->types as $index => $type) {
+ foreach ($this->types[$model->alias] as $index => $type) {
$folder = $this->getUploadFolder($model, $type);
$this->isWritable($this->getUploadFolder($model, $type));
- $this->setRelation($model, $this->types[$index]);
+ $this->setRelation($model, $this->types[$model->alias][$index]);
}
}
@@ -21,7 +21,7 @@ public function setRelation(Model $model, $type) {
$relation = 'hasOne';
//case is defined multiple is a hasMany
- if (isset($this->config[$type]['multiple']) && $this->config[$type]['multiple'] == true) {
+ if (isset($this->config[$model->alias][$type]['multiple']) && $this->config[$model->alias][$type]['multiple'] == true) {
$relation = 'hasMany';
}
@@ -124,7 +124,7 @@ public function getFileExtension($filename) {
* @access public
*/
public function getUploadFolder($model, $type) {
- return APP . str_replace('{DS}', DS, $this->config[$type]['dir']) . DS;
+ return APP . str_replace('{DS}', DS, $this->config[$model->alias][$type]['dir']) . DS;
}
public function isWritable($dir) {
@@ -137,7 +137,7 @@ public function isWritable($dir) {
public function afterSave(Model $model, $created) {
- foreach ($this->types as $type) {
+ foreach ($this->types[$model->alias] as $type) {
//case has the file update :)
if (isset($model->data[$model->alias][$type]['tmp_name']) &&
!empty($model->data[$model->alias][$type]['tmp_name'])) {
@@ -148,7 +148,7 @@ public function afterSave(Model $model, $created) {
public function beforeDelete($model, $cascade = true) {
if ($cascade = true) {
- foreach ($this->types as $type) {
+ foreach ($this->types[$model->alias] as $type) {
$className = 'Attachment'. Inflector::camelize($type);
$attachments = $model->{$className}->find('all', array(
@@ -177,7 +177,7 @@ public function saveFile(Model $model, $type) {
copy($model->data[$model->alias][$type]['tmp_name'], $file);
@unlink($this->data[$model->alias][$type]['tmp_name']);
- if (isset($this->config[$type]['thumbs'])) {
+ if (isset($this->config[$model->alias][$type]['thumbs'])) {
$info = getimagesize($file);
if (!$info) {
throw new CakeException(sprintf('The file %s is not an image', $file));
@@ -254,7 +254,7 @@ public function __createThumbs($file, $type) {
$image = $imagine->open($file);
$thumbName = basename($file);
- foreach ($this->config[$type]['thumbs'] as $key => $values) {
+ foreach ($this->config[$model->alias][$type]['thumbs'] as $key => $values) {
$this->__generateThumb(array(
'name' => str_replace($thumbName, $key . '.' . $thumbName, $file),
'w' => $values['w'],
Something went wrong with that request. Please try again.