Introduction to curl

This is just a barely memo about very, very basics of curl. Since curl fetches URLs and data from some endpoints, it can be used for a variety of reasons. I am using to for a very basic (and quick!) API calls tests.

If you’re on Linux, Unix or any of its clones or if you’re on Mac then you most likely have curl. If you’re on Windows, but you’re using Git for Windows (or some similar tool) then you have curl.

So, let’s jump into that basics. Be warned — you will not find here anything that you wouldn’t already knew.

Read More “Introduction to curl”

Relative file paths and migrations in Yii1

Watch out for relative file paths. When using them in any action, files lands in a folder (from that relative path) in root directory folder (where they’re is expected, at least according to me). But, when you use the very same path in a migration, then files will land in a folder in protected folder. I have just found this to my surprise.

To “fix” this, you need to add:

[php]dirname(__FILE__).’/../../’.[/php]

in front of your relative path (which makes it no longer relative, though).

Details on Yii forum.

Multi-column CListView

CListView widget is a great piece of code for quickly rendering lists of items that:

  • are based on your own provided view for each item (and thus let you customize final view in nearly every aspect) and
  • are providing fully functional paginator in the same time.

However, they lack support for rendering items in more than one column. This article shows one of many workarounds to fix this problem.

Read More “Multi-column CListView”

actionParams are not available in ACF’s matchCallback

When using matchCallback in AccessControlFilter in Yii2, an attempt to read $action->controller->actionParams or Yii::$app->controller->actionParams inside this callback fails, because these attributes are empty.

This is by design, because AccessControlFilter is executed at an earlier stage than parameters binding.

If you need to read action parameters inside matchCallback (for example to judge, if user has access to particular action, basing on action’s parameters) then the only way is to read them from Yii::$app->request->get(). More details here or here.

Using Yii2’s DetailView in a little bit more advanced way

The simplest and easiest way of rendering DetailView is to declare model and attributes list. This will renders each listed model’s attribute in a cool-looking, responsive table.

This article contains examples and solutions for going a little bit beyond that and using DetailView in a little bit more advanced way.

Read More “Using Yii2’s DetailView in a little bit more advanced way”

Interactive mode command-line command for Yii1

For one of my projects I needed an interactive console command in Yii 1, i.e. the one that is gathering all information from user in an interactive mode (a serie of questions and answers displayed directly in the console), ignoring command-line arguments at all.

This is an example (or rather a bare foundation, as this actually is doing nothing) of such solution. It has some console text formatting methods (borders, text alignment) plus simple method for gathering user response.

Read More “Interactive mode command-line command for Yii1”

Render checkbox list with initially checked items in Yii2

This guide shows, how to (in Yii2) render checkbox list with correctly selected items (preselected during form render), using both HTML helper classes and active form approach.

This is an extended version of this Stack Overflow question and an answer following it, plus some additional information from me and from other sources.

Read More “Render checkbox list with initially checked items in Yii2”

Chained, AJAX-updated listboxes in Yii2

There’s a great answer at Stack Overflow that shows, how in Yii2 you can implement a form containing listbox and two input fields. Listbox contains data from one model and when user selects any option, remaining two input fields should be populated with relevant values from related model.

I wrote an extended version of this code. It populates two other listboxes instead of just input fields. And it uses a little bit less deprecated code! :>

Read More “Chained, AJAX-updated listboxes in Yii2”

Changing Yii2 application’s configuration at runtime

You can’t use Yii::t() method inside Yii2 application’s configuration, right?

I mean… you can use it (application won’t fail), but you will always get string passed as method’s param in return, instead of actual translation, no matter, what language is currently set in your application.

This makes “How to translate application title (or any other configuration parameter)” a quite good question.

Read More “Changing Yii2 application’s configuration at runtime”

Migration that requires user confirmation to continue

