1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* @author 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.components
|
8
|
|
|
*/
|
9
|
|
|
abstract class BAbstractModelCopier
|
10
|
|
|
{
|
11
|
|
|
/**
|
12
|
|
|
* @param BActiveRecord $model
|
13
|
|
|
* @param mixed $unsetAttributes
|
14
|
|
|
* @param mixed $setAttributes
|
15
|
|
|
*
|
16
|
|
|
* @return BActiveRecord
|
17
|
|
|
*/
|
18
|
4 |
|
protected function copyModel(BActiveRecord $model, $unsetAttributes = null, $setAttributes = null)
|
19
|
|
|
{
|
20
|
4 |
|
if( $unsetAttributes === null )
|
21
|
4 |
|
{
|
22
|
4 |
|
$unsetAttributes = array('visible');
|
23
|
|
|
|
24
|
4 |
|
if( ($pk = $this->getPrimaryColumn($model)) && is_string($pk) )
|
25
|
4 |
|
$unsetAttributes[] = $pk;
|
26
|
4 |
|
}
|
27
|
|
|
|
28
|
4 |
|
if( $setAttributes === null )
|
29
|
4 |
|
$setAttributes = array();
|
30
|
|
|
|
31
|
4 |
|
$copy = clone $model;
|
32
|
4 |
|
$copy->isNewRecord = true;
|
33
|
4 |
|
$this->unsetAttributes($copy, $unsetAttributes);
|
34
|
4 |
|
$this->setAttributes($copy, $setAttributes);
|
35
|
4 |
|
$copy->setScenario('copy');
|
36
|
4 |
|
$copy->save(false);
|
37
|
|
|
|
38
|
4 |
|
return $model->findByPk($copy->getPrimaryKey());
|
39
|
|
|
}
|
40
|
|
|
|
41
|
|
|
/**
|
42
|
|
|
* @param BActiveRecord $model
|
43
|
|
|
* @param array $attributes
|
44
|
|
|
*/
|
45
|
5 |
|
protected function unsetAttributes(BActiveRecord $model, array $attributes = array())
|
46
|
|
|
{
|
47
|
5 |
|
foreach($model->getValidators() as $validator)
|
48
|
5 |
|
if( $validator instanceof CUniqueValidator )
|
|
|
|
|
49
|
5 |
|
foreach($validator->attributes as $attribute)
|
50
|
5 |
|
$model->{$attribute} = null;
|
51
|
|
|
|
52
|
5 |
|
foreach($attributes as $attribute)
|
53
|
4 |
|
if( isset($model->{$attribute}) )
|
54
|
5 |
|
$model->{$attribute} = null;
|
55
|
5 |
|
}
|
56
|
|
|
|
57
|
|
|
/**
|
58
|
|
|
* @param BActiveRecord $model
|
59
|
|
|
* @param array $attributes
|
60
|
|
|
*/
|
61
|
5 |
|
protected function setAttributes(BActiveRecord $model, $attributes = array())
|
62
|
|
|
{
|
63
|
5 |
|
foreach($attributes as $attribute => $value)
|
64
|
4 |
|
if( $model->hasAttribute($attribute) )
|
65
|
4 |
|
$model->setAttribute($attribute, $value);
|
66
|
5 |
|
}
|
67
|
|
|
|
68
|
|
|
/**
|
69
|
|
|
* @param BActiveRecord $copyModel
|
70
|
|
|
* @param BActiveRecord $originModel
|
71
|
|
|
* @param $relationName
|
72
|
|
|
* @return bool
|
73
|
|
|
*/
|
74
|
3 |
|
protected function copyRelations(BActiveRecord $copyModel, BActiveRecord $originModel, $relationName)
|
75
|
|
|
{
|
76
|
3 |
|
$relation = $originModel->getActiveRelation($relationName);
|
77
|
3 |
|
$relatedModels = $originModel->getRelated($relationName);
|
78
|
|
|
|
79
|
3 |
|
if( !is_array($relatedModels) )
|
80
|
3 |
|
$relatedModels = array($relatedModels);
|
81
|
|
|
|
82
|
|
|
//todo: сделать обработку для составных внешних ключей
|
83
|
3 |
|
$fk = $relation->foreignKey;
|
84
|
3 |
|
if( is_array($fk) )
|
85
|
3 |
|
return false;
|
86
|
|
|
|
87
|
3 |
|
$result = true;
|
88
|
|
|
|
89
|
3 |
|
foreach($relatedModels as $relatedModel)
|
90
|
1 |
|
{
|
91
|
2 |
|
$pk = $this->getPrimaryColumn($relatedModel);
|
92
|
|
|
|
93
|
2 |
|
$copy = $this->copyModel(
|
94
|
2 |
|
$relatedModel,
|
95
|
2 |
|
CMap::mergeArray(array($fk), is_array($pk) ? array() : array($pk)),
|
96
|
2 |
|
array($fk => $copyModel->getPrimaryKey())
|
97
|
2 |
|
);
|
98
|
|
|
|
99
|
2 |
|
$result = $copy && $result;
|
100
|
3 |
|
}
|
101
|
|
|
|
102
|
3 |
|
return $result;
|
103
|
|
|
}
|
104
|
|
|
|
105
|
|
|
/**
|
106
|
|
|
* @param BActiveRecord $model
|
107
|
|
|
*
|
108
|
|
|
* @return array|string
|
109
|
|
|
*/
|
110
|
4 |
|
protected function getPrimaryColumn(BActiveRecord $model)
|
111
|
|
|
{
|
112
|
4 |
|
return $model->getMetaData()->tableSchema->primaryKey;
|
113
|
|
|
}
|
114
|
|
|
|
115
|
|
|
abstract public function copy();
|
116
|
|
|
} |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.