






All problems in computer science can be solved by another level of indirection... Except for the problem of too many layers of indirection.
David Wheeler
Prado, с 2004 → Yii 1.0, 2008 → Yii 1.1, 2010 → ;)
*Except PECL and microframeworks
setMyCoolFactoryDependencyInjectionContainer()
C-prefixes.\yii\base\Object — getters and setters, init method,
configuring via array, Object::className().\yii\base\Component extends Object — event, behavior.
class GoogleMap extends \yii\base\Object
{
public function __construct($param1, $param2, $config = array())
{
// ...
parent::__construct($config);
}
public function init()
{
// ...
parent::init();
}
}
$object = \Yii::createObject(array(
'class' => '\app\components\GoogleMap',
'apiKey' => 'xyz',
'apiSecret' => 'cde',
), $param1, $param2);
$component->on($eventName, $handler);
$component->off($eventName, $handler);
$component->trigger($eventName, Event $eventObject = null);
Global events:
// raising event
\Yii::$app->trigger($eventName);
// subscribing
\Yii::$app->on($eventName, $handler);
View object — data, assets, render.$this in templates refers to view objects.$this->context refers to controller or widget.
use \yii\widgets\Menu;
// ...
echo Menu::widget(array(
'items' => $items,
));
use yii\widgets\ActiveForm;
<?php $form = ActiveForm::begin(); ?>
<?php echo $form->field($model, 'username')->textInput(); ?>
<?php echo $form->field($model, 'password')->passwordInput(); ?>
<div class="form-actions">
<?php echo Html::submitButton('Login'); ?>
</div>
<?php ActiveForm::end(); ?>
app\config\AppAsset::register($this);
return $this->render(...);Removed CFormModel since Model plays nice with forms. Forms are simpler.
Consistent syntax for everything.
$query = new \yii\db\Query;
$query->select('id, name')
->from('tbl_user')
->orderBy('id DESC')
->limit(10);
$command = $query->createCommand();
echo $command->sql;
$rows = $command->queryAll();
$users = User::model()->find()
->orderBy('id DESC')
->limit(10)
->all();
$finder = Post::find()->where(array('a' => 10));
$finder2 = clone $finder;
$finder2->addWhere(array('b' => 1));
$model = $finder->one();
$model2 = $finder2->one();
// шорткаты
$post = Post::find(10); // pk = 10
$post = Post::find(array('a' => 10, 'b' => 1)); // where a = 10 and b = 1
class User extends \yii\db\ActiveRecord
{
public function getPosts()
{
return $this->hasMany('Post', array('user_id' => 'id'));
}
public function getActivePosts()
{
return $this->hasMany('Post', array('user_id' => 'id'))
->where(array('status' => Post::STATUS_ACTIVE));
}
}
$posts = $user->getPosts()->limit(10)->all();
$postCount = $user->getPosts()->count();
$posts = Post::find()->limit(10)->asArray()->all();
foreach($posts as $post) {
echo $post['title']."\n";
}
save() saves only what was changed.
There's new link method to save related records.
$comment = new Comment();
$comment->text = 'Hello, Yii!';
// INSERT INTO post_comment ...
$post->link('comments', $comment);
// DELETE FROM post_comment ...
$post->unlink('comments', $comment);
API is very close to web.
$mergedConfig = ArrayHelper::merge($baseConfig, $specificConfig);
$posts = Post::model()->limit(10)->all();
$titles = ArrayHelper::getColumn('title');
$hash = Security::generatePasswordHash($password);
// ...saving hash to DB...
// reading hash from DB on login
if (Security::verifyPassword($password, $hash) {
// OK
} else {
// wrong password
}
echo \Yii::t('app', 'There {n, plural, =0{are no cats} =1{is one cat} other{are # cats}}!', array(
'n' => 13,
));
// Здесь {n, plural, =0{котов нет} =1{есть один кот} one{# кот} few{# кота} many{# котов} other{# кота}}!
echo \Yii::t('app', '{n,number} is spelled as {n, spellout}', array(
'n' => 42,
));
Getting started with Yii2:
curl -s http://getcomposer.org/installer | php
php composer.phar create-project --stability=dev yiisoft/yii2-app-basic .
1.1 is stable and supported at least till 2016