3

I am a WordPress developer / SEO specialist who has been thrown into the Magento framework and I dont really know how to proceed in the following case.

I am trying to track my visitors by assigning them with a unique user id, and a unique voucher id. I'll then use that data for custom reporting in Google Analytics.

Without knowing the first thing about modules, I need to able to write to a custom table that I will create in PhpMyAdmin.

Is there an easy way to do this and to do it all from /templates/page/html/head.phtml?

It would have to be secure against SQL injection as I will be inserting a COOKIE value into the database.

3
  • To create custom table you need to create your module. or that check this kamleshkamble.blogspot.in/2013/02/…
    – Charlie
    Jan 7, 2015 at 10:34
  • In magento you should follow some magento rules to add your own functionality. you can ask me if you have doubt
    – Charlie
    Jan 7, 2015 at 10:36
  • I know Magento can be a pain when you start, but if you understand the basics of MVC, you should understand that accessing database and manipulating data from the view (here head.phtml) isn't ideal. You want to create a basic module do be able to access (and beforehand, create) your table. There are tons of tutorials on how to create a module from scratch. Jan 7, 2015 at 10:46

2 Answers 2

2

Technically, yes you can. In the html file you can add PHP code which inserts a value in the database. From a MVC point of view, please don't. You should not put any logic in the template, that's what controllers and models are for.

Maybe you can explain what you exactly want to do and at which point, so we can suggest in which controller you can add the logic.

1

That's a pretty broad question but I'll try to point you in the right direction.

To create a module with database table you can read this article on Magento Wiki or use the Ultimate Module Creator to set up a module right away.

Next you'll need to add the code that writes to the database. Please don't use a phtml file to write to the database. In this case I'd use an observer like controller_action_predispatch to write to the database and set the cookie.

You might want to read this article about observers. The method would look something like this

class [Namespace]_[Module]_Model_Observer
{
   public function predispatch($observer)
   {
      Mage::getModel('[namespace]_[model]/item')
         ->setData('[column_name]', filter_input(INPUT_COOKIE, '[name cookie]', FILTER_SANITIZE_STRING))
         ->save();

      return $this;
   }
}

Or... find a Magento developer that can build this for you. Shouldn't take more than 3 to 4 hours. The method described above is very basic and might have bad performance impact if a lot of visitors go through a lot of pages.

3
  • Thanks Sander, the writing to the database will happen on the first visit only. Let me rethink this and get back to you later on. Jan 7, 2015 at 10:55
  • 1
    Sure. I would advise you to re-factor your question or post a new one with a specific issue you run into to get more and better response Jan 7, 2015 at 10:59
  • I know it isnt advisable but I havent got the time to explain what I need to other people now nor to do it properly myself. If I only run SQL on a first visit page, and I get 500 new users a day, will it still have a bad performance? Jan 7, 2015 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.