Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'd create separate (protected) functions, those return a prepared statement that only needs to be executed.</p> <pre><code>/** * @returns PDOStatement */ protected function prepareStatementForCase1(PDO $dbObject,Object $dataToBind){...} /** * @returns PDOStatement */ protected function prepareStatementForCase2(PDO $dbObject,Object $dataToBind){...} </code></pre> <p>Then, I would decide outside, which one has to be called. You can rebuild, maintain and read the code more easily.</p> <p>Example:</p> <pre><code>class Document{ protected $dbObject; public function __construct(PDO $dbObject){ $this-&gt;dbObject=$dbObject; } public function doQuery($paramOne,$paramTwo,...){ $logicalFormulaOne=...; // logical expression here with parameters $logicalFormulaTwo=...; // logical expression here with parameters if($logicalForumlaOne){ $dbStatement=$this-&gt;prepareStatementForCase1($dataToBind); }else if($logicalFormuleTwo){ $dbStatement=$this-&gt;prepareStatementForCase2($dataToBind); } $dbResult=$dbStatement-&gt;execute(); } protected function prepareStatementForCase1(Object $dataToBind){ $dbStatement=$this-&gt;dbObject-&gt;prepare("query string"); $dbStatement-&gt;bindParam(...); return $dbStatement; } } </code></pre> <p>But I would not advice this, when your PDOResult object represents different type of database tuples, or when you return more rows in one of the cases.</p> <p>What I usually do is that I create a class which represents (in your example) a Document. Only one. I can insert, delete, select, modify by its fields, and handle one item. When I need to (for example) fetch more of them, I create a new class, e.g. DocumentList, which handles a collection of documents. This class would give me an array of Document objects when it fetches more of them.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload