Merry Christmas and a happy New Year – 2017

Another year gone by and it is nearly time for Christmas.

Over the last 12 months …

Client work
I have taken on and worked with 3 new clients this year. As you may or may not know I am a one man contractor and my contracts generally last months at a time so 3 new clients has proved sufficient.

Toolkit
I have worked on an internal system for managing my business which is 90% complete. For all intents and purposes the Toolkit is completed. The remaining 10% will involve adding any remaining features I can think up or that may be needed.

Neptune – Lead Tracker
I have developed the initial Neptune Lead Tracker and put it online. Neptune has not seen much growth as of yet, I will leave Neptune for 1 more year then take it down if no one is using Neptune. – http://www.neptuneleadtracker.com

Czech Character Code Converter
I developed a web based software script to help me with work on a particular client this year. Afterwards I gave the web based software script a basic front-end and uploaded it online so it can be used by other people. – http://www.czechcharactercodeconverter.com/

King of Adem
Started writing code for a multiplayer strategy game. This project is on the back-burner at the moment and will remain there until some interest is shown from a game developer who would like to take the project further with me.

Overall a very good year.

As I wind down for Christmas I will not be writing anymore blog posts until January 27th 2018 so please check back then to continue reading my blog.

I wish you all a merry Christmas and a happy New Year, until next year, bye for now.

Good company, bad company

I was reading through my emails a few days ago when my Quora updates lead me to this statement …

“My ability to develop software has been improving monotonically over the years, but I have gone from good company to bad company, to good company. I was considered good in a good company. Then I was considered bad the month after I started at a bad company. Then I went to a good company and was suddenly good again. And I was improving monotonically. After you do this enough times, you realize that it’s not you. It’s the quality of the company.” – Link to Quora article

This struck a reasonance with me. Based on what this person has written and my own experiences we can draw up the following scenario …

So first of all it is unlikely that he was either in a wholly good company or wholly bad company but the observation of the good or bad company was based entirely on the people giving him the good or bad references.

Secondly I would like to point out that in this context good or bad is only an expression to describe the positive or negative references so when I state good company, good column or good reference it means positive reference and when I say bad company, bad column or bad reference it means negative reference there is more to it than this though, keep reading.

Now based on the diagram above let us say the world was divided into 2 categories of people, bad column and good column.

One of these categories of people in this case the bad column category are the group of people that are solely responsible for the negative references that people are receiving today.

One of these categories of people in this case the good column category are the group of people that are solely responsible for all the positive references that people are receiving today.

This means that all the bad references this person was receiving was from people who are in the bad column category and all the good references that the person was receiving was from people in the good column category.

So what does this mean in terms of the good or bad reference? Well let us say you walked into a company where the recruitment manager was in the good column category and you tell him that you recieved a bad reference at the previous company.

Would that make a difference as to whether or not you get the job? Remember this good column person would have given you a good reference rather than a bad one for the exact same work.

So would this good column recruitment manager be able to spot the bad column category statement? If the good column category viewed the quality of your work would the good column person even care about the bad reference?

Now let us say you walked into a company where the recruitment manager was in the bad column category and you tell him that you recieved a good reference at the previous company. Remember this bad column person would have given you a bad reference rather than a good one for the exact same work. Well exactly what I said above for the good column person applies equally in this scenario, would the bad column person care about the good reference?

Is there any continuity between the bad column and good column category people? Will recieving a bad reference from the bad column category people “really” effect your relationship with the good column category people? Or does it just not matter?

So what is the solution to this guys problems if he sees getting a bad reference as a problem. The solution is to find work in a company where your manager is in the good column category of people. The “ideal” solution for this person is to find work in a company where all employees are in the good column category of people.

I say “ideal” solution because for the most part it is just an “ideal” because most companies have a mixture of people from the bad column and good column categories.

Maybe the good column director hired somone in the bad column to oversee a position at the company. A few years down the line the bad column employee has hired more of his bad column friends into the company. Now all that remains at the company is a mixture of people.

Or maybe it is a lot worse for the good column director and the company is now predominatly bad column and it is only a matter of time before the good column director gets thrown out of the company.

Please bear in mind that all of the scenarios I have described above can equally apply to the bad column category people as well, just swap the words “bad column” and “good column” and read it again, you get the idea.

It is also important when talking about these bad column and good column categories of people that I am not talking about superficial personality traits that people have picked up but I am talking about the way in which people are born.

The bad column category people and the good column category people have additional traits other than the references they give that define them as bad column or good column. It is something more fundamental in their make-up which leads the bad column category people to give bad references and the good column category people to give good references.

For example you may occasionally get a bad column category person pretending to be a good column person and giving out a good reference but this would be unnatural for the person and at heart he may believe differently. He has not given the good reference because he believes it but to gain something for himself.

So going back to this “ideal” solution. Would not the ideal solution be then to have companies that consist of wholly bad column category people and companies that consist of wholly good column category people?

If you are director of a company and you are a wholly good column category person why are you employing bad column category people?

If you are director of a company and you are a wholly bad column category person why are you employing good column category people?

Is the ideal solution to simply have a company of your own category people?

What about your clients? If you are good column company director would you take on clients that are in the bad column category? If you are a bad column company director would you take on clients that are in the good column category? If so why?

This is not about “firing people” or “getting rid of people”. Although I believe a lot of this goes on, a good column category manager gets promoted and decides to fire a few bad column category people and vice versa.

It is about dealing with issues where they matter most and that is at the front door of the company. There is no point in either bad column or good column letting in their opposite and then sit their whining that this opposite is spoiling it for us.

The company must have a director and the director must fit into one of the categories described above. As the company director it is the company directors responsibility to ensure that only his / her category of people are given jobs in the company.

Ahh well, our company has 2 directors, one in the bad column category and one in the good column category. Yes, exactly, this sort of partnership should never have been formed in the first place.

Why would anyone in the good column category want to be surrounded by people in the bad column category? Why would anyone in the bad column category want to be surrounded by people in the good column category?

As the company director you had the chance to create your personal Utopia and not have anyone spoil it for you. So why let in a bunch of people from the opposite category?

As stated earlier it is not about someone climbing the corporate ladder and firing those in the opposite category because he or she does not like them.

It is more about those of a particular category not employing the opposite category in the first place. Do not employ the opposite category, do not have the opposite category as your clients.

Alternatively you could divide the company into good column people and bad column people and put them in seperate teams. Have the good column people deal with good column clients and the bad column people deal with bad column clients.

If you want positive references and reviews why work with someone as a client or employee who is going to give you bad references and reviews? Or as a bad column company employer why would you employee a good column employee to then give them a bad reference from which they will start touting their opinions on websites like Glassdoor?

How many more Glassdoor reviews are we going to hear about “revolving door” companies with high staff turnover or firing people over not being a “cultural fit”.

Yes the bad column or good column category person may have accepted the job at the predominatly opposite category company and it turned out the person was “not a good fit” but whoes fault is that? In my opinion it is the solely the employers responsibility.

The employer knows the state of his / her company, he knows which category he / she belongs to at heart. Why employee the opposite?

This is not a matter of pity or feeling sorry, a good column category person rejecting a bad column category job applicant warrants no pity, ohh poor bad column category person has been rejected for job, look the bad column category person can go to companies that are operated by bad column category people, he / she can get bad column category people as clients, he / she can be invited to parties that are hosted by bad column category people, he / she is friends with other bad column category people, yes, he / she has friends can you believe it, yes and bad column categories friends are bad column category people so what place does the bad column category person have in your good column category company anyway? Vice-versa.

You can only lose a job if you have been given one, well in this ideal you would not lose your job because you would never have been given one in the first place. At least not in a company of your opposite column where you would not fit anyway.

So for the people out there saying “getting fired was the best thing that happend to me”. Well it may have been in that instance for you personally. This was probably due to moving from a bad column person company to a good column person company.

Note that if you got fired from a bad column person company and move to another bad column person company then it is unlikely that your situation will get any better in terms of the firings and job references they give you.

Agency vs freelancer my opinion

So I read a lot of articles online about “Agency vs freelancer” the subject of the article being from a client point of view which is best to go with in terms of completing a project successfully.

Sometimes I read the article on a general web development article website which is fine. However sometimes I read the article on a web agencies very own website.

I have possibly written similar articles in the past on this website which I have now deleted and let me tell you why in my opinion it is not a good idea for multi-employee agencies to post articles like the above in their blog.

So let us be honest the article is intended to big up the agency whilst making the freelancer look like a bad choice. This is the fundamental intent of the article and that is why it was posted to the blog in the first place.

Whilst on the surface the article seems to do this there is another underlying tone to the article in my opinion which communicates a different message.

The message it communicates is that the agency views the freelancer as the competition. Writing “Agency vs freelancer” or similar as the article the agency has automatically created the freelancer as the competition. This massive, cutting-edge, leading agency views this small, tacky freelancer as the competition?

This is similar to say Rolls Royce viewing themselves in competition with Ford. Ford does not lose any sales to Rolls Royce and Rolls Royce does not lose any sales to Ford, they are at completely different ends of the market.

The article can give an impression that is at odds with the articles intended purpose.

Having said all this I will also say that there is one other area I will mention in relation to articles such as the above.

Many of these articles attempt to portrait that an advantage of going to an agency is that you get a “team” of people working on your project vs the 1 person freelancer working on your project.

The way this is generally written in the article tends to try to give the following impression …

Freelancer hourly rate = £50
Agency hourly rate = £100

Wow, I am getting a full team of people working on my project rather than the 1 person freelancer for only double the price.

Errrrmmm, not quite. You see that is £100 per hour. So for an agency of 8 people that will cost you £800 per hour. The result is more like as follows …

Freelancer hourly rate = £50
8 person agency hourly rate = £800

You are still paying by the hour no matter what way you look at it. You just happen to be paying double per hour in the above example to have 1 person work an hour for you from an agency.

Let us face it, freelancers have contacts as well and I am certain that if you where willing to pay the freelancer £100 an hour it would not be very hard for the freelance to get some external resources into the project himself thus forming a mini-agency.

Infact you could have just given the freelancer the break he needed to expand his business himself into an agency.

King of Adem – progress update – building queue

Progress has been made on the King of Adem project. For those of you not familiar with this project please read the original King of Adem post at …

http://www.jamesbarnsley.com/site/2017/08/13/king-of-adem/

Since the update I have been primarily working on the building queue. The building queue is the queue which buildings get inserted into when a player wishes to construct a building in one of his / her regions.

To get to the construct building screen the user has to be in one of his / her regions so those areas to get to that screen had to be part built.

The areas part built are namely user, game, region and building. Once on the construct building screen the user can select from a list of buildings that can be constructed.

Each building is validated on a case by case basis and if it does not pass the validations the building cannot be constructed, here are some of the validations so far …

Check fund available
The player must have the correct amount of fund available to construct the building. The amount of funds available is the players start of turn funds subtracting any funds that have been used in the current turn.

For example player may have started with 1000 coins, if a player constructs a building for 300 coins then the program gets the players start of turn funds but substracts 300 coins because it knows a building worth 300 coins was requested to be constructed this current turn.

Check building queue full
The building queue may only allow so many buildings to be in the queue at any one time for each region. Currently this number is set at 5 and the number is stored in a config file.

Check building already built
Check if the building has already been constructed in the region. Each building has a “constructionLimitPerRegion” variable which stores the number of times that particular building may be constructed in each region.

Check dependent buildings constructed
Each building can have what are called dependent buildings. These are buildings which need to be constructed first before you can build the current building.

Also the validation knows whether a building is an upgrade of an existing building or a completely different building that just happens to depend on another building being constructed.

All kinds of checking had to be implemented for this validation, whether building is an upgrade / dependant or just dependant, whether advanced tier buildings already exist for lower dependant buildings, whether lower dependant buildings already exist etc.

Check advanced tier building exists
Check if an advanced tier building already exists for the current building wanting to be constructed. If the advanced tier building already exists then there is no reason to build the current building.

Again this needs to take into account that some buildings can be constructed more than once so in some instances it may need to allow the building to be constructed even though the lower building has already been constructed.

The above validations are run everytime a user issues the command to insert a building into the building queue for a particular region. These validations are run from the “Building” Model. Each building is also given its own Model, the hierachy is like as follows …

\yii\db\ActiveRecord -> \app\components\ActiveRecord -> \app\models\Building -> \app\models\BuildingTownBarracks

