All my classes that connect to a database need to get values of custom columns from their respective tables. So instead of coding a function for each class, is there a way for me to implement a base class from which my classes extend and I can use that base class function to easily get and update data on my database (at least for simple data).
class Users extend BaseClass
{
private $table = "users";
private $columns = ["name", "email", "password"];
}
so from an outside function, I can access the email value like this
Users->where("name", "John")->getEmail();
or possibly
Users->where("name", "John")->get("email");
I could also use this method to update data to the database. The functions where should be universal so it should exist in BaseClass. (I know the database queries that I should use, what I want to know is how to call get after calling where and also possibly setting multiple where requirements).
Users->where("name", "John")->where("last_name", "Smith")->get("email");
I think you want something like this
abstract class BaseClass
{
private $where_clauses=[];
private $columns=[];
private $table='';
protected function setData($table,$cols){
$this->columns=$cols;
$this->table=$table;
}
public function where($key, $value){
$this->where_clauses[$key]=$value;
return $this;
}
public function get($col){
$sql='SELECT '.$col.' FROM '.$this->table.' WHERE';
$first=true;
foreach($this->where_clauses AS $key=>$val){
if(!$first) sql.=' AND ';
$first=false;
$sql.=$key.' = '.$val;
}
// RUN QUERY, Return result
}
}
Note that the where function returns a reference to $this, which is what let's you string the function calls together (not tested the code). This would also need some adapting to let you put two conditions on the same column.
Related
On my site at the beginning of every script I include a "bootstrap" script which queries a few things from the database, does some calculations and then loads the variables into constants that I define one by one.
Some examples are:
define("SITE_ID", $site_id); // $site_id is pulled from a field in the database
define("SITE_NAME", $site_name);
// pulled from a field in the same row as the above
define("STOCK_IDS", $stock_ids);
//computed array of stock id integers from a different query.
//I perform logic on the array after the query before putting it in the definition
define("ANALYTICS_ENABLED", false);
// this is something I define myself and isnt "pulled" from a database
Now, I have many functions on the site. One example function is get_stock_info. And it references the STOCK_IDS constant.
What I want to do is have a class which has the above constants in it and the get_stock_info function.
Would the best approach to be have an empty class "site", create an instance of it and then afterwards define the static variables above one by one? Or is that not a good way and should I move all of of my logic which pulls from the database and calculates SITE_ID, STOCK_IDS, ANALYTICS_ENABLED etc into the constructor instead?
Ultimately I want the class to contain all of the above info and then I would be able to use class methods such as site::get_stock_info() and those methods will have access to the constants via self:: or this.
There's a lot more I want to do than that but that would give me enough to figure the rest out.
I think this approach isn't the best. You should consider not using constants as your values aren't constant. For your case it is better to have a class with classic getters methods.
Something like this:
class SiteInfo
{
private $siteId;
private $siteName;
private $stockIds;
private $analyticsEnabled;
public function __construct()
{
// Results from the database
$results = $query->execute();
$this->siteId = $results['siteId'];
$this->siteName = $results['siteName'];
$this->stockIds = $results['stockIds'];
$this->analyticsEnabled = $results['analyticsEnabled'];
}
public function getSiteId()
{
return $this->siteId;
}
public function getSiteName()
{
return $this->siteName;
}
public function getStockIds()
{
return $this->stockIds;
}
public function isAnalyticsEnabled()
{
return $this->analyticsEnabled;
}
}
I'm having issues accessing the Jobinfo class from my Deliveries. The problem is I need to be able to get the value of getQty from my child class and I also need to be able to get the qty_ship method using a property from the parent. How can I do this? It doesn't seem to work and quite confused over this... I'd like to be able to use methods from Parent->Child and Child->Parent dynamically.
class jobInfo
{
public $JOB_ID;
private $deliveries; // DELIVERIES CLASS
function __construct($job_id)
{
$this->JOB_ID=$job_id;
$this->deliveries = new Deliveries();
}
public function getQty()
{
return $this->query_s('job_sheet','*', 'job_id',$this->JOB_ID, 1, 'qty');
//returns a quantity from query method
}
}
class Deliveries extends jobInfo
{
function __construct(){}
public function qty_ship()
{
$qty = 0;
$SQL = mysql_query("SELECT * FROM deliveries WHERE jID='".parent::JOB_ID."'") or die(mysql_error());
$rows = mysql_num_rows($SQL);
if($rows>0)
{
while($data = mysql_fetch_array($SQL))
{
$qty += $data['qty_shipped'];
}
}
return $qty;
}
public function getTotalBO()
{
$qty = parent::getQty();
$totalship = $this->qty_ship();
$x = $qty-$totalship;
return $x;
}
}
$A = new Jobinfo(15);
You want $this->getQty() and $this->JOB_ID, but for completeness, consider:
Removing your empty no-parm constructor, as it cannot actually be used to instantiate the class unless it calls the parent contructor with a job id, which it cannot do since you expect the job id to be defined externally.
Making JOB_ID protected. For better encapsulation you might instead make it private and provide a getJobId() method.
Working on consistent class naming - jobInfo starts with lowercase and Deliveries starts with uppercase.
Working on consistent function naming - You have underscore-seperated functions mixed with camel-case functions.
Working on consistent spacing - You mix 1-character, 2-character and 0-character spacings throughout without much rhyme or reason.
Welcome to OOP and Good Luck!
If you extend from that class, then you should be using
$this->getQty();
$this will return the current object wich already include the parent with all the public and protected variables and methods.
You should only use :: when you call a static class
for exemple :
jobInfo::getQty();
You might also want to take a look at the naming convention.
http://framework.zend.com/manual/1.12/en/coding-standard.naming-conventions.html
I want to build a paginator class, but i'm not sure what the best method is. I see many different codes on the internet that confuses me.
My data is stored in a MySQL database. Do i have to give a query to my pagniator class so that it can retrieve the data out of MySQL for me (and of course it will automatically do some pagination calculations after that)? Or do i get all the data from a model first, then supply the returned array from that model to the paginator class?
In either way i'd probably have to do another query to get the "total" amount of records and pass that result to the pagninator class as a separate param.
Once i know how to get started then i know how to pick up the rest. I'm just not sure how to pass in the data to the paginator class and what kind of data.
Any idea how to build a good paginator class?
A well designed OO Paginator class should be independent of your data and your database.
It should take parameters like count, page, and per_page, and should return objects that can be used for Dependency Injection into a Model for generating queries with the appropriate LIMIT, or into a PaginationHelper class for rendering the appropriate HTML.
Example of what might be a good Paginator interface (given 10 minutes of thought in this):
/**
* Your pagination master class
*/
interface iPaginator {
public function __construct($total_count, $count_per_page, $current_page=1);
public function getPaginationLimiter();
public function getPaginationHelper();
// These return iPaginatorPage objects
public function getCurrentPage();
public function getNextPage();
public function getPreviousPage();
public function getTotalCount();
public function getCountPerPage();
public function getPageCount(); // Calculated
}
/**
* Page representation
*/
interface iPaginatorPage {
public function __construct($page_number, $start, $end);
public function getNumber();
public function getStart();
public function getEnd();
public function getCount(); // Calculated
}
/**
* Helper for rendering the UI
*/
interface iPaginationHelper {
public function __construct(iPaginator $paginator);
public function render();
}
Example of how you could integrate into your model, by extending your base model, and then having your application models extend PaginatorModel instead of Model:
class PaginationModel extends Model {
public function query($sql, iPaginatorPage $page = null) {
if (!empty($page)) {
$start = $page->getStart();
$length = $page->getCount();
$sql .= " LIMIT ({$start}, {$length})";
return parent::query($sql);
}
}
}
Check the pager implementation from Pear. It's a very nice class, simple to use and with a lots of cool features.
create class, and have function like this:
function resultset($required_filters, $table, $pagenum, $records){
$getCount='SELECT count(*) from table';
$sql="SELECT ".$required fields." FROM ".$table." LIMIT ".(($pagenum-1) * $records).",".$records;
$sqlres=...
return array($getCount, $sqlres); }
call this function as list($rescount, $arrData)=resultset(all
parameters);
and for pagination use some for loop to display page numbers...for total $rescount pages,
using get you can show current pagenum highlighted
Is it possible to override values from Model->fetchAll() so it work globally. I have tried to override this in model, but does not work:
class Application_Model_DbTable_OdbcPush extends Zend_Db_Table_Abstract
{
public function __get(string $col)
{
$res = parent::__get($col);
if ($col == "lastrun") {
$res = ($res == "1912-12-12 00:00:00+07" ? NULL : $res);
}
return $res;
}
//...
}
In a controller:
$odbcModel = new Application_Model_DbTable_OdbcPush();
$rs = $odbcModel->fetchAll( $select );
I want to override value returned from fetchAll(), find() etc when col name is "lastrun";
The way you're going about this isn't going to work. __get is used to get data from protected or private properties and typically used in conjunction with getters.
For example, if you implemented __get() in your Application_Model_DbTable_OdbcPush class you could do something like:
$model = new Application_Model_DbTable_OdbcPush();
//echo out the _primary property (primary key of the table)
echo $model->primary;
and expect it to work. Because _primary exists as a property in Zend_Db_Table_Abstract.
To do what you want to do you'll need to do it after the result set has been returned (unless you want to rewrite the whole Zend Db component). Just run the result set through a foreach and change the value of lastrun to whatever you want.
I tried to find a place to override the Zend Db components to do what you want, but it would involve to many classes.
Remember that when using DbTable classes, they only interact with one table. You'll need to duplicate code for every table you want to effect or you'll need to extend a base class of some kind.
You always have the option to use straight Sql to frame whatever query you can come up with.
Good Luck!
Found the answer, for community i share here :D
http://framework.zend.com/manual/1.12/en/zend.db.table.row.html
So we have to overload Zend_Db_Table_Row and assign it to model/dbtable:
class Application_Model_DbTable_Row_OdbcPush extends Zend_Db_Table_Row_Abstract
{
// do some override here
}
class Application_Model_DbTable_OdbcPush extends Zend_Db_Table_Abstract
{
protected $_name = 'odbcpush';
protected $_primary = 'id';
private $_global = null;
protected $_rowClass = "Application_Model_DbTable_Row_OdbcPush";
// etc
}
So basically I'm making a leap from procedural coding to OOP.
I'm trying to implement the principles of OOP but I have a nagging feeling I'm actually just writing procedural style with Objects.
So say I have a list of pipes/chairs/printers/whatever, they are all all listed as products in my single table database. I need to build a webapp that displays the whole list and items depending on their type, emphasis is on 'correct' use of OOP and its paradigm.
Is there anything wrong about just doing it like:
CLass Show
{
public function showALL(){
$prep = "SELECT * FROM myProducts";
$q = $this->db-> prepare($prep);
$q->execute();
while ($row = $q->fetch())
{
echo "bla bla bla some arranged display".$row['something']
}
}
and then simply
$sth = new show();
$sth->showAll();
I would also implement more specific display methods like:
showSpecificProduct($id)->($id would be passed trough $_GET when user say clicks on one of the links and we would have seperate product.php file that would basically just contain
include('show.class.php');
$sth = new show();
$sth->showSpecificProduct($id);
showSpecificProduct() would be doing both select query and outputing html for display.
So to cut it short, am I going about it allright or I'm just doing procedural coding with classes and objects. Also any ideas/hints etc. on resolving it if I'm doing it wrong?
As well as the model practices described by #Phil and #Drew, I would urge you to separate your business, data and view layers.
I've included a very simple version which will need to be expanded upon in your implementation, but the idea is to keep your Db selects separate from your output and almost "joining" the two together in the controller.
class ProductController
{
public $view;
public function __construct() {
$this->view = new View;
}
public function indexAction() {
$model = new DbProductRepository;
$products = $model->fetchAll();
$this->view->products = $products;
$this->view->render('index', 'product');
}
}
class View
{
protected $_variables = array();
public function __get($name) {
return isset($this->_variables['get']) ? $this->_variables['get'] : null;
}
public function __set($name, $value) {
$this->_variables[$name] = $value;
}
public function render($action, $controller) {
require_once '/path/to/views/' . $controller . '/' . $action . '.php';
}
}
// in /path/to/views/product/index.php
foreach ($this->products as $product) {
echo "Product ID {$product['id']} - {$product['name']} - {$product['cost']}<br />\n";
}
A better fit would be to implement a repository pattern. An example interface might be
interface ProductRepository
{
public function find($id);
public function fetchAll();
}
You would then create a concrete implementation of this interface
class DbProductRepository implements ProductRepsoitory
{
private $db;
public function __construct(PDO $db)
{
$this->db = $db;
}
public function find($id)
{
// prepare execute SQL statement
// Fetch result
// return result
}
public function fetchAll()
{
// etc
}
}
It's generally a bad idea to echo directly from a method or function. Have your methods return the appropriate objects / arrays / whatever and consume those results.
The scenario you are describing above seems like a good candidate for MVC.
In your case, I would create a class strictly for accessing the data (doing selects of product categories or specific products) and then have a different file (your view) take the output and display it.
It could look something like this:
class Product_Model {
public function find($prodId) { ... }
public function fetchAll($category = '') { ... }
public function search($string) { ... }
}
Then somewhere else you can do:
$products = new Product_Model();
$list = $products->fetchAll(37); // get all from category 37
// in true MVC, you would have a view that you would assign the list to
// $view->list = $list;
foreach($ilst as $product) {
echo "Product ID {$product['id']} - {$product['name']} - {$product['cost']}<br />\n";
}
The basic principle of MVC is that you have model classes that are simply objects representing data from some data source (e.g. database). You might have a mapper that maps data from the database to and from your data objects. The controller would then fetch the data from your model classes, and send the information to the view, where the actual presentation is handled. Having view logic (html/javascript) in controllers is not desirable, and interacting directly with your data from the controller is the same.
first, you will want to look into class autoloading. This way you do not have to include each class you use, you just use it and the autoloader will find the right file to include for you.
http://php.net/manual/en/language.oop5.autoload.php
each class should have a single responsibility. you wouldn't have a single class that connects to the database, and changes some user data. instead you would have a database class that you would pass into the user class, and the user class would use the database class to access the database. each function should also have a single responsibility. you should never have an urge to put an "and" in a function name.
You wouldn't want one object to be aware of the properties of another object. this would cause making changes in one class to force you to make changes in another and it eventually gets difficult to make changes. properties should be for internal use by the object.
before you start writing a class, you should first think about how you would want to be able to use it (see test driven development). How would you want the code to look while using it?
$user = new User($db_object);
$user->load($id);
$user->setName($new_name);
$user->save();
Now that you know how you want to be able to use it, it's much easier to code it the right way.
research agile principles when you get a chance.
One rule of thumb is that class names should usually be nouns, because OOP is about having software objects that correspond to real conceptual objects. Class member functions are usually the verbs, that is, the actions you can do with an object.
In your example, show is a strange class name. A more typical way to do it would be to have a class called something like ProductViewer with a member function called show() or list(). Also, you could use subclasses as a way to get specialized capabilities such as custom views for particular product types.