Skip to content

Instantly share code, notes, and snippets.

I'm trying to decide how to model relations where one type of entity is owned by one of several other types of entity. For example, my app involves scheduling. A schedule entry (which corresponds to one record in a schedule table) looks pretty much the same regardless what it belongs to: a meeting, a publication, a broadcast.

Options I considered:

  1. One schedule table with three nullable FKs to the meetings, publications, and broadcasts tables. It doesn't enforce the constraint that for each row, one and only one of the three FKs must point to an existing PK in its corresponding foreign table.

  2. One schedule table with a "polymorphic FK" column and an FK type column indicating to which of the foreign tables the "polymorhpic FK" points. This admits no DB constrains and is just horrible.

  3. Three schedule tables meeting_schedule, publication_schedule and broadcast_schedule each with one proper FK to its owner table. Solves the constraints problem but I need to run queries over all schedule

@tom--
tom-- / 3-way-joins.md
Last active October 20, 2020 09:42
Using 3-way join tables in Yii 2 Active Record

I have an interesting Yii 2 AR design problem.

The DB has 12 primary entity tables related via a full mesh of 72 join tables. Each relation also has a type (and attributes) but those aren't stored in the join tables – they are in additional tables that the 72 join tables reference. So each of the 72 join tables provides a 3-way join between one entity, another entity and another table called link that points to the link_type.

My problem is to write the AR models and relation methods.

Let's make this more concrete with two of the primary entities: artist and recording, related through l_artist_recording. Each record of l_artist_recording has an FK to the link table, through which I can load the link_type name. For example, an artist-recording relation might have link_type "performer", "conductor", or "producer" etc.

This is a simplified schma good enough for thinking about the AR p

@tom--
tom-- / WizardController.php
Created March 25, 2015 13:53
Controller class for Yii 2 to support a wizard using one form model and several actions with branching logic.
<?php
namespace spinitron\controllers;
use Yii;
use yii\web\Controller;
use yii\web\Session;
class WizardController extends Controller
{

Yii 2 basic vs. advanced apps

The names of the two demo apps, Basic and Advanced, can be unhelpful. Many people end up choosing advanced just because of the name even though basic would suit them better.

The key differences are:

  1. Advanced has a complex system of configuration. It is designed so that a team of developers can work together on a project keeping everything, including app config, in VCS but each developer can customize their own config for development, testing and other environments.

    This configuration arrangement can be confusing and is often unnecessary. If you are working alone, you'd probably want to avoid it.

@tom--
tom-- / gist:14864ce73c2a70d132bd
Last active August 29, 2015 14:15
Another Yii FAQ

Question: "I would like to know if it good idea to use Yii for developing..."

First, are you sure that:

  1. A server-side webapp is what's needed?
  2. MVC is the right architecture for the requirements and not, say, CMS?
  3. PHP is suitable for your situation?

If yes to all then Yii might work well for you and is worth considering. Start reading the user guide and take the demo app for a test drive. If you like it then you may be getting closer to an answer to the original question. If not then there are alternatives for you to try.

@tom--
tom-- / revealing-module1.php
Last active August 29, 2015 14:15
Can PHP's so-called "closures" do any real closure work, like a revealing module? http://addyosmani.com/resources/essentialjsdesignpatterns/book/#revealingmodulepatternjavascript
<?php
function constructTotalizer() {
$privateTotal = 0;
$privateAddTotal = function ($n) use (&$privateTotal) {
$privateTotal += $n;
};
$publicAddTotal = function ($n) use ($privateAddTotal) {

I've had it up to here with the phony neo-romanticism of "epic" bike rides.

It started, as I recall it, with Rapha's monochrome marketing images of beautiful young men riding bikes and drinking black coffee in over-understated euro-chic clothing and without helmets, which spoil the look. The presentation always came with some implied or overt association with the history of pro bike racing and the heroism and torment of long hard bike rides.

The monochrome photography has become a mark of the genre. Its canonical form is film of Paris-Roubaix with soundtrack replaced by plaintive, soulful music to express the, passion and suffering, plenty of slow motion, maybe some amped-up contrast, images of muddy-faced cyclists, many of them disconsolate at the side of the road, out of the race due to a crash or equipment failure. The correspondence with Wagner or ETA Hoffmann disturbing: bombast, bogus mythology, Ach Weh, escapism and all that incoherent rot.

It quickly became—to what extent by clever engineering, I

  • Modules are for sub-applications, reusable across projects. rights is a fairly good example.
  • Modules are not just a way of organizing your code into directories and don’t work well for that purpose.
  • User management doesn’t easily fit in a module because it is usually coupled to application business logic.
  • For the same reason, user management does not easily abstract into any kind of extension or into the framework APIs.
  • So you have to implement user management yourself and the right place is in the main app, just like yii2-app-advanced currently demonstrates.
  • The best you can do to abstract parts of the user management functionality into reusable code is to put some of the functionality into extension classes as nineinchnick/yii2-usr does.
@tom--
tom-- / MVC or not.md
Last active August 29, 2015 14:06
Will Yii show down your web site?

Will Yii slow down your web site?

The following question appeared out of the blue in #yii on Freenode in September 2014:

hi i have started working on yii framework... and other pople have been continuously telling me that its a bad decision and using yii will slow down the site and performance will not be good, better go for core php...any of your thoughts will be great help..cheers

How much penalty is there in using an MVC framework such as Yii?

There is no way to really answer the question because it depends so heavily on the specific application, the technologies involved and their deployment. Plenty of attempts have been made to benchmark different MVCs but the results are usually perfectly irrelevant to any given application. It would be a foolish mistake to think that data from, for example, TechEmpower's Web Framework Benchmarks would predict relative performance of your web site.

@tom--
tom-- / Gluster repl node replacement.md
Last active August 29, 2015 14:04
Replication node replacement

In a replication cluster, one of the nodes fails. I replace it with a new machine with the same hostname and address as the failed node. Then, with glusterd stopped on the replacement and running on the survivors

  • Get UUID of failed node from /var/lib/glusterd/peers on one of the survivors
  • Give it to the replacement node by putting it in /var/lib/glusterd/glusterd.info
  • Start glusterd on the replacement
  • On replacement, peer probe one of the survivors
  • Restart glusterd on replacement