Or …

\yii\db\ActiveRecord -> \app\components\ActiveRecord -> \app\models\Building -> \app\models\BuildingPort

etc.

I can run special validations for specific buildings by placing those validations in the specific building class. Those validations will only run for the specific building and not all buildings.

That is as far as I have got so far. Still a long, long way to go, more updates to come soon.

King of Adem

The latest personal project I am working on which also happens to be the largest personal project I have worked on so far.

King of Adem is a game for tabletop minature wargamers. Players manage their kingdom through the app. Kingdoms consist of a number of a territories, each player starts with one.

In each territory there is a capital city in which buildings can be constructed. Buildings provide benefits like more money or the ability to recruit armies.

When 2 armies collide by moving into each other the players can then resolve the battle as normal by playing their normal game of tabletop minatures.

The game takes place on an island called Adem. Adem is a recently discovered island where each player is fighting for control. You fight to become the “King of Adem” by conquering the whole island.

Although Adem is stated as “recently” discovered, this only means “recently” in the grand scheme of things. Adem actually has a number of Kings / Factions already on the island and each Kingdom has some infrastructure.

The project is currently divided into 3 parts …

King of Adem Diagram

Part 1 – Game Server
The Game Server is currently in construction and is being developed using the Yii 2 framework written in PHP. The database is MySQL. The server takes view requests and command requests from the client application.

View requests provide data to the client in JSON format. This data has everything needed to render views for the game for example, building, army, territory data.

Command requests provide data to the server about interactions happening in the client application. For example move commands, construct building commands etc.

The decision to use View / Command requests was chosen instead of opening the server up entirely.

Essentially one option could have been for each Data model on the server to have a view, insert, update, delete request to use for either commands, views or both.

This would give the client more freedom at the cost of putting more logic onto the client.

As the first client is being developed using IOS and IOS uses a view per screen type of structure then View requests make sense. Giving the client the exact data the client needs in order to render the view.

Part 2 – Game Test Client
The Game Test Client is currently being developed alongside the Game Server and serves as a testing ground for the development of the server.

The Game Test Client can send View requests to the server and read / present the data from the server in a user friendly way. The Game Test Client can send command requests to the server.

The Game Test Client is being developed using the Yii 2 framework written in PHP. The Game Test Client is my own personal client for testing the server and is not the actual client the end user / player will be using.

Part 3 – IOS Game Client
The IOS Game Client is the actual client the end user / player will be using to play King of Adem. The IOS Game Client as you might have guessed will be an IPhone application. There may also be the ability to run the application on the IPad as well.

I decided to develop the IOS Game Client because I wanted the game to be portable and it cannot get much more portable than having the game on your phone.

I also choose the game to be an IOS application because I want to improve my IOS skills and offer IOS development as part of the everyday services I offer to my clients.

Once these 3 parts are complete the game will be playable and released to the public. Part 1 and 2 are already in construction, Part 3 will be constructed after parts 1 and 2 and completed.

After the game has been released to the public, a number of other client applications could potentially be developed including an Android version and a full game client developed in Unity3D or similar.

The IOS client will be more management like as opposed to a graphical game. The IOS application could include graphics etc, but it will not be at the level of a full game like experience. This is why I suggest a later developed Unity3D or similar game engine development.

I will be keeping everyone informed of the development of this game through the blog so keep checking back for further updates.

Retaining great employees

There is talk on the web about retaining great employees and the way some of the articles are written about the subject you would think a lot of companies have a real struggle to get and keep great employees.

Not sure what makes the employees “great” employees other than the fact that if they are the ones leaving the company they obviously have options elsewhere.

So what would I do if I had a company with employees and I wanted to retain them without going over the top and paying them huge salaries obviously …

Allow home working

Allow the employees to work from home as and when they choose to.

Less hours in work day

Expect 5 hours “in the zone” work from each employee each day but pay them for 7.5 / 8 hours.

Allow the employee to revise skills, have breaks / lunch, collaborate with coworkers, work on personal projects, exercise or even depending on your limits allow the employee to occasionally take the rest of the day off in the 2.5 / 3 hours spare.

Alternatively if the employee really wants to, the employee can carry on with his / her workload. It is up to the employee what he / she spends the 3 hours doing each day.

