1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* @author Nikita Melnikov <[email protected]>, Sergey Glagolev <[email protected]>
|
4
|
|
|
* @link https://github.com/shogodev/argilla/
|
5
|
|
|
* @copyright Copyright © 2003-2014 Shogo
|
6
|
|
|
* @license http://argilla.ru/LICENSE
|
7
|
|
|
* @package backend.models
|
8
|
|
|
*
|
9
|
|
|
* @method static BAssociation model(string $class = __CLASS__)
|
10
|
|
|
*
|
11
|
|
|
* @property string $src
|
12
|
|
|
* @property string $src_frontend
|
13
|
|
|
* @property integer $src_id
|
14
|
|
|
* @property string $dst
|
15
|
|
|
* @property string $dst_frontend
|
16
|
|
|
* @property integer $dst_id
|
17
|
|
|
*/
|
18
|
|
|
class BAssociation extends BActiveRecord
|
19
|
|
|
{
|
20
|
|
|
/**
|
21
|
|
|
* @return string
|
22
|
|
|
*/
|
23
|
3 |
|
public function tableName()
|
24
|
|
|
{
|
25
|
3 |
|
return '{{association}}';
|
26
|
|
|
}
|
27
|
|
|
|
28
|
|
|
/**
|
29
|
|
|
* @param BActiveRecord $model
|
30
|
|
|
* @param string $dst
|
31
|
|
|
* @param array $ids
|
32
|
|
|
* @param bool $deleteAssociations
|
33
|
|
|
*/
|
34
|
3 |
|
public function updateAssociations(BActiveRecord $model, $dst, array $ids, $deleteAssociations = true)
|
35
|
|
|
{
|
36
|
|
|
if( $deleteAssociations )
|
37
|
3 |
|
self::model()->deleteAssociations($model, $dst);
|
38
|
|
|
|
39
|
3 |
|
foreach($ids as $id)
|
40
|
|
|
{
|
41
|
3 |
|
$association = new BAssociation();
|
42
|
3 |
|
$association->src = $this->getSrc($model);
|
43
|
3 |
|
$association->src_id = $model->getPrimaryKey();
|
44
|
3 |
|
$association->dst = $dst;
|
45
|
3 |
|
$association->dst_id = $id;
|
46
|
|
|
|
47
|
3 |
|
if( $model instanceof IHasFrontendModel )
|
48
|
3 |
|
$association->src_frontend = $model->getFrontendModelName();
|
49
|
|
|
|
50
|
3 |
|
$dstModel = new $dst();
|
51
|
3 |
|
if( $dstModel instanceof IHasFrontendModel )
|
52
|
3 |
|
$association->dst_frontend = $dstModel->getFrontendModelName();
|
53
|
|
|
|
54
|
3 |
|
$association->save();
|
55
|
3 |
|
}
|
56
|
3 |
|
}
|
57
|
|
|
|
58
|
|
|
/**
|
59
|
|
|
* @param BActiveRecord $model
|
60
|
|
|
* @param null|string $dst
|
61
|
|
|
* @param null|string $dstId
|
62
|
|
|
*/
|
63
|
3 |
|
public function deleteAssociations(BActiveRecord $model, $dst = null, $dstId = null)
|
64
|
|
|
{
|
65
|
3 |
|
$src = $this->getSrc($model);
|
66
|
3 |
|
$srcId = $model->getPrimaryKey();
|
67
|
|
|
|
68
|
3 |
|
$criteria = new CDbCriteria();
|
69
|
3 |
|
$criteria->compare('src', $src);
|
70
|
3 |
|
$criteria->compare('src_id', $srcId);
|
71
|
|
|
|
72
|
|
|
if( $dst )
|
|
|
|
|
73
|
3 |
|
$criteria->compare('dst', $dst);
|
74
|
|
|
|
75
|
|
|
if( $dstId )
|
|
|
|
|
76
|
3 |
|
$criteria->compare('dst_id', $dstId);
|
77
|
|
|
|
78
|
3 |
|
$this->deleteAll($criteria);
|
79
|
3 |
|
}
|
80
|
|
|
|
81
|
|
|
/**
|
82
|
|
|
* @param BActiveRecord $model
|
83
|
|
|
*
|
84
|
|
|
* @return string
|
85
|
|
|
*/
|
86
|
5 |
|
public function getSrc(BActiveRecord $model)
|
87
|
|
|
{
|
88
|
5 |
|
return get_class($model);
|
89
|
|
|
}
|
90
|
|
|
|
91
|
|
|
public function getChecked($parameters)
|
92
|
|
|
{
|
93
|
|
|
$criteria = new CDbCriteria();
|
94
|
|
|
|
95
|
|
|
foreach($parameters as $key => $value)
|
96
|
|
|
$criteria->compare($key, $value);
|
97
|
|
|
|
98
|
|
|
return $this->count($criteria) ? true : false;
|
99
|
|
|
}
|
100
|
|
|
|
101
|
|
|
public function getAssociatedKeys($data = null)
|
102
|
|
|
{
|
103
|
|
View Code Duplication |
if( !isset($data) )
|
|
|
|
|
104
|
|
|
{
|
105
|
|
|
$data = Arr::extract($_GET, array('src', 'dst', 'srcId'));
|
106
|
|
|
}
|
107
|
|
|
|
108
|
|
|
if( isset($data['srcId']) )
|
109
|
|
|
{
|
110
|
|
|
$data['src_id'] = Arr::cut($data, 'srcId');
|
|
|
|
|
111
|
|
|
}
|
112
|
|
|
|
113
|
|
|
return CHtml::listData(self::model()->findAllByAttributes($data), 'dst_id', 'dst_id');
|
114
|
|
|
}
|
115
|
|
|
} |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: