Spawning objects


Imagine you have a physical production facility and this facility has input store, two production steps and output store. Imagine you take a batch of 10 unit details to the entry store. This batch has given "entry documents":

  • number of unit details

  • quality certificate

  • set of images showing state of packaging

  • etc.

The critical point is that documentation is ALWAYS for entire batch - not for an individual unit detail!

Now imagine this batch is moved to production. It passes through step I and some new documents are being attached for this step (e.g. measurement report) and it goes to production step II. And here we have complication! For whatever reason during step II is has occurred part of the batch (e.g. 6 detailes) must be send back to step I for correction while the rest (4 detailes) go to output store.

The essential point is that here the integrity of the batch is broken and since this point we should treat part of 6 detailes as "Batch-1a" and part of 4 detailes as "Batch-1b". Nevertheless - all heredity information has to be kept intact - production manager must be able to track where are all detailes from given entry batch regardless its further spawning and each batch has to carry all documents gained during production.

Can you please help me - what OOP datastructure will be best suitable to manage with this sort of problem? How to integrate this within Yii2?

Thanks ahead!

I would do something like (general idea):


  • Unit = keeps informations about single Unit that will not change during the moving process.

  • BatchUnit = junction table to connect Unit and Batch objects, keeps additional information about the Unit status in Batch (default NEW).

  • Image = state of packaging photo.

  • Certificate = quality certificate.

  • Batch = whole batch with relations:

– BatchUnit (hasMany),

– Certificate (hasOne),

– Image (hasMany)

Batch gets attribute parentId which is 0 at the very beginning.

During the moving process BatchUnit, Certificate and Image objects are created and connected to Batch.

A ) If everything is fine Batch finishes with all BatchUnit objects set to status OK.

B ) If some BatchUnits need correction:

  1. Those BatchUnits get status CORRECTION.

  2. New Batch object is created with parentId set to previous Batch ID.

  3. New Batch gets all BatchUnits from parent Batch that need correction (new BatchUnit object are created with the same data as parent Batch BatchUnits but with default status).

  4. New Batch gets new Certificate and Image objects if necessary (new BatchUnit as well).

Repeat A-B for every child Batch untill all related BatchUnit objects have status OK.

Thanks to this we got whole Batch tree, we can see where and when BatchUnits were sent to correction and follow each step of moving process. We can track every Batch parent as well and if needed we can gather the whole list of images and certificates for full Batch tree (parent and every child) with recursion function.

Typical output can look like this:

Batch 1

  • parentID 0

  • BatchUnit A (Unit I) [status OK]

  • BatchUnit B (Unit II) [status OK]

  • BatchUnit C (Unit III) [status CORRECTION]

  • BatchUnit D (Unit IV) [status CORRECTION]

  • BatchUnit E (Unit V) [status CORRECTION]

  • BatchUnit F (Unit VI) [status CORRECTION]

Batch 2

  • parentID 1

  • BatchUnit G (Unit III) [status OK]

  • BatchUnit H (Unit IV) [status CORRECTION]

  • BatchUnit I (Unit V) [status CORRECTION]

  • BatchUnit J (Unit VI) [status OK]

Batch 3

  • parentID 2

  • BatchUnit K (Unit IV) [status OK]

  • BatchUnit L (Unit V) [status OK]

What do you think?


Thank you very much for your input, let me think about this tomorrow!

OK - this is all clear but I have to ask you to confirm:


Initially, I thought you’ve meant “a single detail” (a batch is composed of identical, individual details of given sort) but after next reading I am not that certain any more. Please - clarify!

Thanks ahead!

Unit is unit’s detail indeed. It’s quite abstract for me so I’m not sure if I used it correctly but as a general idea should work fine.


This is then a bit impractical - I mean relation Batch (one) - Unit (many). Imagine a batch of 1500 units for instance (e.g. small plastic parts to be painted). Any comment, please!

You need to store information about single detail if it can be sent to correction.

One Batch to many Units is natural relation in this case. As for the performance you don’t have to keep all 1500 units in memory at once.

If every unit is the same we can do it the other way - store number of all units in batch and number of units that need correction instead of every unit detail.

You mean - to keep the original (after creation) number of details as a batch property and dynamically calculate it’s current number of details by subtracting number of details in each of it’s children?

Sounds interesting!