Yii2 and bower/asset: jquery.inputmask AfterAction report…

So last week Im working along and I run composer install … to get a new framework installation to while trying to track down a bug. The install fails. I am all like ‘what did you say to me!?’ bower-asset/jquery.inputmask package was not being found. I look through the composer.json of the framework and sure enough it is required. A frontend client asset is required to install a framework. While some would say ‘thats a terrible idea’ in this case it makes sense. The framework is a full MVS stack, not just a middleware, or just a ORM, or just a client rendering engine.

Anyways, after looking in Packagist and following the link to Github the repo itself was basically empty. Apparently GirtHub repo alias’s have a life span limit. This limit had expired and thus caused the package URI to no longer resolve correctly. So I submitted a couple tickets on the respective projects and went home.

Not sure how long it took exactly but when I returned to work the next morning not only GitHub restored the alias but the framework group was going to updated the dependency list to point to the (newer) repository in the next point release. While this does not seem like a big thing to some; I felt happy I was able to find, isolate, and identify an issue that effected a number of people; and did so very quickly. Then provided a solution. #problem-solver

 

Related Links:

Packagist: jquery-inputmask
Yii2 GitHub ticket

Going in circles…

…ever try to write something when your brain is not firing on all 8? I recently had all of my wisdom teeth pulled during the same visit to the Dentist. During the recovery I tried to bang out some code Needless to say if you can/are not able to focus and concentrate; very little progress is made…

Days tip: Do not get overly dedicated to a task if you head is not in it game.

Yii2 AR SQL Verbs; Y-U-SO-WEIRD?

Yii2 being an active record style DB abstraction AND needing to support a wide range of database technologies facilitated creating the standard insert / select / update / delete functionality inside the Active Record model layer. While most of implementation makes sense some of it is not intuitive. Herein is a TL;DR of the 4 major commands and the *All() version if applicable.

 

select / selectAll (in Yii2 this is termed ‘find’)

\Class::find([{array of criteria}])->one(); OR \Class::findOne([{array of criteria}]);

\Class::find([{array of criteria}])->all(); OR \Class::findAll([{array of criteria}]);

 

insert / insertAll (termed ‘save’)

$model = new \Class({column values as array});

$model->save();

No saveAll() implementation by default

 

update / updateAll

$model = \Class::find({column values as array});

$model->setAttribute(s)({string or array of SQL SET keyed values})

$model->save();

 

Class::updateAll([{array of criteria}], {string to Key = Value pairs for SQL SET})

 

delete / deleteAll

\Class::delete({string of criteria})

\Class::deleteAll({string of criteria})

 

Bonus: Yii2 also has the ability to create database commands and bypass the Active Record abstraction altogether: Yii::app()->db->createCommand({string  of SQL command}).

 

Now, lets look at MySQL / Maria / MsSQL / Postgra default immplimentation of the same actions:

select/all

Select {string of keyed values} FROM {string of source} WHERE {string of keyed values}

 

insert/All

Insert Into {string of source} VALUES {string of keyed values}

 

update/All

Update {string of source} Set {string of keyed values}

 

delete/All

Delete From {string of source} Where  {string of keyed values}

 

See a pattern there? string source, key/value data sets. And minus Select all start with the data source followed by the command, the the key/value criteria.  And even select makes sense when you treat the field names as a string substitution for `select {needle(s)} from {source|`.

 

It is not that AR is bad, nor that Yii2 had a lack of effort. Trying to support the feature set of multiple database technologies, active record, createQuery, best practices, security, and community requests is daunting, hands down. Maybe I’ll write a package to normalize the base 4 verbs for MySQl/MariaDB…

 

 

Shameless Self Promotion: Presenting during Tampa Bay PHP’s May meetup!

Using Codeception for Acceptance testing

Tuesday, May 30, 2017, 6:30 PM

Sourcetoad’s new location
2901 W Busch Blvd #1018 Tampa, FL

9 Members Went

David Eddy will present “Using Codeception for Acceptance testing: the crash course.”

Check out this Meetup →

 

Update: Video is up on youtube at https://youtu.be/QXSP0bEpF4Y .

Why basic data structure knowledge is important.

Today while working through an issue ticket we came across a situation where console.log() was printing out a rounded variation of an integer. The server response payload was correct, the client was receiving the correct payload. But when the data was processed a rounded variation of the data point was being returned.

http://www.studytonight.com/data-structures/images/introduction-to-data-structures.gif

After a short amount of digging we found that my initial hunch was close: it is a character length limit of the language.  Basic data structure and computer science understanding is in fact useful to be familiar with.

References:

http://stackoverflow.com/questions/1379934/large-numbers-erroneously-rounded-in-javascript

http://www.ecma-international.org/ecma-262/5.1/#sec-8.5

Refactoring array index magic number with class constant.

During a code review my peer Michal Mazur turned me onto the follow example; I have to say I am really digging it: Array Index as constant .

In my specific case here is the usage:

if (!empty($paramData[19]) && is_string($paramData[19])) {
    $paramData = $paramData[19];
} elseif (empty($paramData[19]) && !empty(isset($paramData[17])) && isset($paramData[17])) {
    $paramData = $paramData[17];
}

…and the refactored logic:

if (!empty($rowData[$this::FULL_SOURCE]) && is_string($rowData[$this::FULL_SOURCE])) {
    $imageSource = $rowData[$this::FULL_SOURCE];
} elseif (empty($rowData[$this::FULL_SOURCE]) && !empty(isset($rowData[$this::SQUARE_SOURCE])) &&
    isset($rowData[$this::SQUARE_SOURCE])
) {
    $imageSource = $rowData[$this::SQUARE_SOURCE];
}

While a bit more verbose the logic is much easier to read using the contextually named constants.

PHP and large CSV’s…

After looking around a bit I have yet to find a way to read a specified line from a file without doing one of the following:

  1. fopen()
  2. looping every line until reaching the desired line

Desired functionality:

  1. fopen($file)
  2. imaginary function call `fgetcsvline($lineNumber)` would returns the contents of line $lineNumber

 

Anyone know a solution for this?

 

Update: Derp, how could I forget about ‘The League’? http://csv.thephpleague.com/

PHPStorm 2017.1 released…

I’m sure many of you know this already by JetBrains has released PHPStorm version 2017.1. The improvement list looks nise and Im eager to try ’em out.
Get he full details over at the office release page: https://blog.jetbrains.com/phpstorm/2017/03/phpstorm-2017-1-is-now-released/

Of special interest is the Codecetpion and PHPUnit 6 as I am a big personal supporter of automated testing process.

Quick little thing.

Was sad to see Yii2’s getOldAttributes() did not have to ability to limit based on a provided array; whereas getAttributes() does take an array to limit the returned attributes. So I whipped this up right quick:

/**
 * @param array $array
 *
 * @return array
 */
public function getOldAttributes($array = [])
{
    $returnData = parent::getOldAttributes();

    if (!empty($array)) {
        $returnData = array_intersect($array, $returnData);
    }

    return $returnData;
}

Simple little thing but super handy.