Page 1 of 1

model calling model | or controller calling : correct way?

Posted: Wed Dec 23, 2015 9:04 am
by publicGenome
Hi There,

I'm fairly new to PHP and web development. And I'm facing a situation to which there may be multiple ways to do, but which one is correct, I'm unsure of.
Laravel 4.2, PHP 5.5.24 (cli) MySQL 5.6.23 - home brew

I've model for each table.
I've 3 steps in one feature:

My current feature has flow as:
- provides name of pipeline and save
- Create link of tools for the pipeline above created
- get id and update pipeline

In order to create pipeline, I do an insert:

Controller:-

Code: Select all

		$pipeline_model_object=new Pipeline_model();
		$pipeline_id=$pipeline_model_object->create_new_pipline();
//get pipeline id
Database:

Code: Select all

+----+---------------+------------------+
| id | pipeline_name | id_analysis_link |
+----+---------------+------------------+
| 12 | final_test    |               NULL |
I return the id after creating new pipeline

Controller:-
I use this id :

Code: Select all

		$pipeline_model_object=new Pipeline_model();
		$pipeline_id=$pipeline_model_object->create_new_pipline();
		
		/*
		 * create new pipeline have its id_analysis_link as NULL
		 */
		
		$analysis_id=(new Analysis_link())->create_new_analysis_flow($pipeline_id,$tool_array);

pipeline id is foreign key to the analysis_link table.

Code: Select all

+----+-------------+-------------+------------------+
| id | id_pipeline | id_tool_app | next_analysis_id |
+----+-------------+-------------+------------------+
| 35 |          12 |           1 |               36 |
| 36 |          12 |           2 |               37 |
| 37 |          12 |           3 |               38 |
| 38 |          12 |           4 |             NULL |
+----+-------------+-------------+------------------+

In short, pipeline 12 will go to 35, 35 uses tool_app -1, then next analysis is 36
36 uses tool_app -2, then next analysis is 37
and so on.

Next, I return the id from analysis_link back to the controller, and update the pipeline:

Code: Select all

		$pipeline_model_object=new Pipeline_model();
		$pipeline_id=$pipeline_model_object->create_new_pipline();
		
		/*
		 * create new pipeline have its id_analysis_link as NULL
		 */
		
		$analysis_id=(new Analysis_link())->create_new_analysis_flow($pipeline_id,$tool_array);
		/*
		 * create analysis chain.. and update them there and then
		 */
	
		$pipeline_model_object->update_pipeline($pipeline_id,$analysis_id);	
updated:

Code: Select all

+----+---------------+------------------+
| id | pipeline_name | id_analysis_link |
+----+---------------+------------------+
| 12 | final_test    |               35 |
Is this correct way?
Or, should I call analysis_link model after inserting pipeline.
And again then and there itself I should update?

I'm now facing similar situation in my code ahead.
Please shed some light.

Thank you

Re: model calling model | or controller calling : correct wa

Posted: Wed Dec 23, 2015 10:35 am
by publicGenome
A follow up question on this:
If my output has to be from query on joins on different tables; how does calling individual model for table work in this case?

Thanks.

Re: model calling model | or controller calling : correct wa

Posted: Wed Dec 23, 2015 2:26 pm
by requinix
FYI I edited your post: the data tables work better with code tags (will preserve whitespace) than quotes (won't).


The code looks sane to me: a distinct operation to create a new pipeline, then a distinct operation to create analysis chain thing, then the necessary step of updating the pipeline with the analysis stuff. (Sane given the data structure...) Personally I would rather do it that way than combine the two steps into one as this is easier to read and understand.

However, the data structure. I would suggest changing it a bit, and it would help your joining problem too.

Code: Select all

+----+-------------+-------------+------------------+
| id | id_pipeline | id_tool_app | next_analysis_id |
+----+-------------+-------------+------------------+
| 35 |          12 |           1 |               36 |
| 36 |          12 |           2 |               37 |
| 37 |          12 |           3 |               38 |
| 38 |          12 |           4 |             NULL |
+----+-------------+-------------+------------------+
I see two difficulties:
1. You need the id (35) here to link it into the pipeline.
2. The next_analysis_id makes it harder to get all the link things at once.

How about using a sequence number (1,2,3,4) instead of a linked list (35->36->37->38)?

Code: Select all

+----+-------------+-------------+----------+
| id | id_pipeline | id_tool_app | sequence |
+----+-------------+-------------+----------+
| 35 |          12 |           1 |        1 |
| 36 |          12 |           2 |        2 |
| 37 |          12 |           3 |        3 |
| 38 |          12 |           4 |        4 |
+----+-------------+-------------+----------+
1. You don't need the id with the pipeline and can simply search for the record with id_pipeline=12 and sequence=1.
2. You can find all records by searching id_pipeline=12 and sorting by the sequence number.

Technically you could do away with the id too as the unique key for each record is (id_pipeline,sequence). If you did that then you could put the auto_increment (only one allowed on a table) on that key, meaning sequence is generated automatically for you given each id_pipeline.

Code: Select all

INSERT INTO table (id_pipeline, id_tool_app) VALUES (12, 1) /* sequence=1 */
INSERT INTO table (id_pipeline, id_tool_app) VALUES (12, 2) /* sequence=2 */
INSERT INTO table (id_pipeline, id_tool_app) VALUES (12, 3) /* sequence=3 */
INSERT INTO table (id_pipeline, id_tool_app) VALUES (12, 4) /* sequence=4 */
INSERT INTO table (id_pipeline, id_tool_app) VALUES (13, 11) /* sequence=1 */

Re: model calling model | or controller calling : correct wa

Posted: Tue Dec 29, 2015 9:33 am
by publicGenome
Hi requinix,
Sorry for my delay in replying.

Thank you very much for the detailed reply, editing my post. :)

I'll take into consideration the sequence column. That is very useful for me, for what I need further. It is excellent.

I can use this for my next process/pipeline:

Code: Select all

select app_value from tool_app LEFT JOIN analysis_link_test
 ON tool_app.id=analysis_link_test.id_tool_app where analysis_link_test.id_pipeline=12
 order by analysis_link_test.sequence;
The output I get is exact what I had wanted at: viewtopic.php?f=2&t=142090

The app_value output comes out as the order/sequence (I hope I'm making sense).

This solves purpose on both sides.
I'll not have an auto-increment on sequence, as I/user might want to update the pipeline. I'll look more into update thing before I finalize this table structure.

Thanks again for your help, and guidance! :)