lundi 26 novembre 2018

Is there a problem using a "shared" private property in a class to build fluent/evolving DB query?

I'm trying to write a class that relies on some legacy code to fetch data from database related to a particular table (productcode)

For not repeating myself, I implement a private property $sql that holds the "evolving" query and finally applying getResult() on it (as this snippet shows it)

My question: is there any design principle violation by implementing this pattern? (specially adopting the "shared" $sql property). If the answer is yes, why and what is the proper way to do it?

<?php

class ProductCodeDataSource
{
    use dbmanager_aware_trait;
    private $sql;

    public function findByProductId($productId)
    {
        $this->sql = [];
        $this->sql['fields'] = 'productcode_value';
        $this->sql['tables'] = 'productcode';
        $this->sql['where'][] = sprintf('productcode_product_id = %d', (int) $productId);
        $this->sql['order'][] = 'productcode_default';

        return $this;
    }

    public function findOneByProductId($productId)
    {
        $this->findByProductId($productId);
        $this->sql['limit'] = 1;

        return $this;
    }

    public function getResult()
    {
        // get_all_value is a legacy method that fetches data from mysql DB

        // build_select_ext is also a legacy method that builds sql queries from
        // $sql array structures

        return$this->dbmanager->get_all_value($db->build_select_ext($this->sql));
    }
}

Aucun commentaire:

Enregistrer un commentaire