eymengunay / EoHoneypotBundle

Honeypot type for Symfony forms

Home Page:https://github.com/eymengunay/EoHoneypotBundle

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Alternate ORM configuration

iisisrael opened this issue · comments

Using the class HoneypotPrey extends BaseHoneypotPrey configuration as shown in the example, I was getting this error when running app/console doctrine:schema:update --dump-sql:

[Doctrine\ORM\Mapping\MappingException]
Duplicate definition of column 'id' on entity 'ExampleBundle\Entity\HoneypotPrey' in a field or discriminator column mapping.

I then tried extending Eo\HoneypotBundle\Model\HoneypotPrey instead of Eo\HoneypotBundle\Entity\HoneypotPrey, and that worked as far as the schema update was concerned. I had two tables - HoneypotPrey with just the id column, and honeypot_prey with the columns id, ip, and createdAt. However, after submitting some test spam registrations with the honeypot field populated with data, I was only getting new rows in the HoneypotPrey table (and lines appended to honeypot.log), but nothing in the honeypot_prey table.

So, instead, I mapped the schema like so (sorry, not a fan of annotations)...

ExampleBundle\Entity\HoneypotPrey:
    type: entity
    table: HoneypotPrey
    id:
        id:
            type:   integer
            generator:
                strategy: AUTO
    fields:
        createdAt:
            type:     datetime
        ip:
            type:     string

... generated the entities, copied the __construct() method from Eo\HoneypotBundle\Model\HoneypotPrey into my Entity class, modified my Entity class like so...

namespace ExampleBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Eo\HoneypotBundle\Model\HoneypotPreyInterface;

/**
 * HoneypotPrey
 */
class HoneypotPrey implements HoneypotPreyInterface
{
    ...

... dropped the honeypot_prey table, and reran the schema update command. I'm now getting records in my HoneypotPrey table...

mysql> select * from HoneypotPrey;
+----+---------------------+-------------+
| id | createdAt           | ip          |
+----+---------------------+-------------+
|  4 | 2015-01-14 00:05:56 | 172.16.61.1 |
+----+---------------------+-------------+

Configs are set like so:

# Honeypot
eo_honeypot:
    storage:
        # Record for reporting
        database:
            enabled: true
            driver:  orm
            class:   ExampleBundle:HoneypotPrey
        # Log for IP banning using fail2ban
        file:
            enabled: true
            output:  %kernel.root_dir%/logs/honeypot.log

Thanks for this. I am not a fan of annotations either. I didn't encounter the error because I built the table to match my app as far as naming conventions go.

A work around is very simple. You do not have to use the provided model at all. In my case I just created my table in the ORM and generated the entity like I normally would with my own fields. Just implement Eo\HoneypotBundle\Model\HoneypotPreyInterface in your entity to ensure you have getIp() and setIp(). You can even call the ip column something else as long as you provide the methods required by the interface. Much better for me because my table name in the db would be t_honeypot_prey and primary key honeypot_pre_id. Use your constructor to set the timestamp. On mine I use creationDate to match the rest of my tables.

Just added this constructor to my class:

public function __construct($ip = null)
{
    $this->ip = $ip;
    $this->creationDate = new \DateTime();
}

Hope this helps someone who may come across this issue!