Source for file PMO_MyRequest.php
Documentation is available at PMO_MyRequest.php
* This file contains PMO_MyRequest class which is used to build a SQL query
* using provided fields, tables names, where clause, etc.
* This file is part of the PhpMyObject project.
* For questions, help, comments, discussion, etc., please join our
* forum at {@link http://www.developpez.net/forums/forumdisplay.php?f=770}
* or our mailing list at {@link http://groups.google.com/group/pmo-dev}.
* PhpMyObject is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* @author Nicolas Boiteux <nicolas_boiteux@yahoo.fr>
* @link http://pmo.developpez.com/
* @since PhpMyObject v0.14
* @copyright Copyright (C) 2007-2008 Nicolas Boiteux
* @license GPLv3 {@link http://www.gnu.org/licenses/gpl}
/** requires the interface */
require_once(dirname(__FILE__ ). '/PMO_Request.php');
* This class is used to provide a SQL query to PMO_MyController
* You provide the different parts of the query by using the
* corresponding methods. Each method returns the PMO_MyRequest object
* which allows you to link the methods together, e.g.
* $request = new PMO_MyRequest;
* $request->field('film_id')->from('film_actor')->limit('10')
* $controller = new PMO_MyController;
* $map = $cntroller->objectquery($request)
* This could also be written like this:
* $controller = new PMO_MyController;
* $request = new PMO_MyRequest;
* $controller->objectquery($request->field('film_id')->from('film_actor')->limit('10'));
* Most methods accept a variable number of arguments. So you can do something like this:
* $request = new PMO_MyRequest;
* $request->field('film_id','actor_id')->from('film_actor')->limit('10')
* if none is provided, a SELECT * will be performed.
* the table names of the query
* the where clause of the query
* stores the provided column names from which data will be read.
* This method accepts a variable number of arguments.
* $request = new PMO_MyRequest;
* $request->field('column1', 'column2')->from('mytable');
* If you do not provide column names, a "SELECT *" will be performed.
* @param string $field,... any number of column names you want to read
public function field($field){
$this->field->merge($arg);
* stores the table names on which the request is going to be run
* This method accepts a variable number of arguments.
* $request = new PMO_MyRequest;
* $request->field('column1', 'column2')->from('mytable1', 'mytable2');
* @param string $from,... any number of table names you want to read from
public function from($from){
$this->from->merge($arg);
* stores the where clause
* This method accepts a variable number of arguments.
* You create the where clause by providing conditions.
* $request = new PMO_MyRequest;
* $request->from('MyTable')->where('name = "value"', 'active = 1');
* {@link toString()} will create your where clause by linking them with
* @param string $where,... any number of conditions to build your
public function where($where){
$this->where->merge($arg);
* store the order clause using the provided $order parameter
* This method accepts a variable number of arguments.
* You create the order clause by providing the field names.
* $request = new PMO_MyRequest;
* $request->from('MyTable')->where('active = 1')->order('lastname','firstname');
* @param string $order,... the order field name
public function order($order){
$this->order->merge($arg);
* stores the having clause
* @param string $having,... the having clause
public function having($having){
* stores the groupby clause
* @param string $groupby,... the groupby clause
* stores the limit clause
* @param string|integer$limit the limit you want to impose to your query
public function limit($limit){
* Build the actual SQL query using all the previoulsy
* You should not need to use this directly. Since the
* {@link PMO_MyController::objectquery()} takes a PMO_MyRequest object
* and use this method to call {@see PMO_MyController::query()}
* @return string the SQL query
if($this->field->count() > 0){
$query = "SELECT " . $this->field->implode(',');
if($this->from->count() > 0){
$query .= " FROM " . $this->from->implode(',');
throw new Exception("Error: FROM clause is empty");
if($this->where->count() > 0){
$query .= " WHERE " . $this->where->implode(' AND ');
$query .= " GROUP BY " . $this->groupby;
if($this->having->count() > 0){
$query .= " HAVING " . $this->having->implode(' AND ');
$query .= " LIMIT " . $this->limit;
* resets the PMO_MyRequest object so that it can reused
foreach($this->from as $tablename){
foreach($fk as $column=> $value){
foreach($value['Fk'] as $table2=> $column2){
$this->where($table->getTableName(). ".". $column. " = ". $table2. ".". $column2);
|