$post->on('update', function($event) { // send email notification });
$component = \Yii::createObject(array( 'class' => '\app\components\GoogleMap', 'apiKey' => 'xyz', // 'on eventName' => array('Event', 'run'), // 'as behaviorName' => array(/* Behavior config */), ));
<?php use yii\helpers\base\Html; /** * $this . * $this->context * @var yii\base\View $this */ $this->title = 'Hello world'; ?> <h1><?php echo Html::encode($this->title); ?></h1> <p class="lead"> !</p>
* View can be installed for each controller, or use the base for the application. public function actionIndex() { echo $this->render('index'); }
public function init() { $this->on('beforeAction', function($event) { // $event->isValid = false; }); }
public function behaviors() { return array( 'AccessControl' => array( 'class' => '\yii\web\AccessControl', 'rules' =>array(/* */), ), ); }
public function actionLogin() { $model = new LoginForm(); if ($this->populate($_POST, $model) && $model->login()) { Yii::$app->response->redirect(array('site/index')); } echo $this->render('login', array( 'model' => $model, )); }
$form = $this->beginWidget('yii\widgets\ActiveForm', array( 'options' => array('class' => 'form-horizontal') )); echo $form->field($model, 'username')->textInput(); echo $form->field($model, 'password')->passwordInput(); echo $form->field($model, 'rememberMe')->checkbox(); echo Html::tag('div', Html::submitButton('Login', null, null, array('class' => 'btn btn-primary')), array( 'class' => 'form-actions' )); $this->endWidget();
* Attention: in Html :: tag ($ tag, $ content, $ options) - changed the order of the parameters!"For the most part, ActiveRecord has remained untouched."- written in the previous article. Rightly noticed - do not touch.
// ActiveQuery $query = Post::find(); // $posts = $query->all(); // $posts = $query ->where(array('status' => Post::DRAFT)) ->orderBy('time') ->all(); // $post = $query ->where(array('id' => 10, 'status' => Post::READ)) ->one(); // , where $post = Post::find(array('id' => 10, 'status' => Post::READ)); // $post = Post::find(10) ->where(array('status' => Post::READ)) ->one(); // $posts = $query->indexBy('title')->all(); // $posts = $query->asArray()->all();
class Post extends ActiveRecord { public function getCreator() { return $this->hasOne('User', array('id' => 'user_id')); } public function getComments() { return $this->hasMany('Comment', array('post_id' => 'id')); } public function getTrustComments($isTrust = true) { return $this->hasMany('Comment', array('post_id' => 'id')) ->where('status = :status', array( ':status' => $isTrust ? self::TRUST : self::UNTRUST, )) ->orderBy('id'); } }
$post = Post::find(1); $comment = new Comment(); $comment->text = 'Yii Framework is cool!'; $post->link('comments', $comment);
class Post extends \yii\db\ActiveRecord { /** * @param ActiveQuery $query */ public static function byCreator($query, $userId) { $query->andWhere('user_id = :userId', array('userId' => $userId)); } /** * @param ActiveQuery $query */ public static function removed($query) { $query->andWhere('removed = 1'); } } $posts = Post::find()->removed()->all(); $myPosts = Post::find()->byCreator(Yii::$app->user->id)->all();
Source: https://habr.com/ru/post/178917/
All Articles