Invision Community 4: SEO, prepare for v5 and dormant account notifications By Matt Monday at 02:04 PM
guyroch Posted January 5, 2017 Posted January 5, 2017 I'm trying to add a database column when installing a custom plugin and it is not working. I get no errors when installing the plugin yet the database column is not added. This is my install.php class for the plugin (see step1 method). Is the double // on the first line okay? That was already there. //<?php /* To prevent PHP errors (extending class does not exist) revealing path */ if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) ) { header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' ); exit; } /** * Install Code */ class ips_plugins_setup_install { /** * ... * * @return array If returns TRUE, upgrader will proceed to next step. If it returns any other value, it will set this as the value of the 'extra' GET parameter and rerun this step (useful for loops) */ public function step1() { \IPS\Db::i()->addColumn( 'nexus_packages', array( 'name' => 'p_sku', 'type' => 'varchar', 'length' => 25, 'null' => FALSE, 'default' => '', 'comment' => 'Product SKU code' ) ); return TRUE; } // You can create as many additional methods (step2, step3, etc.) as is necessary. // Each step will be executed in a new HTTP request }
Adriano Faria Posted January 7, 2017 Posted January 7, 2017 10 hours ago, guyroch said: anyone? Next time you better request development support here: https://invisionpower.com/forums/forum/238-development-assistance/. You surely will get faster replies. There's nothing wrong in your script. I can only image that you didn't add a version to it: 1.0.0. This will only works when you create a version. Btw, don't forget to drop that column when you uninstall the plugin. Not sure if you aware, but create a file called "uninstall.php" in the root of your plugin folder (same place of settings.php) with the content: <?php /* To prevent PHP errors (extending class does not exist) revealing path */ if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) ) { header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' ); exit; } try { \IPS\Db::i()->dropColumn( 'nexus_packages', 'p_sku' ); } catch( \IPS\Db\Exception $e ) { /* Ignore "Cannot drop because it does not exist" */ if( $e->getCode() <> 1091 ) { throw $e; } }
guyroch Posted January 7, 2017 Author Posted January 7, 2017 Thank you very much for the reply. You are correct, I omitted the version in the plug in. I'll try that and then come back to confirm. Oh, and thank you for the link for development support. I'll go there from now on... didn't know about it. Regards, Guylain
guyroch Posted January 7, 2017 Author Posted January 7, 2017 Okay, adding the version in the plugin did work, the column is created. However... 'null' => FALSE, ...did not work... the field is nullable albeit the default value did work. Thanks again.
Adriano Faria Posted January 8, 2017 Posted January 8, 2017 On 07/01/2017 at 5:28 PM, guyroch said: Okay, adding the version in the plugin did work, the column is created. However... 'null' => FALSE, ...did not work... the field is nullable albeit the default value did work. Thanks again. I ran you script in a tandon pho file from the suite and column was created. No issue found.
guyroch Posted January 8, 2017 Author Posted January 8, 2017 Thanks for trying it. I still get a nullable column. Not a big issue for now. I was able to add a few front-end hooks to display the sku field in the front end and it works like a charm. The issue I have now is where to add the field in ACP package forms to enter a SKU alongside the other standard package fields. I need a way to populate this field from ACP packages. Thanks.
Adriano Faria Posted January 8, 2017 Posted January 8, 2017 2 hours ago, guyroch said: The issue I have now is where to add the field in ACP package forms to enter a SKU alongside the other standard package fields. I need a way to populate this field from ACP packages. You have to create to hook to extend the form of the proper class to add your field. See an example of extending form:
guyroch Posted January 9, 2017 Author Posted January 9, 2017 Thanks, that is what I'm trying to do right now... but finding the correct class to extend is a bit overwhelming due to sparse documentation. What would this community do without you? Thanks again, I really appreciate it. Guylain
Adriano Faria Posted January 9, 2017 Posted January 9, 2017 Just now, guyroch said: Thanks, that is what I'm trying to do right now... but finding the correct class to extend is a bit overwhelming due to sparse documentation. What would this community do without you? Thanks again, I really appreciate it. Guylain I can't help now, using a mobile device. Tomorrow in the morning I'll show an easy way to identify the class and how to extend the class. Btw, it's the namespace without the .php in file name. So it would be something like: IPS\nexus\package\Package Something like that. Nit sure without see the files.
guyroch Posted January 9, 2017 Author Posted January 9, 2017 Wow, awesome! Was not expecting that level of 1:1 help. A+++ If it helps I'm trying to add the field somewhere in that ACP form... when I create/edit a new product, I'd like to enter and store SKU code in the p_sku field.
Adriano Faria Posted January 9, 2017 Posted January 9, 2017 Yeah. Take a look in the plugin I linked you too. I extend the registration form there.
Adriano Faria Posted January 9, 2017 Posted January 9, 2017 Hook to \IPS\nexus\Package class. /* !ACP Package Form */ /** * [Node] Add/Edit Form * * @param \IPS\Helpers\Form $form The form * @return void */ public function form( &$form ) { $return = parent::form( $form ); $form->add( new \IPS\Helpers\Form\Text( 'new_field', NULL , FALSE ) ); return $return; } That will add the new_field in the end of the form: If you want it after a specific field, then just pass the field as a parameter before the last ) in your field. As a new tab: /* !ACP Package Form */ /** * [Node] Add/Edit Form * * @param \IPS\Helpers\Form $form The form * @return void */ public function form( &$form ) { $return = parent::form( $form ); $form->addTab( 'new_tab' ); $form->add( new \IPS\Helpers\Form\Text( 'new_field', NULL, FALSE ) ); return $return; }
guyroch Posted January 14, 2017 Author Posted January 14, 2017 On 1/7/2017 at 2:28 PM, guyroch said: Okay, adding the version in the plugin did work, the column is created. However... 'null' => FALSE, ...did not work... the field is nullable albeit the default value did work. Thanks again. Finally got the nullable column to work. It's 'allow_null', not 'null' 'allow_null' => FALSE,
Recommended Posts
Archived
This topic is now archived and is closed to further replies.