How to add the $db dependency in Cron

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
myle47
Registered User
Posts: 6
Joined: Fri Jul 31, 2020 6:22 am

How to add the $db dependency in Cron

Post by myle47 »

Hi phpBB rockstars !

I just started writing extensions, and I need help.
After reading the extension doc, taking exemples from other extensions, I cannot manage to achieve my goal, and this is why I'm sure I'm missing something somewhere...

I try to "just" connect the $db variable (I can see it everywhere on other extensions) to my Cron Task.

my services.yml file looks like this :

Code: Select all

services:
    aaa.bbb.cron.task.sample:
        class: aaa\bbb\cron\task\sample
        arguments:
            - '@config'
            - '@log'
            - '@dbal.conn'
            - '%core.root_path%'
            - '%core.php_ext%'
and I am using it this way :

Code: Select all

	protected $config;

	protected $log;

	protected $db;

	protected $phpbb_root_path;
	protected $php_ext;

	public function __construct(\phpbb\config\config $config, \phpbb\log\log $log, \phpbb\db\driver\driver_interface $db, $phpbb_root_path, $php_ext )
	{
		$this->config = $config;
		$this->log = $log;
		$this->db = $db;
		$this->phpbb_root_path = $phpbb_root_path;
		$this->php_ext = $php_ext;
	}
	
	public function run()
	{
		include($phpbb_root_path . 'includes/functions_user.' . $phpEx);

		$sql = 'SELECT group_id
        FROM ' . GROUPS_TABLE . "
        WHERE group_name = '" . $db->sql_escape($group_name) . "'
            AND group_type = " . GROUP_SPECIAL;
	
	
I have 2 issues :
- The $phpbb_root_path and $php_ext contain nothing. they are both empty...
- the php_error log file gives me :

Code: Select all

Uncaught Error: Call to a member function sql_escape() on null
which I understand, it is because my $db is null.

On the same way, the $log and $config are working
This is why I don't understand what I'm missing for the DB...
I did follow the wiki extension, read about dependency injection.

Can you help me understand ?
Many thanks :)
User avatar
3Di
Former Team Member
Posts: 15883
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: How to add the $db dependency in Cron

Post by 3Di »

not tested

Code: Select all

	public function run()
	{
		include($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext);

		$sql = 'SELECT group_id
        FROM ' . GROUPS_TABLE . "
        WHERE group_name = '" . $this->db->sql_escape($group_name) . "'
            AND group_type = " . GROUP_SPECIAL;
	
Please PM me only to request paid works. Thx.
Want to compensate me for my interest? Donate
My development's activity º PhpStorm's proud user
Extensions, Scripts, MOD porting, Update/Upgrades
Looking for a specific feature or alternative option? We will rock you! 🚀
myle47
Registered User
Posts: 6
Joined: Fri Jul 31, 2020 6:22 am

Re: How to add the $db dependency in Cron

Post by myle47 »

I tried this right after posting this topic (because I can't get over this error ^^) but it doesn't;t work either.
$this->phpbb_root_path is still null (as well as $this->php_ext)
And I have the same issue on sql_escape (because $db is null)
But thanks for the reply :)
User avatar
RMcGirr83
Recognised Extension Developer
Posts: 21330
Joined: Wed Jun 22, 2005 4:33 pm
Location: Your display
Name: Rich McGirr

Re: How to add the $db dependency in Cron

Post by RMcGirr83 »

Where is this variable set?

$group_name as I don't see it defined in your code.
Appreciate the extensions/mods/support then buy me a beerImage
Former Modifications/Extensions Team Member | My extensions | github | All requests for support via PM will be ignored
rxu
Extensions Development Team
Posts: 3364
Joined: Wed Oct 25, 2006 12:46 pm
Location: Siberia, Russian Federation
Name: Ruslan
Contact:

Re: How to add the $db dependency in Cron

Post by rxu »

How are you doing that exactly? If you change dependency injections "on-the-fly" you have to purge the cache or disable/enable the extension for every change.
Also don't forget to use $this pseudo-variable throughout the code.
myle47
Registered User
Posts: 6
Joined: Fri Jul 31, 2020 6:22 am

Re: How to add the $db dependency in Cron

Post by myle47 »

Hello all, thanks for your replies.

for the variable $group_name, it is define just before, but I cut the code in the post to be straightforward. but the line before is :

Code: Select all

$group_name = 'REGISTERED';
How I am doing this :
- I disable the extension
- delete data
- copy the updated files
- enable it
- visit the forum to trigger the cron
- the cron is locked because of the error about $db = null.

I don't purge the cache, so I'll try this one before enable the new code.
rxu
Extensions Development Team
Posts: 3364
Joined: Wed Oct 25, 2006 12:46 pm
Location: Siberia, Russian Federation
Name: Ruslan
Contact:

Re: How to add the $db dependency in Cron

Post by rxu »

Also, there's a chance of effect caused by this bug https://tracker.phpbb.com/browse/PHPBB3-16314
myle47
Registered User
Posts: 6
Joined: Fri Jul 31, 2020 6:22 am

Re: How to add the $db dependency in Cron

Post by myle47 »

I’m not sure. Because not only the $db is null, but the $phpbb_root_parh and extension are also null.

If I had a MySQL error, it would mean that I have something on my variable right ?
User avatar
RMcGirr83
Recognised Extension Developer
Posts: 21330
Joined: Wed Jun 22, 2005 4:33 pm
Location: Your display
Name: Rich McGirr

Re: How to add the $db dependency in Cron

Post by RMcGirr83 »

It would probably be a tremendous help if you posted the complete code instead of line snippets.
Appreciate the extensions/mods/support then buy me a beerImage
Former Modifications/Extensions Team Member | My extensions | github | All requests for support via PM will be ignored
myle47
Registered User
Posts: 6
Joined: Fri Jul 31, 2020 6:22 am

Re: How to add the $db dependency in Cron

Post by myle47 »

Hello, here is the services.yml :

Code: Select all

services:
    thewatch.thewatchcron.cron.task.sample:
        class: thewatch\thewatchcron\cron\task\sample
        arguments:
            - '@config'
            - '@log'
            - '@dbal.conn'
            - '%core.root_path%'
            - '%core.php_ext%'
        calls:
            - [set_name, [thewatch.thewatchcron.cron.task.sample]]
        tags:
            - { name: cron.task }
and here the cron task constructor :

Code: Select all

public function __construct(\phpbb\config\config $config, \phpbb\log\log $log, \phpbb\db\driver\driver_interface $db, $phpbb_root_path, $php_ext )
	{
		$this->config = $config;
		$this->log = $log;
		$this->db = $db;
		$this->phpbb_root_path = $phpbb_root_path;
		$this->php_ext = $php_ext;
	}
and now the "run" function (I just try to add a user in the db) :

Code: Select all

public function run()
	{
	
		include($this->$phpbb_root_path . 'includes/functions_user.' . $this->$phpEx);
		$this->log->add('admin', ANONYMOUS, '', 'LOG_TEST', false, array($this->$phpbb_root_path));
		$username = 'Highway of Life';
	
		$password = 'MyCoMpLeX_PaSsWoRd';
		
		$email_address = 'my_email@domain_name.tld';
	
		$group_id = 4;
		
		$group_name = 'REGISTERED';
		$this->log->add('admin', ANONYMOUS, '', 'LOG_TEST', false, array('test log'));
	

		$sql = 'SELECT group_id
        FROM ' . GROUPS_TABLE . "
        WHERE group_name = '" . $this->$db->sql_escape($group_name) . "'
            AND group_type = " . GROUP_SPECIAL;

		
		$result = $db->sql_query($sql);
	
		$row = $db->sql_fetchrow($result);
		$group_id = $row['group_id'];
		$timezone = '-6';
		$language = 'en';
		$user_type = USER_NORMAL;
		$user_ip = $user->ip;
		$registration_time = time();

		$user_row = array(
			'username'              => $username,
			'user_password'         => phpbb_hash($password),
			'user_email'            => $email_address,
			'group_id'              => (int) $group_id,
			'user_timezone'         => (float) $timezone,
			'user_dst'              => $is_dst,
			'user_lang'             => $language,
			'user_type'             => $user_type,
			'user_actkey'           => $user_actkey,
			'user_ip'               => $user_ip,
			'user_regdate'          => $registration_time
		);

		$user_id = user_add($user_row);

		$this->config->set('thewatchcron_cron_last_run', time(), false);
	}
	
After some other test (using the $this on the $php_root_path is still empty...)

What works :
$log and $config works well (I can log and it write stuff in config)

What doesn't:
$db seems null (the php error I put in a previous post)
$php_ext and $php_root_path are empty

thanks again, it is hard to start writing extension, but it's a good thing the community is great here !
User avatar
RMcGirr83
Recognised Extension Developer
Posts: 21330
Joined: Wed Jun 22, 2005 4:33 pm
Location: Your display
Name: Rich McGirr

Re: How to add the $db dependency in Cron

Post by RMcGirr83 »

Code: Select all

include($this->$phpbb_root_path . 'includes/functions_user.' . $this->$phpEx);
Nope, EG, $this->phpbb_root_path and $this->php_ext (not $this->$phpEx) same with uses of $db eg, $this->$db->sql_escape should be $this->db->sql_escape etc and $user isn't set in services nor __construct so it won't be available either.
Appreciate the extensions/mods/support then buy me a beerImage
Former Modifications/Extensions Team Member | My extensions | github | All requests for support via PM will be ignored
User avatar
david63
Registered User
Posts: 18104
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Contact:

Re: How to add the $db dependency in Cron

Post by david63 »

And

Code: Select all

$result = $db->sql_query($sql);
	
		$row = $db->sql_fetchrow($result);
should be

Code: Select all

$result = $this->db->sql_query($sql);
	
		$row = $this->db->sql_fetchrow($result);
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
myle47
Registered User
Posts: 6
Joined: Fri Jul 31, 2020 6:22 am

Re: How to add the $db dependency in Cron

Post by myle47 »

Guys, many thanks ! I can't believe I let a "simple" error like that block me so much time !
Anyway many thanks for your time !

Have a great day !
Post Reply

Return to “Extension Writers Discussion”