Constants for tables in migrations

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
User avatar
Autumn Shade
Registered User
Posts: 29
Joined: Thu May 30, 2019 12:38 pm

Constants for tables in migrations

Post by Autumn Shade » Wed Jun 19, 2019 7:19 am

Greetings. I've recently wrote a couple of extensions for my own usage, some with the great help of our subordinates here, others alone.Anyways, I was wondering if PHPBB3.2 extension does the constants when creating tables in migration ? What constants you might say? I mean these constants:

Code: Select all

$sql = 'SELECT * 
        FROM ' . USERS_TABLE . ' 
        WHERE ' . $db->sql_build_array('SELECT', $sql_array);
You see the USERS_TABLE const? That's what I mean. I am wondering if phpbb does this on its' own, because I don't really like the idea to create my own defines in any of the files. If needed I will do so. I want to do that, because right now it's just a raw SQL and I want to change that.

Solution

It is described in the last post from me.
Last edited by Autumn Shade on Wed Jun 19, 2019 10:26 am, edited 1 time in total.

User avatar
david63
Registered User
Posts: 16195
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: Constants for tables in migrations

Post by david63 » Wed Jun 19, 2019 7:41 am

Not quite sure what it is that you are asking.

If you are referring to using a migrations file then yes you can use the predefined pjpBB table constants if you are modifying an existing table.

If that is not what you mean then you will need to explain what it is that you are wanting to do.
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored

User avatar
Autumn Shade
Registered User
Posts: 29
Joined: Thu May 30, 2019 12:38 pm

Re: Constants for tables in migrations

Post by Autumn Shade » Wed Jun 19, 2019 7:47 am

What I want to understand is the following: You create a migration, right? You create a table within the migration, let's say phpbb_david_table. I want to know if a constant like USERS_TABLE is somewhere created when creating the table and migrating it to the database? So the constant will be DAVID_TABLE.

User avatar
david63
Registered User
Posts: 16195
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: Constants for tables in migrations

Post by david63 » Wed Jun 19, 2019 8:04 am

If you are creating a new table then you will have to handle it yourself with atables.yml file in your extension's config folder and then inject that wherever you want to use it.

You will also have to create that table in your migrations file. Something like this

Code: Select all

	/**
	* Add the table schemas to the database:
	*
	* @return array Array of table schema
	* @access public
	*/
	public function update_schema()
	{
		return array(
			'add_tables' => array(
				$this->table_prefix . 'my_table' => array(
					'COLUMNS'	=> array(
						'my_id' => array('UINT', null, 'auto_increment'),
						'field1' => array('VCHAR:40', ''),
						'field2' => array('VCHAR:40', ''),
						'field3' => array('VCHAR:10', ''),
					),
					'PRIMARY_KEY' => 'my_id',
				),
			),
		);
	}

	/**
	* Drop the schemas from the database
	*
	* @return array Array of table schema
	* @access public
	*/
	public function revert_schema()
	{
		return array(
			'drop_tables'	=> array(
				$this->table_prefix . 'my_table',
			),
		);
	}
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored

User avatar
Autumn Shade
Registered User
Posts: 29
Joined: Thu May 30, 2019 12:38 pm

Re: Constants for tables in migrations

Post by Autumn Shade » Wed Jun 19, 2019 8:12 am

Okay, if I understood this correctly, I have to create an yml file where my constants will be, so I will be able to use them. Let's say I do this:

Code: Select all

parameters:
       autumnshade.testext.table.david_table: %core.table_prefix%david_table
And probably I will have to import it into the services, right? Like:

Code: Select all

imports:
    - { resource: tables.yml }

User avatar
david63
Registered User
Posts: 16195
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: Constants for tables in migrations

Post by david63 » Wed Jun 19, 2019 8:18 am

Almost

Code: Select all

parameters:
       autumnshade.testext.tables.david_table: %core.table_prefix%david_table
You need an "s" on tables
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored

User avatar
Autumn Shade
Registered User
Posts: 29
Joined: Thu May 30, 2019 12:38 pm

Re: Constants for tables in migrations

Post by Autumn Shade » Wed Jun 19, 2019 10:25 am

Alright. Here is the full solution for those who've encountered the same problem:
  • Create tables.yml in your_vendor/your_ext/config folder
  • Create the parameters for your tables. This works as it follows:

    Code: Select all

    parameters:
        your_vendor.your_ext.tables.your_table: '%core.table_prefix%your_table'
    
    (You can have as many tables as you want)
  • Import the tables.yml into services.yml as it follows:

    Code: Select all

    imports:
        - { resource: tables.yml }
    
  • Include as argument to your listener/controller as it follows:

    Code: Select all

    services:
        your_vendor.your_ext.controller.main:
            class: your_vendor\your_ext\controller\main_controller
            arguments:
                - '@config'
                - '@controller.helper'
                - '@template'
                - '@language'
                - '@request'
                - '%your_vendor.your_ext.tables.your_table%'
    
  • Go to your controller/listener and create a variable into the class ( Dependency Injection )
  • Get the table constant as it follows:

    Code: Select all

    class MainController {
            /** @var string Custom string for your table */
            protected $your_table;
    	
    	/**
    	 * Constructor
    	 *
    	 * @param \phpbb\config\config		$config		Config object
    	 * @param \phpbb\controller\helper	$helper		Controller helper object
    	 * @param \phpbb\template\template	$template	Template object
    	 * @param \phpbb\language\language	$language	Language object
    	 * @param \phpbb\request\request	$request	Request object
    	 * @param \your_vendor\your_ext\	$your_table  Your table name string
    	 */
    	public function __construct(
    		\phpbb\config\config $config, 
    		\phpbb\controller\helper $helper,
    		\phpbb\template\template $template, 
    		\phpbb\language\language $language,
    		\phpbb\request\request $request,
    		$your_table
    		)
    	{
    		$this->config		= $config;
    		$this->helper		= $helper;
    		$this->template		= $template;
    		$this->language		= $language;
    		$this->request 		= $request;
    		$this->your_table 	= $your_table;
    	}
    	
        /**
    	 * Example handler
    	 *
    	 * @param string $name
    	 *
    	 * @return \Symfony\Component\HttpFoundation\Response A Symfony Response object
    	 */
    	public function handle($name)
    	{
    		$sql = 'SELECT * FROM ' . $this->your_table . ' WHERE id = 2';
    	}
    }
    
And this is how you can use your tables.yml correctly. Thanks to Paul and David.
Last edited by Autumn Shade on Wed Jun 19, 2019 12:25 pm, edited 3 times in total.

User avatar
david63
Registered User
Posts: 16195
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: Constants for tables in migrations

Post by david63 » Wed Jun 19, 2019 10:54 am

Just a couple of points.

1. You should not be usingglobal in a controller - you should inject Container

2. phpBB uses snake_case not CamelCase ie. MainController should be main_controller
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored

Paul
Infrastructure Team Leader
Infrastructure Team Leader
Posts: 25196
Joined: Sat Dec 04, 2004 3:44 pm
Location: The netherlands.
Name: Paul Sohier
Contact:

Re: Constants for tables in migrations

Post by Paul » Wed Jun 19, 2019 10:54 am

You should not globalize the container, but use constructor dependency (Which you already setup kinda with your services.yml)
Knock knock
Race condition
Who's there?

My BlogMy Photosmy phpBB Extensionscustom phpBB work & Development

User avatar
david63
Registered User
Posts: 16195
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: Constants for tables in migrations

Post by david63 » Wed Jun 19, 2019 11:36 am

Just re-reading this - you do not need the container at all as you can inject the table without the need to make it a constant.
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored

User avatar
Autumn Shade
Registered User
Posts: 29
Joined: Thu May 30, 2019 12:38 pm

Re: Constants for tables in migrations

Post by Autumn Shade » Wed Jun 19, 2019 12:12 pm

Welp, it just doesn't seem to allow me to use the constant itself, so I decided to use the container instead. And yes, I agree, it is much better to use the constructor dependency. Will edit it.

Post Reply

Return to “Extension Writers Discussion”