Some afternoons could be spent doing group activities whether work related or fun related.

Also note that the above idea is not an excuse to compress more or the same amount of work into less time. If that happens then the above advantage has obviously been nullified.

More holidays

20 to 21 days holiday entitlement a year? Increase it. I think for every 8 weeks worked an employee should get around 5 days off work.

Lunch around a big table together

Have the whole company eat lunch together every working day around a big table or multiple tables if needed.

Once a month meal

Once a month take the employees out for a meal, paid for by the company.

Every 3 months have an employee activity day

Every 3 months arrange for there to be a fun activity day such as paint balling or some other group fun activity.

Have an open culture

Set the tone of the workplace as a place where people can ask work related questions of their teammates. Work related mistakes should not be jumped on but merely highlighted in a nice way.

These are the points I would implement in my business if I owned a multi employee business. Not all the points would have to be implemented but the more points the better. I would only apply these rules if the staff where regular employees, the rules would not be applied to contractors. Obviously if the workplace is toxic then none of the above points apply and employees will leave anyway but that is a different topic of discussion.

Tips for freelancers

Here are my tips for new or existing freelancers. Looking around the web there are many articles that discuss this theme and they all seem rather identical to each other.

I personally get bored of reading these after a while but I can promise you that even though some of the ideas discussed in this article may be similar to other articles, they are all my personal tips. Each one of these tips is important even if they may seem generic …

Shared office space

Certain situations may happen whilst freelancing when you need the advice of other freelancers. Preferably this would be face to face advice. When working in a shared environment with other freelancers this gives you the opportunity to get the needed advice right at the time you need it.

You may think that having your own private office is better as it avoids interactions with others but this can be a lonely road and getting advice on internet forums or through Skype just does not have the same effect.

Also being around other freelancers can provide a motivational boost to your energy levels as well as providing opportunities you may have missed whilst working in a private office.

Sell yourself as an individual

If you are freelancer then sell yourself as a freelancer. At a certain point in time a client is going to know that you are individual and not a group of people anyway.

A client will wonder where the rest of the company is, at that point you will have to explain that the company consists of just yourself. Save yourself the hassle and just sell yourself as a freelancer.

Payment schedule

Agree to a payment schedule with your clients and stick to it. Preferably agree a payment schedule where you are always paid in advance so you are never having to do work that you have not already been paid for.

Hourly rates

Charge hourly rates for your work. Quoting for fixed price is always a headache.

Even if you think you have everything covered in your fixed price contract the requirements of the contract can be too subjective and no matter how detailed it appears some of the requirements will always be left open to interpretation i.e. client wants a new kitchen, you build him the kitchen, client actually wants a kitchen with gold work surfaces.

Yes you can argue that you have provided the kitchen thus fulfilling the requirement however the client will also argue that the gold work surfaces came under the “new kitchen” requirement.

Arguments can then happen, clients may threaten legal action, you are a 1 person freelancer against the larger client company. You can take this risk if you want to or you could just save yourself the hassle and charge hourly.

Sustainable hourly rates

You may find that you will not always have a full schedule of work as a freelancer. There are gaps in the work flow and other expenses that need to be accounted for.

Clients will always know someone who can do it cheaper but the bottom line is it does not matter what Bill or Joe next door charges for their freelancing services. Your hourly rates need to be sustainable.

If you do not have sustainable hourly rates then you will not have a business for very long. This business has got to support you if it does not then it will not last.

There is no point in charging cheaper rates to get work if those rates are not going to sustain your business.

Contracts

Adopting the “hourly rate” idea earlier your contract should be reasonably straightforward, an hour of work done, an hour of work paid.

Charging by the hour takes some of work out of the contracts as you not agreeing to a requirements specification on a feature by feature basis.

Always have a contract though nevertheless, contracts provide help when dealing with client disputes.

Client profiling

Do not just accept any client that wants you to do work for them.

Profile the client …

  1. What are the clients good and bad points?
  2. Does the client value your work or see you as a commodity?
  3. Is the client someone you really want to work with?

Come up with a profile of the clients you want to work with and only or at least mostly only work with those types of clients.

Process

Have a clearly defined process for dealing with clients from on-boarding, design, development, testing, delivery and on-going work.

Every freelancer will have a process of some sort but it is important to get the process defined and written down.

The process is good material to present to clients thus helping with marketing activities. The process is important to you as it improves reliability.

Consider these tips if you are a freelancer or are new to freelancing.

Select2 library solution in Yii2 part 4 – retrieving the data

To continue on from the previous article I wrote about saving the Select2 data …

http://www.jamesbarnsley.com/site/2016/11/07/select2-library-solution-in-yii2-part-3-saving-the-data/

I will now talk about how to the retrieve the stored Select2 data so that the data can be displayed on the front-end Select2 field. Following on from the Task / Countries example, place the following “loadSelect2Items” method inside the Master model or Task model if you did not create a Master model …

The code above can be explained as follows …

  1. Line 3 – return the array of Select2 data using the “array_map” function
  2. Line 4 – store the related entity models name into a variable, in this case that would be the Country models name. This uses the Country relationship that was created in “Part 2” of this article series
  3. Line 5 – store the IDs of the related relationship entity model in the return data, in this case that would be the IDs of the Country model records
  4. Line 6 – find all the records for the model / model property in the “select2_data” table, in this case that would be the Task model and “countries” property

The next step is use the “loadSelect2Items” in the Task model to the load the Select2 data into the “countries” property, place the following inside the “afterFind” method of the Task model …

So that concludes the Select2 library solution for Yii2. I have covered the creation of the necessary database table and the read / write methods for reading / writing the data.

After the initial methods have been setup this solution will provide you with a way of easily putting a Select2 field on your forms that allows for many to many relationships between models with just a couple of line of code.

Select2 library solution in Yii2 part 3 – saving the data

To continue from the previous article …

http://www.jamesbarnsley.com/site/2016/10/31/select2-library-solution-in-yii2-part-2-database/

Once the database table has been created the code can now be written to display the Select2 field on the form and save the Select2 data to the database table.

In this article I will demonstrate the Select2 functionality as though we are wanting to add “Tags” to a Task model. When I say “Tags” I mean the standard web term for Tags. Essentially these are words that you can tag to an entity for example a WordPress article that categorizes the WordPress article using the tags allowing a user to search by those words.

When I say Task model, this is a fictional model in a Yii2 application that will allow the user to create and update tasks. For the purposes of this article it is assumed you will create your own Task model along with the controller and views. Create a standard CRUD for tasks possibly giving each task a name and a description.

You will need to create a Tag database table to store the Tag data along with its associated model. Create a database table with “id” field for the primary key and a “name” field to store the Tag name. Create a standard Yii2 model for the Tag table, this model does not need any methods inside it, it is a blank Yii2 model that extends ActiveRecord.

After you have created your task CRUD functionality and the Tag database table with associated model the Select2 functionality can be integrated.

First the field is added to the form using the Kartik Select2 plugin. The plugin can be found here …

http://demos.krajee.com/widget-details/select2

Add the field to the form using code similar to this. Include the plugin in the form …

Add the field to the form …

Create a “tags” property in the Task model, this will be used to store the tags field data on both saving and retrieving …

Now the field is added to the form and you have a property in which to store the array of data which the Select2 library needs in order to function. Next you need to use the “saveSelect2Items” items method as shown below to save the array data in the “tags” property to the database.

The “saveSelect2Items” method can be placed inside the Task model but it would be better in some kind of global area such as a Master model as is it likely that you will want to use the functionality in more than one model / form. I wrote an article on how to create Master models which can be read here …

http://www.jamesbarnsley.com/site/2016/03/14/create-a-master-model-and-controller-in-yii2/

The “saveSelect2Items” method is as follows …

