Klimov Paul
11 years ago
115 changed files with 2941 additions and 1624 deletions
@ -1,4 +1,41 @@
|
||||
Behaviors |
||||
========= |
||||
|
||||
TDB |
||||
A behavior (also knows as mixin) can be used to enhance the functionality of an existing component without modifying its |
||||
code. In particular, it can "inject" its own methods and properties into the component and make them directly accessible |
||||
via the component. It can also respond to the events triggered in the component and thus intercept the normal |
||||
code execution. Unlike PHP traits, behaviors could be attached to classes at runtime. |
||||
|
||||
Using behaviors |
||||
--------------- |
||||
|
||||
Behavior can be attached to any class that extends from `Component`. In order to do so you need to implement `behaviors` |
||||
method. Yii provides `AutoTimestamp` behavior that handles updating timestamp fields on saving active record model. |
||||
|
||||
```php |
||||
class User extends ActiveRecord |
||||
{ |
||||
// ... |
||||
|
||||
public function behaviors() |
||||
{ |
||||
return [ |
||||
'timestamp' => [ |
||||
'class' => 'yii\behaviors\AutoTimestamp', |
||||
'attributes' => [ |
||||
ActiveRecord::EVENT_BEFORE_INSERT => ['create_time', 'update_time'], |
||||
ActiveRecord::EVENT_BEFORE_UPDATE => 'update_time', |
||||
], |
||||
], |
||||
]; |
||||
} |
||||
} |
||||
``` |
||||
|
||||
In the above `class` value is a string containing fully qualified behavior class name. All the other key-values are |
||||
assigned to corresponding properties of the class. |
||||
|
||||
Creating your own behaviors |
||||
--------------------------- |
||||
|
||||
TBD |
@ -1,4 +1,83 @@
|
||||
Building console applications |
||||
============================= |
||||
Console applications |
||||
==================== |
||||
|
||||
TDB |
||||
Yii has full featured support of console. Console application structure in Yii is very similar to web application. It |
||||
consists of one or more [[\yii\console\Controller]] (often referred to as commands). Each has one or more actions. |
||||
|
||||
Usage |
||||
----- |
||||
|
||||
You can execute controller action using the following syntax: |
||||
|
||||
``` |
||||
yii <route> [--param1=value1 --param2 ...] |
||||
``` |
||||
|
||||
For example, `MigrationController::create` with `MigrationController::$migrationTable` set can be called from command |
||||
line like the following: |
||||
|
||||
``` |
||||
yii migreate/create --migrationTable=my_migration |
||||
``` |
||||
|
||||
In the above `yii` is console application entry script described below. |
||||
|
||||
Entry script |
||||
------------ |
||||
|
||||
Console application entry script is typically called `yii`, located in your application root directory and contains |
||||
code like the following: |
||||
|
||||
```php |
||||
#!/usr/bin/env php |
||||
<?php |
||||
/** |
||||
* Yii console bootstrap file. |
||||
* |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
defined('YII_DEBUG') or define('YII_DEBUG', true); |
||||
|
||||
// fcgi doesn't have STDIN defined by default |
||||
defined('STDIN') or define('STDIN', fopen('php://stdin', 'r')); |
||||
|
||||
require(__DIR__ . '/vendor/autoload.php'); |
||||
require(__DIR__ . '/vendor/yiisoft/yii2/yii/Yii.php'); |
||||
|
||||
$config = require(__DIR__ . '/config/console.php'); |
||||
|
||||
$application = new yii\console\Application($config); |
||||
$exitCode = $application->run(); |
||||
exit($exitCode); |
||||
|
||||
``` |
||||
|
||||
This script is a part of your application so you're free to adjust it. There `YII_DEBUG` can be turned off if you do |
||||
not want to see stacktrace on error and want to improve overall performance. In both basic and advanced application |
||||
templates it is enabled to provide more developer-friendly environment. |
||||
|
||||
Configuration |
||||
------------- |
||||
|
||||
As can be seen in the code above, console application uses its own config files named `console.php` so you need to |
||||
configure database connection and the rest of the components you're going to use there in that file. If web and console |
||||
application configs have a lot in common it's a good idea to move matching parts into their own config files as it was |
||||
done in advanced application template. |
||||
|
||||
|
||||
Creating your own console commands |
||||
---------------------------------- |
||||
|
||||
### Controller |
||||
|
||||
### Action |
||||
|
||||
### Parameters |
||||
|
||||
### Return codes |
||||
|
||||
Using return codes is the best practice of console application development. If command returns `0` it means everything |
||||
is OK. If it is a number more than zero, we have an error and integer returned is the error code. |
@ -0,0 +1,168 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\elasticsearch; |
||||
|
||||
use yii\debug\Panel; |
||||
use yii\log\Logger; |
||||
use yii\helpers\Html; |
||||
use yii\web\View; |
||||
|
||||
/** |
||||
* Debugger panel that collects and displays elasticsearch queries performed. |
||||
* |
||||
* @author Carsten Brandt <mail@cebe.cc> |
||||
* @since 2.0 |
||||
*/ |
||||
class DebugPanel extends Panel |
||||
{ |
||||
public function getName() |
||||
{ |
||||
return 'Elasticsearch'; |
||||
} |
||||
|
||||
public function getSummary() |
||||
{ |
||||
$timings = $this->calculateTimings(); |
||||
$queryCount = count($timings); |
||||
$queryTime = 0; |
||||
foreach ($timings as $timing) { |
||||
$queryTime += $timing[3]; |
||||
} |
||||
$queryTime = number_format($queryTime * 1000) . ' ms'; |
||||
$url = $this->getUrl(); |
||||
$output = <<<EOD |
||||
<div class="yii-debug-toolbar-block"> |
||||
<a href="$url" title="Executed $queryCount elasticsearch queries which took $queryTime."> |
||||
ES <span class="label">$queryCount</span> <span class="label">$queryTime</span> |
||||
</a> |
||||
</div> |
||||
EOD; |
||||
return $queryCount > 0 ? $output : ''; |
||||
} |
||||
|
||||
public function getDetail() |
||||
{ |
||||
$rows = []; |
||||
$i = 0; |
||||
foreach ($this->data['messages'] as $log) { |
||||
list ($message, $level, $category, $time, $traces) = $log; |
||||
if ($level == Logger::LEVEL_PROFILE_BEGIN) { |
||||
continue; |
||||
} |
||||
if (($pos = mb_strpos($message, "#")) !== false) { |
||||
$url = mb_substr($message, 0, $pos); |
||||
$body = mb_substr($message, $pos + 1); |
||||
} else { |
||||
$url = $message; |
||||
$body = null; |
||||
} |
||||
$traceString = ''; |
||||
if (!empty($traces)) { |
||||
$traceString .= Html::ul($traces, [ |
||||
'class' => 'trace', |
||||
'item' => function ($trace) { |
||||
return "<li>{$trace['file']}({$trace['line']})</li>"; |
||||
}, |
||||
]); |
||||
} |
||||
$runLinks = ''; |
||||
$c = 0; |
||||
\Yii::$app->elasticsearch->open(); |
||||
foreach(\Yii::$app->elasticsearch->nodes as $node) { |
||||
$pos = mb_strpos($url, ' '); |
||||
$type = mb_substr($url, 0, $pos); |
||||
if ($type == 'GET' && !empty($body)) { |
||||
$type = 'POST'; |
||||
} |
||||
$host = $node['http_address']; |
||||
if (strncmp($host, 'inet[/', 6) == 0) { |
||||
$host = substr($host, 6, -1); |
||||
} |
||||
$nodeUrl = 'http://' . $host . '/' . mb_substr($url, $pos + 1); |
||||
$nodeUrl .= (strpos($nodeUrl, '?') === false) ? '?pretty=true' : '&pretty=true'; |
||||
$nodeBody = json_encode($body); |
||||
\Yii::$app->view->registerJs(<<<JS |
||||
$('#elastic-link-$i-$c').on('click', function() { |
||||
$('#elastic-result-$i').html('Sending $type request to $nodeUrl...'); |
||||
$('#elastic-result-$i').parent('tr').show(); |
||||
$.ajax({ |
||||
type: "$type", |
||||
url: "$nodeUrl", |
||||
body: $nodeBody, |
||||
success: function( data ) { |
||||
$('#elastic-result-$i').html(data); |
||||
}, |
||||
dataType: "text" |
||||
}); |
||||
|
||||
return false; |
||||
}); |
||||
JS |
||||
, View::POS_READY); |
||||
$runLinks .= Html::a(isset($node['name']) ? $node['name'] : $node['http_address'], '#', ['id' => "elastic-link-$i-$c"]) . '<br/>'; |
||||
$c++; |
||||
} |
||||
$rows[] = "<tr><td style=\"width: 80%;\"><div><b>$url</b><br/><p>$body</p>$traceString</div></td><td style=\"width: 20%;\">$runLinks</td></tr><tr style=\"display: none;\"><td colspan=\"2\" id=\"elastic-result-$i\"></td></tr>"; |
||||
$i++; |
||||
} |
||||
$rows = implode("\n", $rows); |
||||
return <<<HTML |
||||
<h1>Elasticsearch Queries</h1> |
||||
|
||||
<table class="table table-condensed table-bordered table-striped table-hover" style="table-layout: fixed;"> |
||||
<thead> |
||||
<tr> |
||||
<th style="width: 80%;">Url / Query</th> |
||||
<th style="width: 20%;">Run Query on node</th> |
||||
</tr> |
||||
</thead> |
||||
<tbody> |
||||
$rows |
||||
</tbody> |
||||
</table> |
||||
HTML; |
||||
} |
||||
|
||||
private $_timings; |
||||
|
||||
protected function calculateTimings() |
||||
{ |
||||
if ($this->_timings !== null) { |
||||
return $this->_timings; |
||||
} |
||||
$messages = $this->data['messages']; |
||||
$timings = []; |
||||
$stack = []; |
||||
foreach ($messages as $i => $log) { |
||||
list($token, $level, $category, $timestamp) = $log; |
||||
$log[5] = $i; |
||||
if ($level == Logger::LEVEL_PROFILE_BEGIN) { |
||||
$stack[] = $log; |
||||
} elseif ($level == Logger::LEVEL_PROFILE_END) { |
||||
if (($last = array_pop($stack)) !== null && $last[0] === $token) { |
||||
$timings[$last[5]] = [count($stack), $token, $last[3], $timestamp - $last[3], $last[4]]; |
||||
} |
||||
} |
||||
} |
||||
|
||||
$now = microtime(true); |
||||
while (($last = array_pop($stack)) !== null) { |
||||
$delta = $now - $last[3]; |
||||
$timings[$last[5]] = [count($stack), $last[0], $last[2], $delta, $last[4]]; |
||||
} |
||||
ksort($timings); |
||||
return $this->_timings = $timings; |
||||
} |
||||
|
||||
public function save() |
||||
{ |
||||
$target = $this->module->logTarget; |
||||
$messages = $target->filterMessages($target->messages, Logger::LEVEL_PROFILE, ['yii\elasticsearch\Connection::httpRequest']); |
||||
return ['messages' => $messages]; |
||||
} |
||||
} |
@ -1,7 +1,7 @@
|
||||
Yii Framework 2 Change Log |
||||
========================== |
||||
|
||||
Version 2 |
||||
--------- |
||||
Version 2.0 alpha |
||||
----------------- |
||||
|
||||
- Initial release. |
||||
- Initial release. |
||||
|
@ -1,21 +1,24 @@
|
||||
Yii 2.0 Public Preview |
||||
====================== |
||||
Yii PHP Framework Version 2 |
||||
=========================== |
||||
|
||||
Thank you for choosing Yii - a high-performance component-based PHP framework. |
||||
This is the core framework code of [Yii 2](https://github.com/yiisoft/yii2). |
||||
|
||||
If you are looking for a production-ready PHP framework, please use |
||||
[Yii v1.1](https://github.com/yiisoft/yii). |
||||
|
||||
Yii 2.0 is still under heavy development. We may make significant changes |
||||
without prior notices. **Yii 2.0 is not ready for production use yet.** |
||||
|
||||
[![Build Status](https://secure.travis-ci.org/yiisoft/yii2.png)](http://travis-ci.org/yiisoft/yii2) |
||||
Installation |
||||
------------ |
||||
|
||||
The preferred way to install this extension is through [composer](http://getcomposer.org/download/). |
||||
|
||||
Either run |
||||
|
||||
REQUIREMENTS |
||||
------------ |
||||
``` |
||||
php composer.phar require yiisoft/yii2-framework "*" |
||||
``` |
||||
|
||||
The minimum requirement by Yii is that your Web server supports PHP 5.4.0. |
||||
or add |
||||
|
||||
```json |
||||
"yiisoft/yii2-framework": "*" |
||||
``` |
||||
|
||||
to the require section of your composer.json. |
||||
|
@ -0,0 +1,160 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\data; |
||||
|
||||
use Yii; |
||||
use yii\base\InvalidConfigException; |
||||
use yii\base\Model; |
||||
use yii\db\Connection; |
||||
|
||||
/** |
||||
* SqlDataProvider implements a data provider based on a plain SQL statement. |
||||
* |
||||
* SqlDataProvider provides data in terms of arrays, each representing a row of query result. |
||||
* |
||||
* Like other data providers, SqlDataProvider also supports sorting and pagination. |
||||
* It does so by modifying the given [[sql]] statement with "ORDER BY" and "LIMIT" |
||||
* clauses. You may configure the [[sort]] and [[pagination]] properties to |
||||
* customize sorting and pagination behaviors. |
||||
* |
||||
* SqlDataProvider may be used in the following way: |
||||
* |
||||
* ~~~ |
||||
* $count = Yii::$app->db->createCommand(' |
||||
* SELECT COUNT(*) FROM tbl_user WHERE status=:status |
||||
* ', [':status' => 1])->queryScalar(); |
||||
* |
||||
* $dataProvider = new SqlDataProvider([ |
||||
* 'sql' => 'SELECT * FROM tbl_user WHERE status=:status', |
||||
* 'params' => [':status' => 1], |
||||
* 'totalCount' => $count, |
||||
* 'sort' => [ |
||||
* 'attributes' => [ |
||||
* 'age', |
||||
* 'name' => [ |
||||
* 'asc' => ['first_name' => SORT_ASC, 'last_name' => SORT_ASC], |
||||
* 'desc' => ['first_name' => SORT_DESC, 'last_name' => SORT_DESC], |
||||
* 'default' => SORT_DESC, |
||||
* 'label' => 'Name', |
||||
* ], |
||||
* ], |
||||
* ], |
||||
* 'pagination' => [ |
||||
* 'pageSize' => 20, |
||||
* ], |
||||
* ]); |
||||
* |
||||
* // get the user records in the current page |
||||
* $models = $dataProvider->getModels(); |
||||
* ~~~ |
||||
* |
||||
* Note: if you want to use the pagination feature, you must configure the [[totalCount]] property |
||||
* to be the total number of rows (without pagination). And if you want to use the sorting feature, |
||||
* you must configure the [[sort]] property so that the provider knows which columns can be sorted. |
||||
* |
||||
* @author Qiang Xue <qiang.xue@gmail.com> |
||||
* @since 2.0 |
||||
*/ |
||||
class SqlDataProvider extends BaseDataProvider |
||||
{ |
||||
/** |
||||
* @var Connection|string the DB connection object or the application component ID of the DB connection. |
||||
* If not set, the default DB connection will be used. |
||||
*/ |
||||
public $db; |
||||
/** |
||||
* @var string the SQL statement to be used for fetching data rows. |
||||
*/ |
||||
public $sql; |
||||
/** |
||||
* @var array parameters (name=>value) to be bound to the SQL statement. |
||||
*/ |
||||
public $params = []; |
||||
/** |
||||
* @var string|callable the column that is used as the key of the data models. |
||||
* This can be either a column name, or a callable that returns the key value of a given data model. |
||||
* |
||||
* If this is not set, the keys of the [[models]] array will be used. |
||||
*/ |
||||
public $key; |
||||
|
||||
|
||||
/** |
||||
* Initializes the DB connection component. |
||||
* This method will initialize the [[db]] property to make sure it refers to a valid DB connection. |
||||
* @throws InvalidConfigException if [[db]] is invalid. |
||||
*/ |
||||
public function init() |
||||
{ |
||||
parent::init(); |
||||
if (is_string($this->db)) { |
||||
$this->db = Yii::$app->getComponent($this->db); |
||||
} |
||||
if (!$this->db instanceof Connection) { |
||||
throw new InvalidConfigException('The "db" property must be a valid DB Connection application component.'); |
||||
} |
||||
if ($this->sql === null) { |
||||
throw new InvalidConfigException('The "sql" property must be set.'); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* @inheritdoc |
||||
*/ |
||||
protected function prepareModels() |
||||
{ |
||||
$sql = $this->sql; |
||||
$qb = $this->db->getQueryBuilder(); |
||||
if (($sort = $this->getSort()) !== false) { |
||||
$orderBy = $qb->buildOrderBy($sort->getOrders()); |
||||
if (!empty($orderBy)) { |
||||
$orderBy = substr($orderBy, 9); |
||||
if (preg_match('/\s+order\s+by\s+[\w\s,\.]+$/i', $sql)) { |
||||
$sql .= ', ' . $orderBy; |
||||
} else { |
||||
$sql .= ' ORDER BY ' . $orderBy; |
||||
} |
||||
} |
||||
} |
||||
|
||||
if (($pagination = $this->getPagination()) !== false) { |
||||
$pagination->totalCount = $this->getTotalCount(); |
||||
$sql .= ' ' . $qb->buildLimit($pagination->getLimit(), $pagination->getOffset()); |
||||
} |
||||
|
||||
return $this->db->createCommand($sql, $this->params)->queryAll(); |
||||
} |
||||
|
||||
/** |
||||
* @inheritdoc |
||||
*/ |
||||
protected function prepareKeys($models) |
||||
{ |
||||
$keys = []; |
||||
if ($this->key !== null) { |
||||
foreach ($models as $model) { |
||||
if (is_string($this->key)) { |
||||
$keys[] = $model[$this->key]; |
||||
} else { |
||||
$keys[] = call_user_func($this->key, $model); |
||||
} |
||||
} |
||||
return $keys; |
||||
} else { |
||||
return array_keys($models); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* @inheritdoc |
||||
*/ |
||||
protected function prepareTotalCount() |
||||
{ |
||||
return 0; |
||||
} |
||||
} |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,279 @@
|
||||
<?php |
||||
/** |
||||
* |
||||
* |
||||
* @author Carsten Brandt <mail@cebe.cc> |
||||
*/ |
||||
|
||||
namespace yii\db; |
||||
|
||||
/** |
||||
* ActiveRecordInterface |
||||
* |
||||
* @author Qiang Xue <qiang.xue@gmail.com> |
||||
* @author Carsten Brandt <mail@cebe.cc> |
||||
* @since 2.0 |
||||
*/ |
||||
interface ActiveRecordInterface |
||||
{ |
||||
/** |
||||
* Returns the primary key **name(s)** for this AR class. |
||||
* |
||||
* Note that an array should be returned even when the record only has a single primary key. |
||||
* |
||||
* For the primary key **value** see [[getPrimaryKey()]] instead. |
||||
* |
||||
* @return string[] the primary key name(s) for this AR class. |
||||
*/ |
||||
public static function primaryKey(); |
||||
|
||||
/** |
||||
* Returns the list of all attribute names of the record. |
||||
* @return array list of attribute names. |
||||
*/ |
||||
public function attributes(); |
||||
|
||||
/** |
||||
* Returns the named attribute value. |
||||
* If this record is the result of a query and the attribute is not loaded, |
||||
* null will be returned. |
||||
* @param string $name the attribute name |
||||
* @return mixed the attribute value. Null if the attribute is not set or does not exist. |
||||
* @see hasAttribute() |
||||
*/ |
||||
public function getAttribute($name); |
||||
|
||||
/** |
||||
* Sets the named attribute value. |
||||
* @param string $name the attribute name. |
||||
* @param mixed $value the attribute value. |
||||
* @see hasAttribute() |
||||
*/ |
||||
public function setAttribute($name, $value); |
||||
|
||||
/** |
||||
* Returns a value indicating whether the record has an attribute with the specified name. |
||||
* @param string $name the name of the attribute |
||||
* @return boolean whether the record has an attribute with the specified name. |
||||
*/ |
||||
public function hasAttribute($name); |
||||
|
||||
/** |
||||
* Returns the primary key value(s). |
||||
* @param boolean $asArray whether to return the primary key value as an array. If true, |
||||
* the return value will be an array with attribute names as keys and attribute values as values. |
||||
* Note that for composite primary keys, an array will always be returned regardless of this parameter value. |
||||
* @return mixed the primary key value. An array (attribute name => attribute value) is returned if the primary key |
||||
* is composite or `$asArray` is true. A string is returned otherwise (null will be returned if |
||||
* the key value is null). |
||||
*/ |
||||
public function getPrimaryKey($asArray = false); |
||||
|
||||
/** |
||||
* Creates an [[ActiveQueryInterface|ActiveQuery]] instance for query purpose. |
||||
* |
||||
* This method is usually ment to be used like this: |
||||
* |
||||
* ```php |
||||
* Customer::find(1); // find one customer by primary key |
||||
* Customer::find()->all(); // find all customers |
||||
* ``` |
||||
* |
||||
* @param mixed $q the query parameter. This can be one of the followings: |
||||
* |
||||
* - a scalar value (integer or string): query by a single primary key value and return the |
||||
* corresponding record. |
||||
* - an array of name-value pairs: query by a set of attribute values and return a single record matching all of them. |
||||
* - null (not specified): return a new [[ActiveQuery]] object for further query purpose. |
||||
* |
||||
* @return ActiveQueryInterface|static|null When `$q` is null, a new [[ActiveQuery]] instance |
||||
* is returned; when `$q` is a scalar or an array, an ActiveRecord object matching it will be |
||||
* returned (null will be returned if there is no matching). |
||||
*/ |
||||
public static function find($q = null); |
||||
|
||||
/** |
||||
* Creates an [[ActiveQueryInterface|ActiveQuery]] instance. |
||||
* This method is called by [[find()]] to start a SELECT query. |
||||
* You may override this method to return a customized query (e.g. `CustomerQuery` specified |
||||
* written for querying `Customer` purpose.) |
||||
* @return ActiveQueryInterface the newly created [[ActiveQueryInterface|ActiveQuery]] instance. |
||||
*/ |
||||
public static function createQuery(); |
||||
|
||||
/** |
||||
* Updates records using the provided attribute values and conditions. |
||||
* For example, to change the status to be 1 for all customers whose status is 2: |
||||
* |
||||
* ~~~ |
||||
* Customer::updateAll(['status' => 1], ['status' => '2']); |
||||
* ~~~ |
||||
* |
||||
* @param array $attributes attribute values (name-value pairs) to be saved for the record. |
||||
* Unlike [[update()]] these are not going to be validated. |
||||
* @param array $condition the condition that matches the records that should get updated. |
||||
* Please refer to [[QueryInterface::where()]] on how to specify this parameter. |
||||
* An empty condition will match all records. |
||||
* @return integer the number of rows updated |
||||
*/ |
||||
public static function updateAll($attributes, $condition = null); |
||||
|
||||
/** |
||||
* Deletes records using the provided conditions. |
||||
* WARNING: If you do not specify any condition, this method will delete ALL rows in the table. |
||||
* |
||||
* For example, to delete all customers whose status is 3: |
||||
* |
||||
* ~~~ |
||||
* Customer::deleteAll([status = 3]); |
||||
* ~~~ |
||||
* |
||||
* @param array $condition the condition that matches the records that should get deleted. |
||||
* Please refer to [[QueryInterface::where()]] on how to specify this parameter. |
||||
* An empty condition will match all records. |
||||
* @return integer the number of rows deleted |
||||
*/ |
||||
public static function deleteAll($condition = null); |
||||
|
||||
/** |
||||
* Saves the current record. |
||||
* |
||||
* This method will call [[insert()]] when [[isNewRecord]] is true, or [[update()]] |
||||
* when [[isNewRecord]] is false. |
||||
* |
||||
* For example, to save a customer record: |
||||
* |
||||
* ~~~ |
||||
* $customer = new Customer; // or $customer = Customer::find($id); |
||||
* $customer->name = $name; |
||||
* $customer->email = $email; |
||||
* $customer->save(); |
||||
* ~~~ |
||||
* |
||||
* @param boolean $runValidation whether to perform validation before saving the record. |
||||
* If the validation fails, the record will not be saved to database. `false` will be returned |
||||
* in this case. |
||||
* @param array $attributes list of attributes that need to be saved. Defaults to null, |
||||
* meaning all attributes that are loaded from DB will be saved. |
||||
* @return boolean whether the saving succeeds |
||||
*/ |
||||
public function save($runValidation = true, $attributes = null); |
||||
|
||||
/** |
||||
* Inserts the record into the database using the attribute values of this record. |
||||
* |
||||
* Usage example: |
||||
* |
||||
* ```php |
||||
* $customer = new Customer; |
||||
* $customer->name = $name; |
||||
* $customer->email = $email; |
||||
* $customer->insert(); |
||||
* ``` |
||||
* |
||||
* @param boolean $runValidation whether to perform validation before saving the record. |
||||
* If the validation fails, the record will not be inserted into the database. |
||||
* @param array $attributes list of attributes that need to be saved. Defaults to null, |
||||
* meaning all attributes that are loaded from DB will be saved. |
||||
* @return boolean whether the attributes are valid and the record is inserted successfully. |
||||
*/ |
||||
public function insert($runValidation = true, $attributes = null); |
||||
|
||||
/** |
||||
* Saves the changes to this active record into the database. |
||||
* |
||||
* Usage example: |
||||
* |
||||
* ```php |
||||
* $customer = Customer::find($id); |
||||
* $customer->name = $name; |
||||
* $customer->email = $email; |
||||
* $customer->update(); |
||||
* ``` |
||||
* |
||||
* @param boolean $runValidation whether to perform validation before saving the record. |
||||
* If the validation fails, the record will not be inserted into the database. |
||||
* @param array $attributes list of attributes that need to be saved. Defaults to null, |
||||
* meaning all attributes that are loaded from DB will be saved. |
||||
* @return integer|boolean the number of rows affected, or false if validation fails |
||||
* or updating process is stopped for other reasons. |
||||
* Note that it is possible that the number of rows affected is 0, even though the |
||||
* update execution is successful. |
||||
*/ |
||||
public function update($runValidation = true, $attributes = null); |
||||
|
||||
/** |
||||
* Deletes the record from the database. |
||||
* |
||||
* @return integer|boolean the number of rows deleted, or false if the deletion is unsuccessful for some reason. |
||||
* Note that it is possible that the number of rows deleted is 0, even though the deletion execution is successful. |
||||
*/ |
||||
public function delete(); |
||||
|
||||
/** |
||||
* Returns a value indicating whether the current record is new (not saved in the database). |
||||
* @return boolean whether the record is new and should be inserted when calling [[save()]]. |
||||
*/ |
||||
public function getIsNewRecord(); |
||||
|
||||
/** |
||||
* Returns a value indicating whether the given active record is the same as the current one. |
||||
* Two [[isNewRecord|new]] records are considered to be not equal. |
||||
* @param static $record record to compare to |
||||
* @return boolean whether the two active records refer to the same row in the same database table. |
||||
*/ |
||||
public function equals($record); |
||||
|
||||
/** |
||||
* Creates an [[ActiveRelationInterface|ActiveRelation]] instance. |
||||
* This method is called by [[BaseActiveRecord::hasOne()]] and [[BaseActiveRecord::hasMany()]] to |
||||
* create a relation instance. |
||||
* You may override this method to return a customized relation. |
||||
* @param array $config the configuration passed to the ActiveRelation class. |
||||
* @return ActiveRelation the newly created [[ActiveRelation]] instance. |
||||
*/ |
||||
public static function createActiveRelation($config = []); |
||||
|
||||
/** |
||||
* Returns the relation object with the specified name. |
||||
* A relation is defined by a getter method which returns an [[ActiveRelationInterface|ActiveRelation]] object. |
||||
* It can be declared in either the ActiveRecord class itself or one of its behaviors. |
||||
* @param string $name the relation name |
||||
* @return ActiveRelation the relation object |
||||
*/ |
||||
public function getRelation($name); |
||||
|
||||
/** |
||||
* Establishes the relationship between two records. |
||||
* |
||||
* The relationship is established by setting the foreign key value(s) in one record |
||||
* to be the corresponding primary key value(s) in the other record. |
||||
* The record with the foreign key will be saved into database without performing validation. |
||||
* |
||||
* If the relationship involves a pivot table, a new row will be inserted into the |
||||
* pivot table which contains the primary key values from both records. |
||||
* |
||||
* This method requires that the primary key value is not null. |
||||
* |
||||
* @param string $name the case sensitive name of the relationship. |
||||
* @param static $model the record to be linked with the current one. |
||||
* @param array $extraColumns additional column values to be saved into the pivot table. |
||||
* This parameter is only meaningful for a relationship involving a pivot table |
||||
* (i.e., a relation set with `[[ActiveRelationInterface::via()]]`.) |
||||
*/ |
||||
public function link($name, $model, $extraColumns = []); |
||||
|
||||
/** |
||||
* Destroys the relationship between two records. |
||||
* |
||||
* The record with the foreign key of the relationship will be deleted if `$delete` is true. |
||||
* Otherwise, the foreign key will be set null and the record will be saved without validation. |
||||
* |
||||
* @param string $name the case sensitive name of the relationship. |
||||
* @param static $model the model to be unlinked from the current one. |
||||
* @param boolean $delete whether to delete the model that contains the foreign key. |
||||
* If false, the model's foreign key will be set null and saved. |
||||
* If true, the model containing the foreign key will be deleted. |
||||
*/ |
||||
public function unlink($name, $model, $delete = false); |
||||
} |
File diff suppressed because it is too large
Load Diff
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue