Quantcast
Channel: Active questions tagged symfony4 - Stack Overflow
Viewing all articles
Browse latest Browse all 3925

How to create your own customized DQL just like Symfony's DQL

$
0
0

This is my customized DQL.

DQL.php

class DQL {
    protected $select;
    protected $from;

    public function select($string)
    {
        $this->stmt .= $string;
    }

    public function from($table, $alias)
    {
        $this->stmt .= " FROM " . $table . " AS " . $alias;
    }

    public function getQuery()
    {
        $this->query = $this->conn->prepare($this->stmt);
    }

     public function getResult()
     {
        $this->query->execute();
        return json_encode($this->query->fetchAll());
     }

 }

I call it like this.

Student.php

$em = new DQL();
$em->select('*');
$em->from('student_credentials', 'sc');
$em->getQuery();
$em->getResult();

Putting $em each line is quite a work. So how can I implement the code below. This is how "Symfony" uses it.

$em = new DQL();
$em->select('*')
   ->from('student_credentials', 'sc')
   ->getQuery()
   ->getResult();

I know that this returns Call to a member function from() on null.

How can I make this work or is there any way to use DQL plug ins etc.


Viewing all articles
Browse latest Browse all 3925

Trending Articles