The code above can be explained as follows …

  1. Line 3 – get the class name of the model object in this case that would be the Task model and convert it to lowercase characters
  2. Line 4 – get the class name of the store model object in this case that would be the Tag model and convert it to lowercase characters
  3. Line 5 – get the Select2 array data of the model object in this case that would be the “tags” property in the Task model
  4. Line 7 – if the model object Select2 property is not empty, in this case that would be the “tag” property
  5. Line 9 – get the current stored records in relation to the model and model properties we are using. In this case the Task model, Tag model and “tag” property
  6. Line 11 – loop through the currently stored records
  7. Line 13 – if the related model object ID is not in the model object property array data. In this case that is if the Tag ID is not in the “tag” property array data
  8. Line 15 – set the deleted flag to 1, which indicates deleted
  9. Line 16 – save the record
  10. Line 22 – loop through the array data in the model object property, in this case that is the Task model “tags” property
  11. Line 24 – check to see whether a record already exists in relation to the model and model properties we are using. In this case the Task model, Tag model and “tag” property
  12. Line 26 – if no existing record can be found
  13. Line 28 to 33 – create a new Select2 record and set the properties
  14. Line 35 and 36 – save the new Select2 record but if it does not save then return false
  15. Line 42 – the else part of the original if statement, if there is no array data in the “tags” property
  16. Line 44 – delete all the existing records for the model we are dealing with. In this case that is the Task model

As mentioned in the database article the “saveSelect2Items” method soft deletes the unwanted Select2 records with a little adaptation this could be altered to hard delete the records.

Now that the “saveSelect2Items” has been created you can call it in your Task model by placing the following code in the “beforeSave” method of the Task model …

The first parameter of the “saveSelect2Items” is the property used to store the Select2 array data, the second parameter is the original model that uses the Select2 field on the form in this case that is the Task model. The third parameter is the relation model in this case that is the Tag model.

The code up to this point puts a Select2 field on a form and saves the Select2 data when the form is saved. If you go back to the form the Select2 field will not be populated with the saved data yet as that is covered in the next article which talks about retrieving the Select2 data.

Select2 library solution in Yii2 part 2 – database

To continue from the previous article …

http://www.jamesbarnsley.com/site/2016/10/24/select2-library-solution-in-yii2-part-1-introduction/

I will now talk about the database table needed to get my implementation of the Select2 library working. As mentioned earlier the original design for the database included a table for each set of Select2 relations. So if I wanted a Person to be able to select multiple Countries I would have a Select2 PersonCountries table, 1 table per relation.

The latest design for the database has just 1 table to store all of the Select2 relations used throughout the web software application. I find this easier as I can then use the Select2 like a module, whenever I want Select2 in my web software application I just drop in the code files along with the database table.

The database table I am using is as follows …

I will now run through what the columns in the database table are used for …

  1. id – unique integer for the record
  2. entityModel – the short name of the model class stored in lowercase characters, for example the “Person” model
  3. entityModelField – the name of the model property used on the form to select the related Select2 relations, for example “countries”
  4. entityModelID – the ID of the model, for example the ID of the “Person” model
  5. relationEntityModel – the short name of the related model class in lowercase characters, for example the “Country” model
  6. relationEntityModelID – the ID of the related model, for example the ID of the “Country” model
  7. deleted – a flag to determine if this record is deleted
  8. createdBy – the ID of the user this record was created by
  9. createdDate – the date this record was created
  10. updatedBy – the ID of the user who last updated this record
  11. updatedDate – the date this record was last updated

Please note that “id” is just a standard primary key and is not unique to this article also deleted, createdBy, createdDate, updatedBy and updatedDate are not unique to this article. These are just standard columns that I populate all of my tables with and are not really necessary for the purposes of this article.

The deleted column may be necessary if you are running the code exactly as explained in this article but the record could have just as easily have been hard deleted and hence the deleted column is just personal preference.

To know more about the standard columns I use and to auto populate these columns read the following article …

http://www.jamesbarnsley.com/site/2016/04/04/populating-standard-columns-in-a-database-table-using-yii-2/

The above database table is the table I use to power the Select2 library relations in my web software applications. An example use case for this can be described as follows …

You have a update Person form (entityModel) with a countries field (entityModelField) that allows the user to select various Countries that are related to the person (the ID of the Person is the entityModelID). You can select various Countries and save them to the Person. For each country saved a select2_library record will be created, the ID of the Country is the relationEntityModelID and the Country model itself is the relationEntityModel.

At this point a Select2Library model will need to be created for the “select2_library” table. In the example used in this article a relationship will need to be created between the Select2Library model and the Country model. This will need to be created using the “relationEntityModelID” like this …

All of the above will become clearer when actually running the code and seeing the results. The code to get this to run will be described in the follow up articles.