Since there’s a pure Linux underlying PHP, you can use STDIN (actually: php://stdin) and read it with fopen when your PHP script is run in console to stop execution of your script and read a button user pressed.

Since Yii migrations are run in console, you can use this mechanism to ask user whether she or he want to continue with your database migration.

Quite handy in some scenarios (like: destroying entire database), don’t you think?

Read More “Migration that requires user confirmation to continue”

Converting database structure or .sql file into Yii migration

Yii Framework 1.x migration system is very powerful and gives you much more power and control over database update process. But it becomes a horrible nightmare, if you have to deal with .sql files and convert them into Yii migration files manually.

This article should help you in that strike or at least ease your work.

If you have any doubts, why the hell migrations are better than using plan .sql files then this blog post should provide you with at least some reasons.

Read More “Converting database structure or .sql file into Yii migration”

Dump current database state to Yii migration

There’s a wonderful extension to Yii 1.x, called database-command, written by the one and only schmunk, which allows you to quickly and easily generate any set of CDbMigrationCommands (actually, entire single migration code) based on current database schema.

This isn’t, of course, the only one out there. There are some others. But I like this one the most, mainly for flexibility (may parameters to suit generated migration file to your needs). However, if this is your first approach to using custom yiic commands, you may get a little bit confused. This article should help you.

Read More “Dump current database state to Yii migration”

An introduction to database migrations in Yii

If you’re new to Yii migrations, these sources might be a help for you:

  1. General information: Database Migration topic in The Definitive Guide to Yii.
  2. How to create initial migration basing on current database state, explains this Stack Overflow question and GitHub Gist (with InitialDbMigrationCommand class’ code) to support it.

To use migrations, you have to properly configure console version of your Yii application. Details follows.

Read More “An introduction to database migrations in Yii”

non-YiiBooster fields with YiiBooster prepends

I don’t like the idea of rewriting my entire application’s view to use new Bootstrap 3, so my Yii apps remain on Bootstrap 2, served by YiiBooster extension. The biggest problem, I find in it is that its controls have prepends somehow “built-in”.

Both field and its prepend is rendered with one line of code. So, when you want to replace any YiiBooster’s control with for example own widget / library, you always remove one line, but looses both field and prepend.

I found ugly and nasty workaround for this.

Read More “non-YiiBooster fields with YiiBooster prepends”

Cyclic jobs in Yii and MySQL

When we are talking about some cyclic tasks executed periodically in PHP, most of us automatically thinks about CRON. It is not surprising, since it is very popular, easy to manage (available often even on shared hostings) and easy to code (it simply fires given PHP script).

However, there are four other ways you can achieve exactly the same in Yii/PHP. And many Yii developers may not be aware about some of them.

Read More “Cyclic jobs in Yii and MySQL”

Capture double click on Yii’s CGridView row

I was using a simple Javascript code (attached to CGridView.selectionChanged property) to capture click on any grid view’s row and redirect user to row-specific URL. I wanted to change it to double click only.

CGridView exposes selectionChanged even only, no click or dblclick, so that was a quite nice challenge. It seems that the only way is to capture this event yourself.

Read More “Capture double click on Yii’s CGridView row”

Delete model with relations

To delete in Yii a model with some relations we usually have at least three options.

Depending on many factors (RDBMS type, table type, situation, context etc.) most of them have advantages and disadvantages. Good developer already knows these options, so intention of this post is not to discuss them, but to show you an interesting, new (at least for me) fourth approach to solve this problem, that I found recently.

Read More “Delete model with relations”

urlReferrer and opening new browser tab

To open particular URL in a new tab, you have at least two possibilities:

  • click a link with middle button (usually under mouse wheel) or
  • open a blank tab and copy-paste URL to URL bar.

There is slight difference it this (at least in Chrome) when it comes to urlReferrer in Yii:

  • in first case browser sets referrer correctly (i.e. Yii::app()->request->uUrlReferrer contains a valid URL),
  • in second approach URL referrer IS NOT set (i.e. Yii::app()->request->urlReferrer is an empty string).

The second situation is obvious. Opening a new tab means that browser does not have a history for it and thus it is unable to set urlReferrer correctly. However, it is better to keep this information in mind. If you rely on this information, you may get yourself (or your code) into a problem.

CGridView: the summaryText maddnes!

I’m finishing entry phase of my first big project (first big project, where I’m key project manager and lead developer, to be precise). Right now I’m after four days of sleeping for 5 hours and coding for remaining 19 hours each day. My brain simply bowls. In this specific situation, I’ve crafted a helper method, which makes mad-laughing or mad-screaming, each time I see it.

Read More “CGridView: the summaryText maddnes!”

Upload files via AJAX and fill up other model properties in the same time

There’s a comment to Yii 1.1: XUpload Workflow Wiki article, made by MeDroogie, who asks, how to handle situation, where file is correctly uploaded (via AJAX, in separate layer, handled by upload action), but form submission itself fails, as form processing action stops on some model’s validation errors.

I had the same problem with other AJAX uploader, that I think, I solved gracefully.

Keep in mind, that since solution, that I’m discussing in this article, is part of proprietary project, I cannot share any code. This is purely design or theoretical discussion.

Read More “Upload files via AJAX and fill up other model properties in the same time”