Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何扩展Silex Doctrine MySQL查询库?_Php_Doctrine_Silex_Extends - Fatal编程技术网

Php 如何扩展Silex Doctrine MySQL查询库?

Php 如何扩展Silex Doctrine MySQL查询库?,php,doctrine,silex,extends,Php,Doctrine,Silex,Extends,正如标题所说,我正在使用Silex和Doctrine。尽管如此,我还是想使用我自己的一些简单类来扩展这个原则。但我不知道在“扩展…”之后放什么。这是我如何扩展学说的一个例子: public function action($action, $table, $where = array()) { if(count($where) === 3) { $operators = array('=', '>', '<', '>=', '<=');

正如标题所说,我正在使用Silex和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;
}
公共函数操作($action,$table,$where=array()){
如果(计数($where)==3){

$operators=array(“=”、“>”、“=”、“您需要创建自定义实体存储库(),您可以看到您必须扩展
Doctrine\ORM\EntityRepository

在自定义实体存储库中,您需要使用以下方法之一创建方法:

  • 方法:findBy、findOneBy等(最简单但有限)
  • (几乎是SQL,但含有类固醇)
  • (漂亮的OOP API)
  • (最差选项)
您需要开始考虑ORM方式,而不是旧的连接字符串来形成我的不安全sql方式;)

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;
}