-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Add new models (implement #347).
- Loading branch information
1 parent
b671134
commit 2cfa26f
Showing
5 changed files
with
308 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
App::uses('AppModel', 'Model'); | ||
|
||
class Album extends AppModel | ||
{ | ||
private $bandId; | ||
public $belongsTo = array('Band'); | ||
public $hasMany = array('Track'); | ||
public $validate = array( | ||
'name' => array( | ||
'rule' => array('maxLength', 255), | ||
'required' => 'create', | ||
'allowEmpty' => false, | ||
'message' => 'Album\'s name cannot be empty or exceed 255 characters.' | ||
), | ||
'cover' => array( | ||
'rule' => array('maxLength', 37), | ||
'allowEmpty' => true, | ||
'message' => 'Album\'s cover cannot exceed 37 characters.' | ||
), | ||
'year' => array( | ||
'rule' => array('date', 'y'), | ||
'allowEmpty' => true, | ||
'message' => 'Invalid album year.' | ||
) | ||
); | ||
|
||
public function beforeValidate($options = array()) | ||
{ | ||
if (isset($this->data[$this->alias]['id'])) { | ||
$this->validator()->getField('name')->getRule(0)->allowEmpty = true; | ||
} | ||
} | ||
|
||
public function beforeSave($options = array()) | ||
{ | ||
if (isset($this->data[$this->alias]['id'])) { | ||
$this->data[$this->alias]['updated'] = date('Y-m-d H:i:s'); | ||
} else { | ||
unset($this->data[$this->alias]['updated']); | ||
} | ||
unset($this->data[$this->alias]['created']); | ||
return true; | ||
} | ||
|
||
public function beforeDelete($cascade = false) | ||
{ | ||
$album = $this->find('first', array( | ||
'fields' => array('band_id'), | ||
'conditions' => array('id' => $this->id) | ||
)); | ||
|
||
if (isset($album)) { | ||
$this->bandId = $album[$this->alias]['band_id']; | ||
} | ||
return true; | ||
} | ||
|
||
public function afterDelete() | ||
{ | ||
if (!empty($this->bandId)) { | ||
$neighbours = $this->find('count', array( | ||
'conditions' => array('band_id' => $this->bandId) | ||
)); | ||
|
||
if ($neighbours == 0) { | ||
$band = ClassRegistry::init('Band'); | ||
$band->delete($this->bandId); | ||
} | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
App::uses('AppModel', 'Model'); | ||
|
||
class Band extends AppModel | ||
{ | ||
public $hasMany = array('Album'); | ||
public $validate = array( | ||
'name' => array( | ||
'rule' => array('maxLength', 255), | ||
'required' => 'create', | ||
'allowEmpty' => false, | ||
'message' => 'The band\'s name cannot be empty or exceed 255 characters.' | ||
) | ||
); | ||
|
||
public function beforeValidate($options = array()) | ||
{ | ||
if (isset($this->data[$this->alias]['id'])) { | ||
$this->validator()->getField('name')->getRule(0)->allowEmpty = true; | ||
} | ||
} | ||
|
||
public function beforeSave($options = array()) | ||
{ | ||
if (isset($this->data[$this->alias]['id'])) { | ||
$this->data[$this->alias]['updated'] = date('Y-m-d H:i:s'); | ||
} else { | ||
unset($this->data[$this->alias]['updated']); | ||
} | ||
unset($this->data[$this->alias]['created']); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
App::uses('AppModel', 'Model'); | ||
|
||
class Track extends AppModel | ||
{ | ||
private $albumId; | ||
public $belongsTo = array('Album'); | ||
public $validate = array( | ||
'title' => array( | ||
'rule' => array('maxLength', 255), | ||
'message' => 'The title of a track cannot exceed 255 characters.' | ||
), | ||
'source_path' => array( | ||
'rule' => array('maxLength', 4096), | ||
'allowEmpty' => false, | ||
'required' => 'create', | ||
'message' => 'The source path of a file cannot be empty or exceed 4096 characters.' | ||
), | ||
'playtime' => array( | ||
'rule' => array('maxLength', 9), | ||
'message' => 'The playtime cannot exceed 9 characters.' | ||
), | ||
'track_number' => array( | ||
'rule' => array('naturalNumber', true), | ||
'message' => 'The track number must be a natural integer.' | ||
), | ||
'max_track_number' => array( | ||
'rule' => array('naturalNumber', true), | ||
'message' => 'The max track number must be a natural integer.' | ||
), | ||
'disc_number' => array( | ||
'rule' => array('naturalNumber', true), | ||
'message' => 'The disc number must be a natural integer.' | ||
), | ||
'max_disc_number' => array( | ||
'rule' => array('naturalNumber', true), | ||
'message' => 'The max disc number must be a natural integer.' | ||
), | ||
'year' => array( | ||
'rule' => array('date', 'y'), | ||
'allowEmpty' => true, | ||
'message' => 'Invalid track year.' | ||
|
||
), | ||
'genre' => array( | ||
'rule' => array('maxLength', 255), | ||
'message' => 'The genre of a track cannot exceed 255 characters.' | ||
), | ||
'artist' => array( | ||
'rule' => array('maxLength', 255), | ||
'message' => 'The artist value cannot exceed 255 characters.' | ||
) | ||
); | ||
|
||
public function beforeSave($options = array()) | ||
{ | ||
if (isset($this->data[$this->alias]['id'])) { | ||
$this->data[$this->alias]['updated'] = date('Y-m-d H:i:s'); | ||
} else { | ||
unset($this->data[$this->alias]['updated']); | ||
} | ||
unset($this->data[$this->alias]['created']); | ||
return true; | ||
} | ||
|
||
public function beforeDelete($cascade = false) | ||
{ | ||
$track = $this->find('first', array( | ||
'fields' => array('album_id'), | ||
'conditions' => array('id' => $this->id) | ||
)); | ||
|
||
if (isset($track)) { | ||
$this->albumId = $track[$this->alias]['album_id']; | ||
} | ||
return true; | ||
} | ||
|
||
public function afterDelete() | ||
{ | ||
if (!empty($this->albumId)) { | ||
$neighbours = $this->find('count', array( | ||
'conditions' => array('album_id' => $this->albumId) | ||
)); | ||
|
||
if ($neighbours == 0) { | ||
$album = ClassRegistry::init('Album'); | ||
$album->delete($this->albumId); | ||
} | ||
} | ||
return true; | ||
} | ||
} |