Jump to content

Store data in database => fieldtype?


chrizz
 Share

Recommended Posts

hello everyone!

I'm a little bit confused with fieldtypes and input fields.

What I want to do is to store 404 errors in a table in my database. 

I think I need a new fieldtype for this, BUT: does every fieldtype require a inputfield? Is there any example for a minimum fieldtype with only the required methods? I took a look the the events module by Ryan but in my case this is much more than I ever need for my "error logger".

Anny suggestions, help, links and so one are welcome!

thanks a lot!

Chris

Link to comment
Share on other sites

Better approach would be autoload module, that will hook into 404 error, and saves hits into a custom db table (instead of fieldtype). See https://github.com/apeisa/ProcessRedirects/blob/master/ProcessRedirects.module module, since it is pretty close (it checks 404:s for redirects).

Here is the relevant hook: https://github.com/apeisa/ProcessRedirects/blob/master/ProcessRedirects.module#L20

  • Like 3
Link to comment
Share on other sites

I just found this module (just for getting an idea of this module, fieldtype, inputfield stuff)

https://github.com/marcostoll/processwire-search-engine-referrer-tracker/blob/master/SearchEngineReferrerTracker.module

This seems to me as if it is an implementation like you mentioned. 

Perhaps somebody can verify these statements

- I don't need a fieldtype as long as I don't want to add "custom stuff" to a page

- If I use a custom fieldtype I need a custom Inputfield for this or define in the fieldtpye class which inputfields can be used

- A custom DB table has no requirements except it should be installed through a module

So if I don't want to configure my module in the admin I can start like this

ErrorLogger extends WireData implements module
 

and the only necessary methods are

getModuleInfo, init, install, uninstall

+

at least one method which writes data to the database and hooks the 404 event

a lot of stuff but I think that I will now see a light :)

thanks!

Link to comment
Share on other sites

You need a fieldtype, if you have content, that is specific to a page. Field belongs to a template, and page has a template - so the content in fields is always tied into pages.

Since 404 pages aren't actual pages, having field doesn't have much sense. What you need is database (or flat file or anything really) where you just store those results. Then autoload module is your way to find those 404 hits and save that data.

Then if you want to provide admin ui for that data, then you would build process-module.

  • Like 5
Link to comment
Share on other sites

To expound on what Apeisa has said...

There's a very good write-up by Teppo about Fields and Inputfields over at his blog

http://www.flamingruby.com/blog/anatomy-of-fields-in-processwire/

A bit more here:

http://processwire.com/api/fieldtypes/

http://processwire.com/api/fieldtypes/validation-fieldtypes-vs-inputfields/

All Fields have a corresponding table in the PW database named like so.. field_title, field_body, field_myfield, etc. Have a look in phpMyAdmin or something :-)

  • Like 1
Link to comment
Share on other sites

All Fields have a corresponding table in the PW database named like so.. field_title, field_body, field_myfield, etc. Have a look in phpMyAdmin or something :-)

One correction: currently, yes. But there is no hard coded requirement for field to store data in database. It could be Amazon S3, flat file, 3rd party api... anything really.

  • Like 2
Link to comment
Share on other sites

One correction: currently, yes. But there is no hard coded requirement for field to store data in database. It could be Amazon S3, flat file, 3rd party api... anything really.

Yes, that's right. I use a 404 log with file:

	public function init() {
		$this->addHookBefore('ProcessPageView::pageNotFound', $this, 'hookLog404Errors');
	}

	public function hookLog404Errors() {
		// determine the URL that wasn't found
		$url = $_SERVER['REQUEST_URI'];
		// if installed in a subdirectory, make $url relative to the directory ProcessWire is installed in
		if($this->config->urls->root != '/') {
			$url = substr($url, strlen($this->config->urls->root)-1);
		}
		$ua = isset($_SERVER['HTTP_USER_AGENT']) ? substr(trim(strip_tags($_SERVER['HTTP_USER_AGENT'])),0,240) : '';
		$this->log->save('404-errors', "{$url} \t :: \t {$ua}");
	}

The API variable $log was introduced with 2.3.1-dev and is available in 2.4 stable now: http://processwire.com/talk/topic/3768-processwire-231-dev-branch/

  • Like 6
Link to comment
Share on other sites

One correction: currently, yes. But there is no hard coded requirement for field to store data in database. It could be Amazon S3, flat file, 3rd party api... anything really.

What I meant is, if it is a Field created in the PW admin, (e.g. a text, date, email, textarea, etc.) - not via API. Is this still the case? Could I create a Field in the admin, say, called summary but have that store its data in Amazon S3, flat flle, etc?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

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