XF 2.1 Inserting into the DB

Ozzy47

Well-known member
Okay, I have created a table in the DB. It is set as follows.

PHP:
public function installStep1()



{
    $this->schemaManager()->createTable('xf_ozzmodz_spaminator_log ', function(Create $table)
    {
        $table->addColumn('log_id', 'int')->autoIncrement();
        $table->addColumn('password', 'char', 100)->setDefault('');
        $table->addColumn('spamcheck', 'varchar', 10)->setDefault('');
        $table->addColumn('accepts', 'varchar', 10)->setDefault('');
        $table->addColumn('CaptchaInput ', 'varchar', 100)->setDefault('');
        $table->addPrimaryKey('log_id');
    });
}

Now on the registration form, I have the following hidden fields.
1) password: textbox
2) spamcheck: checkbox
3) accepts: checkbox
4) CaptchaInput: textbox
The log_id is there to record the attempt, so I have a running count of how many times the form has been filled in with hidden fields.

Now what I do is if any of those are filled out, give the user an error, or redirect them back to the registration from like the core does if someone fills in a hidden field.

Now what I would like to do is when a user submits the form (even though it is rejected) is insert the values from my hidden fields and the log_id into the table I created.

I have looked into some of the core files but I do not understand it, nor have I been able to figure out how to do this. I could probably hack in some direct DB queries but I would much rather do it correctly. If someone could show me what to do it would be greatly appreciated.
 
You'd want to create an entity that matches your table. Basically all you need is a new class that extends XF\Mvc\Entity with a getStructure() method. You can check existing entities on how to do that.

Once you have your entity class, instantiate a new one in your code with $myEntity = \XF::em()->create ('YourAddon:YourEntity'). You can either set individual values with $myEntity->myProp = 'myValue' or bulkset them $myEntity->bulkSet(['myProp' => 'myValue']). Call $myEntity->save() when you're done setting all values and you're good.

Fwiw, I would add an ip_id field to that table and have an IP relation to record the IP address from which this log was triggered. You can check existing entities that store IP for that, for example Post.
 
Thanks Lucas. :)

Okay, I have read through that and the docs, and I am none the wiser than I was last night.

To simplify things, let's assume this. I have a table in the DB named xf_ozzmodz_spaminator_log. In this table I have two colums, log_id and password_confirm. The log_id is an auto increment and is there to record the attempt, so I have a running count of how many times the form has been filled in with hidden fields.

Now on the registration form I have a textbox field named, password_confirm. When someone clicks on the register button to submit the form I redirect the user to the index page of they filled out that field, and deny the registration. This is done in my pub/controller/registration.php file.

Now what I would like to happen is update the table by incrementing the log_id column and inserting the text entered from the textbox into the password_confirm column.

I would like a step by step process as I have no clue what I am doing.
 
Can you clarify what you don't understand? This is one of the few cases where the dev documents are actually quite clear.

As far as I can tell, all you need to do is create an entity containing a getStructure() function that defines the properties of your SQL table. Simply copy-paste the demo getStructure() method into the body of the demo entity class and edit accordingly.
 
I don't understand any of it, as it does not relate to my code at all (in my mind). That is why I would like a detailed explanation using my data.

I currently have this in a file, OzzModz/Spaminator/Entity/Spaminator.php


PHP:
<?php

namespace OzzModz\Spaminator\Entity;

use XF\Mvc\Entity\Structure;

class FeaturedThread extends \XF\Mvc\Entity\Entity
{
  public static function getStructure(Structure $structure)
  {
      $structure->table = 'xf_ozzmodz_spaminator_log';
      $structure->shortName = 'OzzModz\Spaminator:Spaminator';
      $structure->primaryKey = 'log_id';
      $structure->columns = [
          'log_id' => ['type' => self::UINT,
          'passworrd_comfirm' => ['type' => self::UINT,]
      ];
      $structure->getters = [];
      /*$structure->relations = [
          'Thread' => [
              'entity' => 'XF:Thread',
              'type' => self::TO_ONE,
              'conditions' => 'thread_id',
              'primary' => true
          ],
      ];*/

      return $structure;
  }
}

I do not understand what to do with the $structure->getters to get it compatable with what I am doing, nor do I understand how to use this in the RegisterForm.php file to save upon submit.
 
  1. You do not need any getters, what you have is enough.
  2. Your class name is wrong, it must be the same as the file name.
  3. As explained in my last post, create an instance of your entity in your controller, populate it with your data, and save it. You can look at other controllers on how they save entities if you need examples.
 
Back
Top Bottom