2
votes

As the title says, I'm using Silex and Doctrine. Though, I'd like to use some of my own simple classes to extend Doctrine. But I don't know what to put after 'extends ....?'. This is an example of how I would extend doctrine:

public function action($action, $table, $where = array()) {
    if(count($where) === 3) {
        $operators = array('=', '>', '<', '>=', '<=');

        $field = $where[0];
        $operator = $where[1];
        $value = $where[2];

        if(in_array($operator, $operators)) {
            $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";

            if(!$this->query($sql, array($value))->error()) {
                return $this;
            }
        }

    }

    return false;
}

And this one:

public function insert($table, $fields = array()) {
    $keys = array_keys($fields);
    $values = null;
    $x = 1;

    foreach($fields as $field) {
        $values .= '?';
        if ($x < count($fields)) {
            $values .= ', ';
        }
        $x++;
    }

    $sql = "INSERT INTO {$table} (`" . implode('`, `', $keys) . "`) VALUES ({$values})";

    if(!$this->query($sql, $fields)->error()) {
        return true;
    }

    return false;
}

So, to create an external class that extends Doctrine. What would my class look like:

class DB extends ... {

//blablabla

}

1

1 Answers

1
votes

You need to create your custom entity repositories (Symfony example is crystal clear on this) Your can see you must extend Doctrine\ORM\EntityRepository

Inside your custom entity repository you need to create your methods by using one of the following methods:

You need to start thinking the ORM way and not the old concatenate-strings-to-form-my-insecure-sql way ;)