-2

First, I need to tell you that I am pretty new in PHP OOP so please bear with me.

When I create the MySQL tables, I prefer to specify the name of the table in columns. For example instead of id in users table I use user_id, or instead of id in photos table I use photo_id. That helps me understand the MySQL statements easier when I check them later on. (I understand that different people have different preferences)

Now, the problem is, when I have a method in a class that uses the table's id but I want to use the in sub-classes that each target different tables. Here is an example:

I have two tables. One for users and one for photos. The user table have PK as user_id and the photo table and PK as photo_id. For each table I have a class with the same names. Also, both classes have many methods in comman, so I created one class and put these classes and sub-classes and mostly it is fine. The problem comes when I want to target a table based on it's ID, like the Save method below:

  // updates a row if it exists, creates it if it doesn't
    public function Save(){

        return isset($this->user_id) ? $this->Update() : $this->Create();

    }

//Creates a user in Database
    public function Create(){
        global $Database;

        $Properties = $this->CleanProperties();


        $Sql = "INSERT INTO " . self::$DBTable . "(" . implode(",", array_keys($Properties)) . ")";
        $Sql .= "VALUE ('";


        $Sql .= implode("', '", (array_values($Properties)));
        $Sql .= "')";

        if ($Database->Query($Sql)){
            $this->user_id= $Database->InsertID();
             return true;

        } else {

            return false;
        }

    }

    /// Updates a user in Database
    /// returns "true" if the row is affected
    /// returns "false" if not
    Public function Update(){
        global $Database;
        $Properties = $this->CleanProperties();

        $PropertyPairs = array ();

        foreach ($Properties as $Key => $Value){

            $PropertyPairs[] = "{$Key}='{$Value}'";
        }

        $Sql =  "UPDATE " . self::$DBTable . " SET ";
        $Sql .= implode(", ", $PropertyPairs);
        $Sql .= " WHERE user_id     =  " . $Database->EscapeString($this->user_id) ;

        $Database->Query($Sql);

        return (mysqli_affected_rows($Database->DBConnection) == 1) ? true : false;
    }

    /// Deletes a user row from MySQL base on the user ID
    /// returns "true" if the row is affected
    /// returns "false" if not
    public function Delete(){
        global $Database;

        $Sql = "DELETE FROM " . self::$DBTable;
        $Sql .= " WHERE user_id = ";
        $Sql .= $Database->EscapeString($this->user_id);
        $Sql .= " LIMIT 1";
        $Database->Query($Sql);

        return (mysqli_affected_rows($Database->DBConnection) == 1) ? true : false;
    }

I searched for different solutions and what I found was a method called fetch_field_direct. But, using that made me more confused.

Does anyone have a solution for this? Thanks

1 Answers1

0

Not sure I understand your question correctly, but I think all these problems occur because you decided to use a different standard for your data tables by specifying table name with the id. The simplest solution to that would probably be obeying the standard and using id without prefixes. If you want to have a better readability, you can always try to specify your table name with a dot notation like photos.id giving you the same readability as photo_id and no issues. An alternative (which I would not recommend) would be to pass the id as a parameter like this:

<?php
Class Foo {
    public $bar;
    public function __construct() {
        $this->bar = 'test';
    }
    public function test($param) {
        return $this->{$param};
    }
}

$obj = new Foo();

echo $obj->test('bar');

prints 'test'.

Danielius
  • 852
  • 5
  • 23
  • Hi, Thanks for the comment. I am pretty much for following the standard and best practices. But, I didn't come to the conclusion that using the id for each table (with no prefix) is standard rather more of a preference. for example in this discussion: https://stackoverflow.com/questions/208580/naming-of-id-columns-in-database-tables – siamak ehsani Jun 07 '20 at 20:19
  • @siamakehsani well, it may not be a standard, but it can still make your life a bit easier in this case. What do you think about using dot notation, wouldn't it give you the same effect in the sense of readability? You can always try to implement the second method I wrote about and have a parameter `$prefix` or something and add it to the id, even though I would not recommend it... – Danielius Jun 07 '20 at 20:24
  • Yes I think so. I will definitely give it a try and see how it goes. – siamak ehsani Jun 07 '20 at 20:39