0

I have a table {cases} which has in part, upload_id, case_id case_name etc etc. With a custom form, users, depending on role(4 custom roles), will upload a file upload_id. This has been serialized. If it is a new case, case_name is NOT present then get max(case_id) add one and save to DB. Also users can add a file to existing cases, so the upload_id would auto-increase, and the case_id stays the same. I've been working on this one area for 2 weeks and have come to the end of my rope.

I have written all of this into a custom PHP web app a few years ago, but now I have to rewrite it all in Drupal 7. Any pointers in the right direction would be appreciated.

**********EDIT**************

I didn't think you could have 2 Serial Fields in the same table schema. I already have upload_id as a serial. I just can not figure out how to insert the case_id in Drupal.

Real scenario:

User uploads the file, upload_id =3796, case_id = 2916, case_name = 'xxx Macy'.

User forgot to include something needs to upload additional file. upload_id = 3797, case_id = 2916 where case_name = 'xxx Macy'. And still forgot a file, upload_id = 3798, case_id = 2916.

New user uploads file, upload_id = 3799, case_id = 2917, case_name = 'Jane Deer'. Real example

4
  • Your question is a little unclear. Did you already create a custom table and form in your module and need help on how to handle the incrementing or are you still looking for a way to best do this in Drupal to start with?
    – Zuzuesque
    Commented Jul 12, 2016 at 21:48
  • @Zuzuesque Yes I have the table build using Schema API. And the code I am working with is $case_result = db_query("SELECT MAX(case_id) as case_id FROM {cases} ")->fetchCol(); $form['case_id'] = array( '#type' => 'value', '#value' => isset($diagnostic->case_id) ? $diagnostic->case_id : NULL, ); if it is a new case, take $case_result add 1 and insert into {cases} along with the other fields from the form.
    – Steveep
    Commented Jul 13, 2016 at 22:37
  • @Steveep: The way you describe file_id it is not a serial. Serials don't change when editing an entry - case_id would be a serial as it doesn't update when a user uploads a new file. On its own table file_id might be a serial (and likely is) in the case table it is only a number. This is also a general SQL question and not really a Drupal question.
    – Zuzuesque
    Commented Jul 14, 2016 at 16:47
  • @Zuzuesque Here is the Schema I have: 'upload_id' => array('type'=>'**serial**', 'length'=>7, 'unsigned'=>TRUE,'not null'=>TRUE,), 'case_id' => array('type' => 'int','length' => 7,'unsigned' => TRUE, ), As you can see the upload_id is Serial and as such is not even a part of the form, but how do I set case_id? $form['case_id'] = array( '#type' => 'value', '#value' => isset($diagnostic->case_id) ? $diagnostic->case_id : NULL, ); having it be NULL is the only way I found that doesn't cause a white screen of death.
    – Steveep
    Commented Jul 14, 2016 at 19:03

2 Answers 2

2

They way you describe your case_id it sounds like the primary key of your table (a number which increases by 1 for each new entry). Rather than increasing this number in the form (and spare your self the trouble of assure atomic results when more than one person are entering cases at once) you can just set the column as primary key in your schema and let the db manage it. That way you don't need the field in your form at all.

$schema['case'] = array(
  'fields' => array(
    'case_id'       => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
   //your other fields
  ),

  'primary key' => array('case_id'),
);

That said I would consider not using a custom table to start with and instead leverage the functionality Drupal is shipped with and use a node to represent your cases.

There is a contrib module called Serial Field which allows you to add a field to your content type which increases by 1 for each case you create. There is a file field in core which allows you to upload a file to the site as well. The name field is a little tricky as Drupal doesn't allow empty titles but you can easily work around it by adding a simple text field for the name and generate the actual title with Automatic Entity Label.

In this case you need to download the two contrib modules, install them, create a case node type, add and configure the fields and you are done. The advantage is you can still leverage a great number of existing modules working with nodes/fields and entities which is much harder/impossible with custom tables. And you can display the results using Views.

0

It sounds as though you are trying to keep as much of the old PHP app that you wrote and just wrap Drupal around it. You will have much more success if you recreate the app in drupal and then use the migrate module to pull in all the existing data.

As for the data structures in drupal. I would consider using nodes for cases, and then use the file entity module to build more usefulness around the files. Then you can link the two entities together with and entity reference.

2
  • I am not married to PHP, I don't care if it's written in COBOL or even BASIC, as long as the functionality works. As for any existing data, I have that under control, so I would basically be starting from scratch.
    – Steveep
    Commented Jul 22, 2016 at 0:46
  • It isn't about the language it is about the framework. Drupal is written in PHP but, like most of the other answers stated, you're still writing as though you are just writing PHP and not using Drupal. If you are going to use Drupal then you should use Drupal and not attempt to wrap Drupal around your existing functionality. Otherwise you are not using Drupal. Instead you are just writing another PHP app and you would probably be better off just updating the app to the latest PHP and giving it a fresh design. Commented Jul 22, 2016 at 18:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.