Commit 26717e3e by Carsten Brandt

allow referencing the guide from API docs

parent e9b513d4
ActiveRecord implements the [Active Record design pattern](http://en.wikipedia.org/wiki/Active_record).
The idea is that an ActiveRecord object is associated with a row in a database table
so object properties are mapped to columns of the corresponding database row.
For example, a `Customer` object is associated with a row in the `tbl_customer`
table. Instead of writing raw SQL statements to access the data in the table,
you can call intuitive methods available in the corresponding ActiveRecord class
to achieve the same goals. For example, calling [[save()]] would insert or update a row
in the underlying table:
~~~
$customer = new Customer();
$customer->name = 'Qiang';
$customer->save();
~~~
### Declaring ActiveRecord Classes
To declare an ActiveRecord class you need to extend [[\yii\db\ActiveRecord]] and
implement `tableName` method like the following:
~~~
class Customer extends \yii\db\ActiveRecord
{
/**
* @return string the name of the table associated with this ActiveRecord class.
*/
public static function tableName()
{
return 'tbl_customer';
}
}
~~~
### Connecting to Database
ActiveRecord relies on a [[Connection|DB connection]]. By default, it assumes that
there is an application component named `db` that gives the needed [[Connection]]
instance which serves as the DB connection. Usually this component is configured
via application configuration like the following:
~~~
return [
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=testdb',
'username' => 'demo',
'password' => 'demo',
// turn on schema caching to improve performance
// 'schemaCacheDuration' => 3600,
],
],
];
~~~
### Getting Data from Database
There are two ActiveRecord methods for getting data:
- [[find()]]
- [[findBySql()]]
They both return an [[ActiveQuery]] instance. Coupled with the various customization and query methods
provided by [[ActiveQuery]], ActiveRecord supports very flexible and powerful data retrieval approaches.
The followings are some examples,
~~~
// to retrieve all *active* customers and order them by their ID:
$customers = Customer::find()
->where(['status' => $active])
->orderBy('id')
->all();
// to return a single customer whose ID is 1:
$customer = Customer::find()
->where(['id' => 1])
->one();
// or use the following shortcut approach:
$customer = Customer::find(1);
// to retrieve customers using a raw SQL statement:
$sql = 'SELECT * FROM tbl_customer';
$customers = Customer::findBySql($sql)->all();
// to return the number of *active* customers:
$count = Customer::find()
->where(['status' => $active])
->count();
// to return customers in terms of arrays rather than `Customer` objects:
$customers = Customer::find()->asArray()->all();
// each $customers element is an array of name-value pairs
// to index the result by customer IDs:
$customers = Customer::find()->indexBy('id')->all();
// $customers array is indexed by customer IDs
~~~
### Accessing Column Data
ActiveRecord maps each column of the corresponding database table row to an *attribute* in the ActiveRecord
object. An attribute is like a regular object property whose name is the same as the corresponding column
name and is case sensitive.
To read the value of a column, we can use the following expression:
~~~
// "id" is the name of a column in the table associated with $customer ActiveRecord object
$id = $customer->id;
// or alternatively,
$id = $customer->getAttribute('id');
~~~
We can get all column values through the [[attributes]] property:
~~~
$values = $customer->attributes;
~~~
### Persisting Data to Database
ActiveRecord provides the following methods to insert, update and delete data:
- [[save()]]
- [[insert()]]
- [[update()]]
- [[delete()]]
- [[updateCounters()]]
- [[updateAll()]]
- [[updateAllCounters()]]
- [[deleteAll()]]
Note that [[updateAll()]], [[updateAllCounters()]] and [[deleteAll()]] apply to the whole database
table, while the rest of the methods only apply to the row associated with the ActiveRecord object.
The followings are some examples:
~~~
// to insert a new customer record
$customer = new Customer;
$customer->name = 'James';
$customer->email = 'james@example.com';
$customer->save(); // equivalent to $customer->insert();
// to update an existing customer record
$customer = Customer::find($id);
$customer->email = 'james@example.com';
$customer->save(); // equivalent to $customer->update();
// to delete an existing customer record
$customer = Customer::find($id);
$customer->delete();
// to increment the age of all customers by 1
Customer::updateAllCounters(['age' => 1]);
~~~
### Getting Relational Data
Using ActiveRecord you can expose relationships as properties. For example,
with an appropriate declaration, `$customer->orders` can return an array of `Order` objects
which represent the orders placed by the specified customer.
To declare a relationship, define a getter method which returns an [[ActiveRelation]] object. For example,
~~~
class Customer extends \yii\db\ActiveRecord
{
public function getOrders()
{
return $this->hasMany(Order::className(), ['customer_id' => 'id']);
}
}
class Order extends \yii\db\ActiveRecord
{
public function getCustomer()
{
return $this->hasOne(Customer::className(), ['id' => 'customer_id']);
}
}
~~~
Within the getter methods above, we call [[hasMany()]] or [[hasOne()]] methods to
create a new [[ActiveRelation]] object. The [[hasMany()]] method declares
a one-many relationship. For example, a customer has many orders. And the [[hasOne()]]
method declares a many-one or one-one relationship. For example, an order has one customer.
Both methods take two parameters:
- `$class`: the name of the class that the related models should use.
- `$link`: the association between columns from two tables. This should be given as an array.
The keys of the array are the names of the columns from the table associated with `$class`,
while the values of the array are the names of the columns from the declaring class.
It is a good practice to define relationships based on table foreign keys.
After declaring relationships getting relational data is as easy as accessing
a component property that is defined by the getter method:
~~~
// the orders of a customer
$customer = Customer::find($id);
$orders = $customer->orders; // $orders is an array of Order objects
// the customer of the first order
$customer2 = $orders[0]->customer; // $customer == $customer2
~~~
Because [[ActiveRelation]] extends from [[ActiveQuery]], it has the same query building methods,
which allows us to customize the query for retrieving the related objects.
For example, we may declare a `bigOrders` relationship which returns orders whose
subtotal exceeds certain amount:
~~~
class Customer extends \yii\db\ActiveRecord
{
public function getBigOrders($threshold = 100)
{
return $this->hasMany(Order::className(), ['customer_id' => 'id'])
->where('subtotal > :threshold', [':threshold' => $threshold])
->orderBy('id');
}
}
~~~
Sometimes, two tables are related together via an intermediary table called
[pivot table](http://en.wikipedia.org/wiki/Pivot_table). To declare such relationships, we can customize
the [[ActiveRelation]] object by calling its [[ActiveRelation::via()]] or [[ActiveRelation::viaTable()]]
method.
For example, if table `tbl_order` and table `tbl_item` are related via pivot table `tbl_order_item`,
we can declare the `items` relation in the `Order` class like the following:
~~~
class Order extends \yii\db\ActiveRecord
{
public function getItems()
{
return $this->hasMany(Item::className(), ['id' => 'item_id'])
->viaTable('tbl_order_item', ['order_id' => 'id']);
}
}
~~~
[[ActiveRelation::via()]] method is similar to [[ActiveRelation::viaTable()]] except that
the first parameter of [[ActiveRelation::via()]] takes a relation name declared in the ActiveRecord class.
For example, the above `items` relation can be equivalently declared as follows:
~~~
class Order extends \yii\db\ActiveRecord
{
public function getOrderItems()
{
return $this->hasMany(OrderItem::className(), ['order_id' => 'id']);
}
public function getItems()
{
return $this->hasMany(Item::className(), ['id' => 'item_id'])
->via('orderItems');
}
}
~~~
When you access the related objects the first time, behind the scene ActiveRecord performs a DB query
to retrieve the corresponding data and populate it into the related objects. No query will be performed
if you access the same related objects again. We call this *lazy loading*. For example,
~~~
// SQL executed: SELECT * FROM tbl_customer WHERE id=1
$customer = Customer::find(1);
// SQL executed: SELECT * FROM tbl_order WHERE customer_id=1
$orders = $customer->orders;
// no SQL executed
$orders2 = $customer->orders;
~~~
Lazy loading is very convenient to use. However, it may suffer from performance
issue in the following scenario:
~~~
// SQL executed: SELECT * FROM tbl_customer LIMIT 100
$customers = Customer::find()->limit(100)->all();
foreach ($customers as $customer) {
// SQL executed: SELECT * FROM tbl_order WHERE customer_id=...
$orders = $customer->orders;
// ...handle $orders...
}
~~~
How many SQL queries will be performed in the above code, assuming there are more than 100 customers in
the database? 101! The first SQL query brings back 100 customers. Then for each customer, a SQL query
is performed to bring back the customer's orders.
To solve the above performance problem, you can use the so-called *eager loading* by calling [[ActiveQuery::with()]]:
~~~
// SQL executed: SELECT * FROM tbl_customer LIMIT 100
// SELECT * FROM tbl_orders WHERE customer_id IN (1,2,...)
$customers = Customer::find()->limit(100)
->with('orders')->all();
foreach ($customers as $customer) {
// no SQL executed
$orders = $customer->orders;
// ...handle $orders...
}
~~~
As you can see, only two SQL queries are needed for the same task.
Sometimes, you may want to customize the relational queries on the fly. It can be
done for both lazy loading and eager loading. For example,
~~~
$customer = Customer::find(1);
// lazy loading: SELECT * FROM tbl_order WHERE customer_id=1 AND subtotal>100
$orders = $customer->getOrders()->where('subtotal>100')->all();
// eager loading: SELECT * FROM tbl_customer LIMIT 10
SELECT * FROM tbl_order WHERE customer_id IN (1,2,...) AND subtotal>100
$customers = Customer::find()->limit(100)->with([
'orders' => function($query) {
$query->andWhere('subtotal>100');
},
])->all();
~~~
### Working with Relationships
ActiveRecord provides the following two methods for establishing and breaking a
relationship between two ActiveRecord objects:
- [[link()]]
- [[unlink()]]
For example, given a customer and a new order, we can use the following code to make the
order owned by the customer:
~~~
$customer = Customer::find(1);
$order = new Order;
$order->subtotal = 100;
$customer->link('orders', $order);
~~~
The [[link()]] call above will set the `customer_id` of the order to be the primary key
value of `$customer` and then call [[save()]] to save the order into database.
### Data Input and Validation
TBD
### Life Cycles of an ActiveRecord Object
An ActiveRecord object undergoes different life cycles when it is used in different cases.
Subclasses or ActiveRecord behaviors may "inject" custom code in these life cycles through
method overriding and event handling mechanisms.
When instantiating a new ActiveRecord instance, we will have the following life cycles:
1. constructor
2. [[init()]]: will trigger an [[EVENT_INIT]] event
When getting an ActiveRecord instance through the [[find()]] method, we will have the following life cycles:
1. constructor
2. [[init()]]: will trigger an [[EVENT_INIT]] event
3. [[afterFind()]]: will trigger an [[EVENT_AFTER_FIND]] event
When calling [[save()]] to insert or update an ActiveRecord, we will have the following life cycles:
1. [[beforeValidate()]]: will trigger an [[EVENT_BEFORE_VALIDATE]] event
2. [[afterValidate()]]: will trigger an [[EVENT_AFTER_VALIDATE]] event
3. [[beforeSave()]]: will trigger an [[EVENT_BEFORE_INSERT]] or [[EVENT_BEFORE_UPDATE]] event
4. perform the actual data insertion or updating
5. [[afterSave()]]: will trigger an [[EVENT_AFTER_INSERT]] or [[EVENT_AFTER_UPDATE]] event
Finally when calling [[delete()]] to delete an ActiveRecord, we will have the following life cycles:
1. [[beforeDelete()]]: will trigger an [[EVENT_BEFORE_DELETE]] event
2. perform the actual data deletion
3. [[afterDelete()]]: will trigger an [[EVENT_AFTER_DELETE]] event
### Scopes
A scope is a method that customizes a given [[ActiveQuery]] object. Scope methods are defined
in the ActiveRecord classes. They can be invoked through the [[ActiveQuery]] object that is created
via [[find()]] or [[findBySql()]]. The following is an example:
~~~
class Customer extends \yii\db\ActiveRecord
{
// ...
/**
* @param ActiveQuery $query
*/
public static function active($query)
{
$query->andWhere('status = 1');
}
}
$customers = Customer::find()->active()->all();
~~~
In the above, the `active()` method is defined in `Customer` while we are calling it
through `ActiveQuery` returned by `Customer::find()`.
Scopes can be parameterized. For example, we can define and use the following `olderThan` scope:
~~~
class Customer extends \yii\db\ActiveRecord
{
// ...
/**
* @param ActiveQuery $query
* @param integer $age
*/
public static function olderThan($query, $age = 30)
{
$query->andWhere('age > :age', [':age' => $age]);
}
}
$customers = Customer::find()->olderThan(50)->all();
~~~
The parameters should follow after the `$query` parameter when defining the scope method, and they
can take default values like shown above.
### Atomic operations and scenarios
TBD
......@@ -2,7 +2,7 @@ Active Record
=============
Active Record implements the [Active Record design pattern](http://en.wikipedia.org/wiki/Active_record).
The premise behind Active Record is that an individual [[yii\db\ActiveRecord]] object is associated with a specific row in a database table. The object's attributes are mapped to the columns of the corresponding table. Referencing an Active Record attribute is equivalent to accessing
The premise behind Active Record is that an individual [[yii\db\ActiveRecord|ActiveRecord]] object is associated with a specific row in a database table. The object's attributes are mapped to the columns of the corresponding table. Referencing an Active Record attribute is equivalent to accessing
the corresponding table column for that record.
As an example, say that the `Customer` ActiveRecord class is associated with the
......
......@@ -53,6 +53,7 @@ class RenderController extends Controller
$renderer->targetDir = $targetDir;
if ($this->guide !== null && $renderer->hasProperty('guideUrl')) {
$renderer->guideUrl = './';
$renderer->markDownFiles = $this->findMarkdownFiles($this->guide, ['README.md']);
}
$this->stdout('Searching files to process... ');
......@@ -116,7 +117,7 @@ class RenderController extends Controller
// render guide if specified
if ($this->guide !== null) {
$renderer->renderMarkdownFiles($this->findMarkdownFiles($this->guide, ['README.md']), $this);
$renderer->renderMarkdownFiles($this);
$this->stdout('Publishing images...');
FileHelper::copyDirectory(rtrim($this->guide, '/\\') . '/images', $targetDir . '/images');
......
......@@ -29,6 +29,14 @@ class ApiMarkdown extends GithubMarkdown
protected $context;
public function prepare()
{
parent::prepare();
// add references to guide pages
$this->references = array_merge($this->references, static::$renderer->getGuideReferences());
}
/**
* @inheritDoc
*/
......
......@@ -31,7 +31,10 @@ abstract class BaseRenderer extends Component
* @var Context the [[Context]] currently being rendered.
*/
public $context;
/**
* @var array files for guide pages
*/
public $markDownFiles;
/**
* Renders a given [[Context]].
......@@ -49,7 +52,7 @@ abstract class BaseRenderer extends Component
* @param Controller $controller the apidoc controller instance. Can be used to control output.
* @return
*/
public abstract function renderMarkdownFiles($files, $controller);
public abstract function renderMarkdownFiles($controller);
/**
* creates a link to a type (class, interface or trait)
......
......@@ -134,11 +134,11 @@ class Renderer extends \yii\apidoc\templates\html\Renderer
/**
* Renders a given [[Context]].
*
* @param Context $context the api documentation context to render.
* @param Controller $controller the apidoc controller instance. Can be used to control output.
*/
public function renderMarkdownFiles($files, $controller)
public function renderMarkdownFiles($controller)
{
$files = $this->markDownFiles;
$dir = Yii::getAlias($this->targetDir);
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
......@@ -174,7 +174,7 @@ class Renderer extends \yii\apidoc\templates\html\Renderer
];
$output = $this->getView()->renderFile($this->guideLayout, $params, $this);
}
$fileName = 'guide_' . str_replace('.md', '.html', basename($file));
$fileName = $this->generateGuideFileName($file);
file_put_contents($dir . '/' . $fileName, $output);
Console::updateProgress(++$done, $fileCount);
}
......@@ -183,6 +183,21 @@ class Renderer extends \yii\apidoc\templates\html\Renderer
$controller->stdout('done.' . PHP_EOL, Console::FG_GREEN);
}
protected function generateGuideFileName($file)
{
return 'guide_' . basename($file, '.md') . '.html';
}
public function getGuideReferences()
{
$refs = [];
foreach($this->markDownFiles as $file) {
$refName = 'guide-' . basename($file, '.md');
$refs[$refName] = ['url' => $this->generateGuideFileName($file)];
}
return $refs;
}
protected function fixMarkdownLinks($content)
{
$content = preg_replace('/href\s*=\s*"([^"\/]+)\.md(#.*)?"/i', 'href="guide_\1.html\2"', $content);
......
......@@ -26,6 +26,9 @@ use yii\helpers\Html;
*/
class Slider extends Widget
{
/**
* @inheritDoc
*/
protected $clientEventMap = [
'change' => 'slidechange',
'create' => 'slidecreate',
......
......@@ -43,6 +43,9 @@ use yii\helpers\Html;
*/
class SliderInput extends InputWidget
{
/**
* @inheritDoc
*/
protected $clientEventMap = [
'change' => 'slidechange',
'create' => 'slidecreate',
......
......@@ -37,6 +37,9 @@ use yii\helpers\Html;
*/
class Spinner extends InputWidget
{
/**
* @inheritDoc
*/
protected $clientEventMap = [
'spin' => 'spin',
];
......
......@@ -15,7 +15,61 @@ use yii\helpers\StringHelper;
/**
* ActiveRecord is the base class for classes representing relational data in terms of objects.
*
* @include @yii/db/ActiveRecord.md
* Active Record implements the [Active Record design pattern](http://en.wikipedia.org/wiki/Active_record).
* The premise behind Active Record is that an individual [[ActiveRecord]] object is associated with a specific
* row in a database table. The object's attributes are mapped to the columns of the corresponding table.
* Referencing an Active Record attribute is equivalent to accessing the corresponding table column for that record.
*
* As an example, say that the `Customer` ActiveRecord class is associated with the `tbl_customer` table.
* This would mean that the class's `name` attribute is automatically mapped to the `name` column in `tbl_customer`.
* Thanks to Active Record, assuming the variable `$customer` is an object of type `Customer`, to get the value of
* the `name` column for the table row, you can use the expression `$customer->name`.
* In this example, Active Record is providing an object-oriented interface for accessing data stored in the database.
* But Active Record provides much more functionality than this.
*
* To declare an ActiveRecord class you need to extend [[\yii\db\ActiveRecord]] and
* implement the `tableName` method:
*
* ```php
* <?php
*
* class Customer extends \yii\db\ActiveRecord
* {
* /**
* * @return string the name of the table associated with this ActiveRecord class.
* * /
* public static function tableName()
* {
* return 'tbl_customer';
* }
* }
* ```
*
* The `tableName` method only has to return the name of the database table associated with the class.
*
* > Tip: You may also use the [Gii code generator][guide-gii] to generate ActiveRecord classes from your
* > database tables.
*
* Class instances are obtained in one of two ways:
*
* * Using the `new` operator to create a new, empty object
* * Using a method to fetch an existing record (or records) from the database
*
* Here is a short teaser how working with an ActiveRecord looks like:
*
* ```php
* $user = new User();
* $user->name = 'Qiang';
* $user->save(); // a new row is inserted into tbl_user
*
* // the following will retrieve the user 'CeBe' from the database
* $user = User::find()->where(['name' => 'CeBe'])->one();
*
* // this will get related records from table tbl_orders when relation is defined
* $orders = $user->orders;
* ```
*
* For more details and usage information on ActiveRecord, see the [guide article on ActiveRecord][guide-active-record].
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Carsten Brandt <mail@cebe.cc>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment