Jump to content

Hook Help: Check "If record exists" in DB


Recommended Posts

Hi all - I'm trying to learn how to create add-on modules & hooks. The documentation is good but I could use some more examples or pointers. I've created a form field within the Admin Domain Details tab that inserts data to mod_ table on save. That part works fine.  But now I need the Admin Domain Details tab to populate those values on load. I'm muddling through Laravel docs too (eek) and can't seem to figure this out.

Table: mod_domain_restriction
Field 1: (unique/int) dr_domainid (the domain_id)
Field 2: (boolean) dr_restrict_domain

The formfield is a radio input and I just need to tell it, essentially, if `mod_domain_restriction`.`dr_domainid` exists and matches $vars['id'] (right?), then:

return ['Restrict Domain' => '<input type="radio" name="restrict_domain" value="0" id="off">
	<label for="off">Off</label>  
	<input type="radio" name="restrict_domain" value="1" id="on" checked>
	<label for="on">On</label>',
];

else

return [
	'Restrict Domain' => '<input type="radio" name="restrict_domain" value="0" id="off" checked>
	<label for="off">Off</label>  
	<input type="radio" name="restrict_domain" value="1" id="on">
	<label for="on">On</label>',
];

I'm not sure how to do that initial query with Laravel. This is what I've pieced together - I know it's way wrong but I'm trying. 🙂 (Note: I'm working with domain_id 1 so I just hard coded it in for testing. I need that to be dynamic too, but one thing at a time!)

 

EDIT:  Solution-> this worked --

use Illuminate\Database\Capsule\Manager as Capsule;

add_hook('AdminClientDomainsTabFields', 1, function(array $params)
	{
		try {
		    $results = Capsule::table('mod_domain_restriction')
		        ->where('dr_domainid', '1')
		        ->get();

		    if ($results == 0) {

--

The hook logs aren't giving me much. Even when I'm able to get the syntax worked out, I don't get any results. So, I'm on the wrong path and would love input?  Once I get this first part figured, I can try to figure out the "if exists - update, otherwise insert". Thanks!!

Date Description Username IP Address
09/07/2020 06:25
Hooks Debug: Hook File Loaded: /home/***/public_html/modules/addons/domain_restriction/hooks.php
admin  
09/07/2020 06:25
Hooks Debug: Could not include file: /home/***/public_html/modules/addons/domain_restriction/hooks.php. Error: syntax error, unexpected 'else' (T_ELSE), expecting ',' or ')'
admin  
Edited by earthgirlllc
Link to comment
Share on other sites

I figured it out

use Illuminate\Database\Capsule\Manager as Capsule;

add_hook('AdminClientDomainsTabFields', 2, function(array $params)
	{
		$domainId = $_GET['id'];
		try {
		    $results = Capsule::table('mod_domain_restriction')
		        ->where('dr_domainid', $domainId)
		        ->where('dr_restrict', 1)
		        ->first();

		    if ($results >= 1) {
		    	// DO THIS		   
			} 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated