Skip to content

Commit 4e03647

Browse files
committed
smart active record
1 parent 0155e5d commit 4e03647

File tree

3 files changed

+193
-0
lines changed

3 files changed

+193
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
/vendor/

SmartActiveRecord.php

+174
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
<?php
2+
/**
3+
* @author Alexey Samoylov <[email protected]>
4+
*
5+
* @package YiiSmart
6+
* @version 1.0
7+
*/
8+
9+
/**
10+
* Class SmartActiveRecord
11+
*
12+
* @property boolean isPosted
13+
*/
14+
abstract class SmartActiveRecord extends CActiveRecord
15+
{
16+
/**
17+
* Returns class name in static calls.
18+
*
19+
* @return string
20+
*/
21+
protected static function className()
22+
{
23+
return get_called_class();
24+
}
25+
26+
/**
27+
* @inheritdoc
28+
*/
29+
public static function model($className = __CLASS__)
30+
{
31+
return parent::model($className);
32+
}
33+
34+
/**
35+
* Loads model by id.
36+
* If not found, throws not found exception.
37+
*
38+
* @param $id
39+
* @return CActiveRecord
40+
* @throws CHttpException
41+
*/
42+
public static function loadModel($id)
43+
{
44+
$class = static::className();
45+
$model = $class::model()->findByPk((int)$id);
46+
if (!$model instanceof CActiveRecord)
47+
throw new CHttpException(404, Yii::t('app', 'The requested page does not exist.'));
48+
return $model;
49+
}
50+
51+
/**
52+
* Tries to save model, throwing an exception on any error (including validation).
53+
*
54+
* @param bool $runValidation
55+
* @param array|null $attributes
56+
* @throws CHttpException
57+
*/
58+
public function trySave($runValidation = true, $attributes = null)
59+
{
60+
$result = $this->save($runValidation, $attributes);
61+
if ($result === false) {
62+
$message = 'Object saving error. save() returned ' . CVarDumper::dumpAsString($result) .
63+
' Last DB error: ' . CVarDumper::dumpAsString($this->getDbConnection()->pdoInstance->errorInfo());
64+
65+
if (count($errors = $this->getErrors()) > 0)
66+
$message = 'Object saving error: ' . CVarDumper::dumpAsString($errors);
67+
68+
throw new CHttpException(500, $message);
69+
}
70+
}
71+
72+
/**
73+
* Validates model using ajax.
74+
*
75+
* @param string|null $form
76+
* @return bool
77+
*/
78+
public function performAjaxValidation($form = null)
79+
{
80+
if (Yii::app()->request->isAjaxRequest) {
81+
if ($form != null && isset($_POST['ajax']) && $_POST['ajax'] != $form)
82+
return false;
83+
echo CActiveForm::validate($this);
84+
Yii::app()->end();
85+
}
86+
return false;
87+
}
88+
89+
/**
90+
* Checks if the model was posted
91+
*
92+
* @return bool
93+
*/
94+
public function getIsPosted()
95+
{
96+
return isset($_POST[static::className()]);
97+
}
98+
99+
/**
100+
* Loads model attributes from POST data
101+
*
102+
* @param array|null $attributes
103+
*/
104+
public function loadPostData($attributes = null)
105+
{
106+
$class = static::className();
107+
if (isset($_POST[$class])) {
108+
$postData = $_POST[$class];
109+
if (is_array($attributes))
110+
$postData = array_intersect_key($postData, array_flip($attributes));
111+
$this->setAttributes($postData);
112+
}
113+
}
114+
115+
/**
116+
* Set only safe attributes.
117+
*
118+
* @param $attributes
119+
*/
120+
public function safeSetAttributes($attributes)
121+
{
122+
$this->setAttributes(array_intersect_key($attributes, array_flip($this->safeAttributeNames)));
123+
}
124+
125+
/**
126+
* Makes list data for using in CHtml::dropDownList() and so on.
127+
*
128+
* @param string $textField
129+
* @param string $valueField
130+
* @return array
131+
*/
132+
public static function listData($textField = 'name', $valueField = 'id')
133+
{
134+
return CHtml::listData(static::model()->findAll(['select' => [$valueField, $textField]]), $valueField, $textField);
135+
}
136+
137+
/**
138+
* Returns of class constants optionally filtered by prefix.
139+
*
140+
* @param $prefix
141+
* @return array
142+
*/
143+
public static function getConstants($prefix)
144+
{
145+
$class = static::className();
146+
$reflection = new ReflectionClass($class);
147+
148+
$result = [];
149+
foreach ($reflection->getConstants() as $name => $value) {
150+
if (preg_match("/^{$prefix}/i", $name)) {
151+
$result[$name] = $value;
152+
}
153+
}
154+
return $result;
155+
}
156+
157+
/**
158+
* Makes list data using class constants.
159+
*
160+
* @param string $prefix
161+
* @return array
162+
*/
163+
public static function listDataConstants($prefix = '')
164+
{
165+
$class = static::className();
166+
$constants = static::getConstants($prefix);
167+
168+
$result = [];
169+
foreach ($constants as $value) {
170+
$list[$value] = Yii::t('app', $class . '_' . $prefix . '_' . $value);
171+
}
172+
return $result;
173+
}
174+
}

composer.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "lagman/yii-smart",
3+
"description": "Collection of smart extensions for base Yii components",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "Alexey Samoylov",
8+
"email": "[email protected]",
9+
"homepage": "http://yiiblog.ru",
10+
"role": "Developer"
11+
}
12+
],
13+
"minimum-stability": "dev",
14+
"require": {
15+
"php":">=5.4.0"
16+
}
17+
}

0 commit comments

Comments
 (0)