-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppModel.php
More file actions
executable file
·69 lines (56 loc) · 2.05 KB
/
Copy pathAppModel.php
File metadata and controls
executable file
·69 lines (56 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
/**
* Application model for Cake.
*
* This file is application-wide model file. You can put all
* application-wide model-related methods here.
*
* @link http://cakephp.org CakePHP(tm) Project
* @package app.Model
* @since CakePHP(tm) v 0.2.9
*/
App::uses('Model', 'Model');
/**
* Application model for Cake.
*
* Add your application-wide methods in the class below, your models
* will inherit them.
*
* @package app.Model
*/
class AppModel extends Model {
public function saveAssociated($data = null, $options = array()) {
foreach ($data as $alias => $modelData) {
if (!empty($this->hasAndBelongsToMany[$alias])) {
$habtm = array();
$emId = false;
$Model = ClassRegistry::init($this->hasAndBelongsToMany[$alias]['className']);
if (array_key_exists($alias,$options)) {
if ( isset($modelData[$options[$alias]['unique']])) {
$searched = $modelData[$options[$alias]['unique']];
$unique = ucfirst($options[$alias]['unique']);
$fb = 'findBy' . $unique;
}
$em = $Model->$fb($searched);
if (!empty($em)) {
$em = $em[$alias];
$emId = $em['id'];
}
}
foreach ($modelData as $modelDatum) {
if (empty($modelDatum['id'])) {
$Model->create();
}
if ($emId) {
$habtm[] = $emId;
} else {
$Model->save($modelData);
$habtm[] = empty($modelData['id']) ? $Model->getInsertID() : $modelData['id'];
}
}
$data[$alias] = array($alias => $habtm);
}
}
return parent::saveAssociated($data, $options);
}
}