After my new production server installation, I discovered I couldn’t run migrations: $ ./yii migrate/up bash: ./yii: Permission denied Adding execution permissions to ./yii resolved the problem: $ chmod +x ./yii deploy@apps:/var/www/mp$ ./yii migrate/up Yii Migration Tool (based on Yii v2.0.8) No new migrations found. Your system is up-to-date. Found via this comment.
Category: 06 Tips
How to fix “An internal server error occurred” in Yii
If you’re running into a near empty screen showing, “An internal server error occurred” in Yii2, check your error logs. It’s a simple fix. My logs showed: An Error occurred while handling another error:\nexception ‘yii\\base\\InvalidConfigException’ with message ‘The directory is not writable by the Web process: /var/www/mp/backend/web/assets’ in /var/www/mp/vendor/yiisoft/yii2/web/AssetManager.php:213 You can resolve it by opening […]
Atom: Show Open File in Tree View
If you’re editing a file in Atom, the command to reveal the file in the sidebar is: Command + Shift + \ I’ve recently switched to Atom and very much appreciate it as an emerging power editor on the MacOS platform.
How to fix “yiisoft/yii2-composer” plugin requires composer-plugin-api 1.0.0
If you’ve been getting the error below: The “yiisoft/yii2-composer” plugin requires composer-plugin-api 1.0.0, this *WILL* break in the future and it should be fixed ASAP (require ^1.0 for example). I’ll try to help you fix it with this blog post. Increasingly, I’m fatiguing of the frequent composer errors that require regular reconfiguration. While you have to […]
Setting the State of the Switch Input Widget Radio Button
The Krajee Switch Input widget for Yii2 based on Bootstrap Switch is great. I’ll write more about it in the near future. However, I had one major problem with a gap in the documentation – others seem to have been confused as well. When you’re using the widget in SwitchInput::RADIO mode, it’s not well documented […]
How to Display Form Errors (the ErrorSummary)
Let’s say you’re using a validator that doesn’t correspond to a specific field and you want to display it on the form. For example, I’m using the compare validator and wish the error summary to appear on the create form. [php] <?php use yii\helpers\Html; use yii\widgets\ActiveForm; /* @var $this yii\web\View */ /* @var $model frontend\models\Participant */ […]
How to Use the Yii2 Compare Validation
Here’s an example of how to use the Yii2 compare validator. The rules go in your model e.g. app/models/Participant.php. In this example, I’m ensuring that the participant_id and the invited_by id are not the same. I don’t want someone to be able to invite themselves. [php] public function rules() { return [ [‘participant_id’, ‘compare’,’compareAttribute’=>’invited_by’,’operator’=>’!=’,’message’=>’You cannot invite […]
How to Count ActiveDataProvider Items
Here’s how to create an ActiveDataProvider in Yii2 and get a count of records: [php] use yii\data\ActiveDataProvider; $participantProvider = new ActiveDataProvider([ ‘query’ => Participant::find()->where([‘meeting_id’=>$id]), ]); echo $participantProvider->getCount(); [/php]
Check if a record exists using ActiveRecord
In Yii2, if you’d like to check if a record exists in a table, use the following code: [php] Customer::find()->where( [ ’email’ => ‘jeff@yii2x.com’ ] )->exists(); [/php]
Dropdown Menus for Navigation Bar
Yii2 uses short array notation extensively. Once you get used to it, it’s great, but it can take a bit of time to read code blocks with it. When arrays are nested, it can be even more confusing. I found the Bootstrap navigation bar layouts to be hard to read. I also couldn’t easily find […]