I am trying to build a custom ActiveRecord class and am trying to return MySQL results in an object. The object will be the model.
Here's what I'm doing:
while($object = mysql_fetch_object($result, $this->_model)) {
foreach($object as $key => $val) {
if($this->is_serialized($val)) $object->$key = unserialize($val);
}
$return_value[] = $object;
}
If I do a print_r() on the result, I get an empty class (no variables have been set or anything).
Here is my model:
class User extends ActiveRecord {
public $_hasOne = "Profile";
public $_required = array('full_name', 'user_name', 'password', 'country');
public $_unique = array('full_name', 'user_name');
}
I can't figure out what I'm doing wrong!
EDIT: Fixed it in the end. At the beginning of my Activerecord class I had this:
$fields = $this->query("SHOW COLUMNS FROM " . $this->_table);
while($field = mysql_fetch_assoc($fields)) {
$this->_columns[] = $field['Field'];
$this->$field['Field'] = null;
}
Which was setting all the values to NULL!
Bit of a silly mistake. It's now:
$fields = $this->query("SHOW COLUMNS FROM " . $this->_table);
while($field = mysql_fetch_assoc($fields)) {
$this->_columns[] = $field['Field'];
if(!isset($this->$field['Field'])) $this->$field['Field'] = null;
}
Stupid question, but $this->_model is "User" right (I'd echo that value, it might have gotten unset somehow).
After that, your class should have fields by the same names as the columns which you want to represent. So:
class Foo
{
private $id;
public $val;
public function __toString(){ return "$this->id => $this->val"; }
}
mysql_connect('localhost','root','');
$q = mysql_query('SELECT 1 as id, 2 as val FROM DUAL');
$foo = mysql_fetch_object( $q, 'Foo' );
echo $foo; // 1 => 2
Next, you can't iterate over an object. You need to iterate over object properties:
foreach(get_object_vars($object) as $key => $val) {
if($this->is_serialized($val)) $object->$key = unserialize($val);
}
Related
This is my first question here and I will try to clarify it as much as possible.
I am a begginer and I am going through lynda - beyond PHP MySQL lessons when I got to this part.
Code is working just fine, I just need better explanation for myself to the line that is commented in a code.
require_once('database.php');
class User {
public $id;
public $username;
public $password;
public $first_name;
public $last_name;
public static function find_all() {
return self::find_by_sql("SELECT * FROM users");
}
/////
public static function find_by_sql($sql="") {
global $database;
$result_set = $database->query($sql);
$user_array = array();
while ($row = $database->fetch_array($result_set)) {
$user_array[] = self::instantiate($row);
}
return $user_array;
}
And finaly lines that i almost understand :)
private static function instantiate($row) {
$user = new self;
foreach($row as $attribute=>$value){
if($user->has_attribute($attribute)) {
$user->$attribute = $value; /// THIS LINE BUGS ME
}
}
return $user;
}
private function has_attribute($attribute) {
$user_vars = get_object_vars($this);
return array_key_exists($attribute, $user_vars);
}
}
So I think I don't understand array_key_exists which returns TRUE or FALSE, in my case its true, but then line $users->$attributes =$value ; makes no sense for me,
So, I check if keys from fetch array MATCH variable names from object,
if($user->has_attribute($attribute)) { //and then this is true,perform nxt line
$user->$attribute = $value; // i got match of attribute above,how does it put values in $user_vars???
I know it says something like " if user has same attribute as key from that fetch array then put into that same attribute value of that attribute $value but i just dont see how it is done when i never returned object variables
Thank you for your time !
edit:
class variables names are equal to names of column_names from database
The User class has public attributes $id, $username, $password, etc. That means that you can assign values to the attributes in the following form:
$u = new User;
$u->id = 123;
$u->username = 'username';
and using dynamic property names:
$prop = 'id';
$u->$prop = 123;
$prop = 'username';
$u->$prop = 'username';
This is just the thing that happens in the lines that you don't understand:
if ($user->has_attribute($attribute)) {
$user->$attribute = $value;
The has_attribute method fetches all attributes of the $user object with get_object_vars function. The latter fetches the object properties as an array:
$user_vars = get_object_vars($this);
/* i.e.
$user_vars = array (
'id' => ...,
'username' => ...,
...
);
*/
Then the has_attribute method checks if the given $attribute key exists in the array of properties:
return array_key_exists($attribute, $user_vars);
If it exists (true), the $attribute property is assigned to $value.
I'm trying to use OOP ways. I have bunch of methods that return same format of array. I want to guarantee that user of this class knows what will be returned. How would I go about doing that? Please forgive me as I'm not sure of correct terminologies to describe my problem.
class myModel {
public function getArray1(){
$data = array();
$id = array();
....
return array('data'=>$data, 'id'=>$id); <== HOW TO GUARANTEE RETURN FORMAT
};
public function getArray2(){
$data = array();
$id = array();
....
return array('data'=>$data, 'id'=>$id); <== HOW TO GUARANTEE RETURN FORMAT
};
public function getArray3(){
$data = array();
$id = array();
....
return array('data'=>$data, 'id'=>$id); <== HOW TO GUARANTEE RETURN FORMAT
};
}
You can try to define additional class for returning value:
class returnArray {
$data = array();
$id = array();
function construct($data,$id) {
$this->data = $data;
$this->id = $id;
}
}
//...
public function getArray1(){
$data = array();
$id = array();
....
return new returnArray('data'=>$data, 'id'=>$id);
};
Then You can add accessors to the returnArray class or leave it as public if You want or even implement __toString() if necessary.
In C#, given this class
public class MyClass {
public int Id { get; set; }
public int Name { get; set; }
}
I can do this to instantiate it:
var instance = new MyClass(){
Id = 34,
Name = "Frank"
};
which is a lot nicer than doing this:
var instance = new MyClass();
instance.Id = 34;
instance.Name = "Frank";
Can I do this in PHP, or is my only option this:
$instance = new MyClass();
$instance->Id = 34;
$instance->Name = "Frank";
There isn't a way to set all the fields at once in PHP like in C#. The closest you can get is as follows:
class MyClass {
function __construct(array $data = array()) {
foreach($data as $key => $value) {
$this->$key = $value;
}
}
}
$instance = new MyClass(array(
'Id' => 34,
'Name' => 'Peter',
));
You should modify it a bit so that private fields aren't accessible (perhaps a naming convention that you have all private fields start with a underscore and within the foreach check that $key doesn't start with that).
I am very like to use PDO, so it might be on.
Usage could be as:
$m = new MDB();
$m->Users()->GetRec($param);
Users() is a name of table in the database, GetRec($param) is my function.
A way I go looking like this:
class MDB extends DB {
function __construct(){
parent::__construct();
if ($result = $this->pdo->query("SHOW TABLES"))
{
while ($row = $result->fetch(PDO::FETCH_NUM))
{
// this is only my imagination (not working at all)
__set($row[0],true);
}
}
}
// set
public function __set($name, $value)
{
// here could be a method (not properties)
$this->$name = $value;
}
Sure it's all seems not exactly what I want. So I am able to get some suggestions and advices in this question.
upd 1.
Thanks for the magic method __call and now I'm trying to make it within. Watch my updated code:
class MDB extends DB {
function __construct(){
parent::__construct();
}
public function __call( $method, $param )
{
$tables = array();
if ($result = $this->pdo->query("SHOW TABLES"))
{
while ($row = $result->fetch(PDO::FETCH_NUM))
{
$tables[] = $row[0];
}
}
if (in_array($method,$tables))
{
return $this;
}
else
{
return FALSE;
}
}
Well, seems it works for me!
Yes it's possible! The wrong row in your code is:
__set($row[0],true);
There you should call:
$this->$row[0] = true;
Take a look a the simple example below and at the overloading documentation of PHP http://www.php.net/manual/en/language.oop5.overloading.php.
class A{
//The array which will contain your data
private $tables;
function __construct($array){
$counter = 1;
foreach($array as $value){
//That's the right call!
$this->$value = $counter++;
}
}
function __set($name, $value){
$this->tables[$name] = $value;
}
function __get($name){
if (array_key_exists($name, $this->tables)) {
return $this->tables[$name];
}
}
}
$array = array('a1', 'a2', 'a3', 'a4', 'a5');
$a = new A($array);
foreach($array as $key){
echo $a->$key;
}
Hope this help!
I'm trying to dynamically add class properties into an PDO prepared statement. To achieve this I need to create an array that will grab the properties from the class, put them in an array, and add a : to the beginning of each key. Then, separate each key with a comma. The closest I've gotten to achieve this is using:
foreach ($property as $field)
$propertyValues = implode(", :", array_keys($property));
return $propertyValues;
}
which gives me
username, :password
I just need a way to add a : to the first key which in this case is username. So it would look like
:username, :password
Keep in mind, however, that I'm trying to make this dynamic so that I can extend its functionality to other classes, and I'm not always going to know what the first array key is.
If your interested in reading the entire class, here it is:
<?php
require_once("../config/main.php");
class Database{
protected static $dbFields = array('username', 'password');
public $dbConnection;
public $tableName = 'users';
public $id;
public $username;
public $password;
public function __construct() {
$this->connect();
}
public function connect(){
try {
$this->dbConnection = new PDO("mysql:host=".DB_SERVER."; dbname=".DB_NAME, DB_USER, DB_PASS);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
}
public function properties() {
$properties = array();
foreach (self::$dbFields as $field) {
if (isset($this->field) || property_exists($this, $field)) {
$properties[$field] = $this->$field;
}
}
return $properties;
}
public function property_values() {
$property = $this->properties();
$propertyValues = implode(", :", array_keys($property));
return $propertyValues;
}
public function insert(){
// this is where all the is going to happen. it's a work in progress
// $sql = "INSERT INTO". $this->tableName. " (";
// $sql .= implode(", ",array_keys($this->properties()));
// $sql .= ")VALUES(". ;
// $q = $this->db->prepare($sql);
// $q->execute(array('John', 'hula'));
}
}
$database = new Database();
$vals = $database->property_values();
print_r($vals);
?>
$propertyValues = ':'. implode(", :", array_keys($property));
To get the first array key, you can use the foolproof:
reset($array);
$firstkey = key($array);
Since you also need the value (cannot change the key, can only unset the old one and set a new), you can use:
$value = reset($array);
$firstkey = key($array);
unset($array[$firstkey]);
$array[':'.$firstkey] = $value;
However
This isn't really the cleanest approach. As often happens, you have become fixated on tweaking "your" approach to work when there is in fact a cleaner approach available.
One approach:
$keys = array_map(function($key) { return ':'.$key; }, array_keys($property));
$property = array_combine($keys, $property);
Now all the property names have had a ':' added in front.
Another approach would be to just manually prepend a ':'; webbiedave beat me to that.