M
M
Maila2017-07-01 18:39:18
Layout
Maila, 2017-07-01 18:39:18

Sorting blog posts Yii2 advanced - why doesn't the order change?

The phpStorm IDE in the SiteControllers file puts use common\models\Blog; automatically, maybe something else. In SiblimeText3, if this line is not added, it gives an error: "Class 'frontend\controllers\Blog' not found"
in the trace, the line $blogs = Blog::find( )->all(); in frontend\controllers\SiteController.php
But if you add it, blog posts appear in order one after another. Further, when changing the order of posts - nothing changes. Why is that?e1ab4e7901074cc384e20c0fd90d675d.jpg

<?php
namespace frontend\controllers;

use Yii;
use yii\base\InvalidParamException;
use yii\web\BadRequestHttpException;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\LoginForm;
use common\models\Blog;
use frontend\models\PasswordResetRequestForm;
use frontend\models\ResetPasswordForm;
use frontend\models\SignupForm;
use frontend\models\ContactForm;

/**
 * Site controller
 */
class SiteController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout', 'signup'],
                'rules' => [
                    [
                        'actions' => ['signup'],
                        'allow' => true,
                        'roles' => ['?'],
                    ],
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    /**
     * Displays homepage.
     *
     * @return mixed
     */
    public function actionIndex()
    {
        $blogs = Blog::find()->andWhere(['status_id'=>1])->orderBy('sort')->all();
        return $this->render('index',['blogs'=>$blogs]);
    }

    /**
     * Logs in a user.
     *
     * @return mixed
     */
    public function actionLogin()
    {
        if (!Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        } else {
            return $this->render('login', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Logs out the current user.
     *
     * @return mixed
     */
    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }

    /**
     * Displays contact page.
     *
     * @return mixed
     */
    public function actionContact()
    {
        $model = new ContactForm();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
                Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
            } else {
                Yii::$app->session->setFlash('error', 'There was an error sending your message.');
            }

            return $this->refresh();
        } else {
            return $this->render('contact', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Displays about page.
     *
     * @return mixed
     */
    public function actionAbout()
    {
        return $this->render('about');
    }

    /**
     * Signs user up.
     *
     * @return mixed
     */
    public function actionSignup()
    {
        $model = new SignupForm();
        if ($model->load(Yii::$app->request->post())) {
            if ($user = $model->signup()) {
                if (Yii::$app->getUser()->login($user)) {
                    return $this->goHome();
                }
            }
        }

        return $this->render('signup', [
            'model' => $model,
        ]);
    }

    /**
     * Requests password reset.
     *
     * @return mixed
     */
    public function actionRequestPasswordReset()
    {
        $model = new PasswordResetRequestForm();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            if ($model->sendEmail()) {
                Yii::$app->session->setFlash('success', 'Check your email for further instructions.');

                return $this->goHome();
            } else {
                Yii::$app->session->setFlash('error', 'Sorry, we are unable to reset password for the provided email address.');
            }
        }

        return $this->render('requestPasswordResetToken', [
            'model' => $model,
        ]);
    }

    /**
     * Resets password.
     *
     * @param string $token
     * @return mixed
     * @throws BadRequestHttpException
     */
    public function actionResetPassword($token)
    {
        try {
            $model = new ResetPasswordForm($token);
        } catch (InvalidParamException $e) {
            throw new BadRequestHttpException($e->getMessage());
        }

        if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
            Yii::$app->session->setFlash('success', 'New password saved.');

            return $this->goHome();
        }

        return $this->render('resetPassword', [
            'model' => $model,
        ]);
    }
}
File Blog.php (common\models)
<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "blog".
 *
 * @property integer $id
 * @property string $title
 * @property string $text
 * @property string $url
 * @property integer $status_id
 * @property integer $sort
 */
class Blog extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'blog';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['title', 'url'], 'required'],
            [['text'], 'string'],
            [['status_id', 'sort'], 'integer'],
            [['sort'], 'integer','max'=>99, 'min'=>1],
            [['title', 'url'], 'string', 'max' => 150],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => 'Заголовок',
            'text' => 'Текст',
            'url' => 'ЧПУ',
            'status_id' => 'Статус',
            'sort' => 'Сортировка',
        ];
    }
}

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
G
grigorysh1, 2018-12-07
@grigorysh1

Try property: "originLeft: false". It positions elements on the right or left side. If false, then it should be on the right.

A
aliasst, 2018-12-07
@aliasst

or maybe make two large blocks 50% flex, and inside each small blocks just going under each other in a column

O
OKF, 2018-12-08
@OKF

Alternatively, you can try columns. They are also not without flaws, but for your case,
https://codepen.io/fainz777/pen/JwPzvW?editors=0110 may be suitable

K
k2lhu, 2017-07-01
@k2lhu

sorting is done like this

$blogs = Blog::find()->where(['status_id'=>1])->orderBy(['id' => SORT_DESC])->all();

либо

$blogs = Blog::find()->where(['status_id'=>1])->orderBy(['id' => SORT_ASC])->all();

well, not andWhere - but simply where, you don’t have any more conditions here.

M
Maxim Timofeev, 2017-07-02
@webinar

use common\models\Blog;
$blogs = Blog::find()->all(); // тут блог это common\models\Blog как указано в use

Analog:
Your option:
Accordingly, by not specifying a namespace, you tell the program to look for the Blog class with the namespace of this class (in the current folder). And as a result you get "Class 'frontend\controllers\Blog' not found". Yii and code editors have nothing to do with it. It's just that phpStorm does some work for you by writing use, but SiblimeText doesn't do that, at least out of the box.
You can have 1000 Blog classes in your project and the namespace indicates which one you want to use. So you have to keep an eye on it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question