Using table_prefix on controller

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
User avatar
RiccardoB.
Registered User
Posts: 38
Joined: Sat Nov 27, 2010 9:54 am
Location: Italy
Name: Riccardo
Contact:

Using table_prefix on controller

Post by RiccardoB. » Mon Sep 15, 2014 10:10 am

When I developed a MOD for 3.0.x I added custom tables to

Code: Select all

includes/constants.php
with a code like

Code: Select all

define('MY_CUSTOM_TABLE', $table_prefix . 'my_custom_table');
In a new 3.1.x Extension, trying to follow the "keep more inside the extension", I won't edit constants but how could I get the $table_prefix value so I can use it in queries in my controller?

I have to pass a specific class to the controller constructor?

Thanks in advance :)
Freelance Software Developer for Web, Desktop and Mobile.

nicofuma
3.2 Release Manager
3.2 Release Manager
Posts: 546
Joined: Sun Apr 13, 2014 1:47 am
Location: Grenoble - France

Re: Using table_prefix on controller

Post by nicofuma » Mon Sep 15, 2014 1:03 pm

to get the $table_prefix value and any other service you need to use the dependency injection (for the table_prefix it's %core.table_prefix%)
Member of phpBB Development-Team
No Support via PM

User avatar
RiccardoB.
Registered User
Posts: 38
Joined: Sat Nov 27, 2010 9:54 am
Location: Italy
Name: Riccardo
Contact:

Re: Using table_prefix on controller

Post by RiccardoB. » Mon Sep 15, 2014 1:31 pm

nicofuma wrote:to get the $table_prefix value and any other service you need to use the dependency injection (for the table_prefix it's %core.table_prefix%)
Thanks for the help :)

Can you address me where to find all the values available for injection?
Freelance Software Developer for Web, Desktop and Mobile.

nicofuma
3.2 Release Manager
3.2 Release Manager
Posts: 546
Joined: Sun Apr 13, 2014 1:47 am
Location: Grenoble - France

Re: Using table_prefix on controller

Post by nicofuma » Mon Sep 15, 2014 3:25 pm

You can use my debug tools extension and the console command debug:container
Member of phpBB Development-Team
No Support via PM

User avatar
VSE
Extensions Development Coordinator
Extensions Development Coordinator
Posts: 4664
Joined: Sat Jan 17, 2009 9:37 am
Location: Los Angeles, CA
Name: Matt Friedman
Contact:

Re: Using table_prefix on controller

Post by VSE » Mon Sep 15, 2014 4:45 pm

RiccardoB. wrote:When I developed a MOD for 3.0.x I added custom tables to

Code: Select all

includes/constants.php
with a code like

Code: Select all

define('MY_CUSTOM_TABLE', $table_prefix . 'my_custom_table');
In a new 3.1.x Extension, trying to follow the "keep more inside the extension", I won't edit constants but how could I get the $table_prefix value so I can use it in queries in my controller?

I have to pass a specific class to the controller constructor?

Thanks in advance :)
If what you're asking is how do you reference your own custom db tables in your extension, add them to the services.yml (or a separate tables.yml that you import into services.yml) and then use them as any other service.

Code: Select all

parameters:
    tables.my_custom_table: %core.table_prefix%my_custom_table

my.some.service:
        class: my\extension\myclass
        arguments:
            - @dbal.conn
            - % tables.my_custom_table%
And define them in your class constructor

Code: Select all

class myclass
{
  public function __construct(\phpbb\db\driver\driver_interface $db, $my_custom_table)
Dictated but not read.
Official phpBB Extensions My Extensions & MODs
Please do not PM me for support.

User avatar
RiccardoB.
Registered User
Posts: 38
Joined: Sat Nov 27, 2010 9:54 am
Location: Italy
Name: Riccardo
Contact:

Re: Using table_prefix on controller

Post by RiccardoB. » Mon Sep 15, 2014 7:55 pm

VSE wrote:If what you're asking is how do you reference your own custom db tables in your extension, add them to the services.yml (or a separate tables.yml that you import into services.yml) and then use them as any other service.
I thought I need to get $table_prefix inside the container... but yes, this is what I'm looking for, thanks! :)
Freelance Software Developer for Web, Desktop and Mobile.

Post Reply

Return to “Extension Writers Discussion”

Who is online

Users browsing this forum: </Solidjeuh>, Paul and 8 guests