1
<?php

class User {

// properties

public $table = 'users';
public $id;
public $username ;
public $password ; 
public $first_name ;
public $last_name ;

Above (id, username..., last_name) are the column of users table in MySQL Database. After fetching(I'm using PDO) a specific record from users table using id, how can I assign all records to their respective properties dynamically(a function may help that will search through all available properties and search those properties in the resulting associative array and then assign those specific values to their respective properties)?

// methods

public function set_user($id){
    global $dbh;
    // sql
    $sql = "SELECT * FROM $this->table WHERE id = {$id}";
    // return result array
    $result_arr = $dbh->pdo_query_fetch($sql);
}

$result_arr contains associative array of id, username, password, first_name and last_name.

NOTE : Here pdo_query_fetch() is a user-defined function.

Yousuf Memon
  • 4,638
  • 12
  • 41
  • 57
  • 1
    Take care when someones unsets the global variable `$dbh` your object would not work any longer. Instead inject the PDO connection when you create the object or use a setter method, that is more save and easier to change later. – hakre Sep 30 '12 at 15:30

4 Answers4

4

You're using PDO, it has that with PDO::FETCH_INTO:

PDO::query ( string $statement , int $PDO::FETCH_INTO , object $object )

See PDO::queryDocs.

It might be that your "own" database class then is "owning" you, because you started to encapsulate things too early. This might require you to add an additional function to your $dbh class and/or you might want to start to refactor.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • please a little more detail with an example. – Yousuf Memon Sep 30 '12 at 15:22
  • 1
    You find an example in a related question: http://stackoverflow.com/questions/7282962/php-pdo-do-the-fetch-styles-fetch-class-and-fetch-into-fetch-into-private-objec , it also demonstrates one of the other fetch-modes. Also there are various similar examples discussed in other questions like http://stackoverflow.com/questions/747847/how-can-i-simply-return-objects-in-pdo and http://stackoverflow.com/questions/7986167/pdo-fetchobject-into-object-attributes-oop-in-php – hakre Sep 30 '12 at 15:24
  • 2
    your way is sweeter, i remove my answer and +1 – Alain Tiemblo Sep 30 '12 at 15:31
3

I would suggest adding a method to your user class:

public function Assign( $vars = array( ) )
{
    if( is_object( $vars )) {
      $this->Assign( get_object_vars( $vars ));
      return;
    }

    foreach ( $vars as $key => $value )
      if ( property_exists( $this, $key ) )
        $this->$key = $value;
}

With this method you can set properties from another object, or a array to your existing object.

You can use it like this:

myUser = new User();
myUser->Assign($result_arr);
JvdBerg
  • 21,777
  • 8
  • 38
  • 55
1

You can use get_object_vars

$properties = get_object_vars($this);
foreach ($properties as $property => $property_value) {
  if (isset($result_arr[$property])) 
    $this->$property = $result_arr[$property];
}
air4x
  • 5,618
  • 1
  • 23
  • 36
1

Best would be to use PDOStatement::fetchObject (docs) or PDO::query with PDO::FETCH_INTO flag (docs) but if there is no way to do that for some reasons:

foreach ($result_arr as $key => $value) {
    if (property_exists($user, $key)) {
       $user->$key = $value;
    }
}

also you can extend User class with __set :

public function __set($key, $value) {
    // do nothing
}

it will be called on every non-accessible property call (private or not defined)

lupatus
  • 4,208
  • 17
  • 19