Making CRUD abstracted class in PHP

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












I did a working CRUD abstracted class, and I am not sure that this is a good way for it, so I would like to hear your opinion.



protected function properties()
$properties = array();
foreach (self::$table_fields as $db_field)
if(property_exists($this, $db_field))
$properties[$db_field] = $this->$db_field;


return $properties;


public function create()
$db = db::getConnection()->conn;
$properties = $this->properties();

$key = implode(",",array_keys($properties));
$value = implode(",:",array_keys($properties));

$sql = "INSERT INTO ".self::$table."(" . $key . ") ";
$sql.= "VALUES (:" . $value . ")";
$stmt = $db->prepare($sql);
foreach($properties as $key=>$value)
$stmt->bindParam($key,$value);

if($stmt->execute())
print_r($stmt);
$this->id = db::the_insert_id();
return true;
else
print_r($stmt);
return false;

return $stmt;



This is working well: I am getting TRUE as response and it creates a user in database, but I am just not sure about it.







share|improve this question





















  • Just to be clear — what class is this, and what is db::?
    – 200_success
    Jan 25 at 22:39










  • db is database and its calling from another class. it's static method, i have also auto loader.
    – ÐÐ¸ÐºÐ¾Ð»Ð° Р.
    Jan 25 at 23:50










  • I know that db stands for "database", but what is that code? Mysqli?
    – 200_success
    Jan 26 at 0:17










  • it is PDO github.com/anonunder/firstep/blob/master/singleton_db.php
    – ÐÐ¸ÐºÐ¾Ð»Ð° Р.
    Jan 26 at 1:04
















up vote
0
down vote

favorite












I did a working CRUD abstracted class, and I am not sure that this is a good way for it, so I would like to hear your opinion.



protected function properties()
$properties = array();
foreach (self::$table_fields as $db_field)
if(property_exists($this, $db_field))
$properties[$db_field] = $this->$db_field;


return $properties;


public function create()
$db = db::getConnection()->conn;
$properties = $this->properties();

$key = implode(",",array_keys($properties));
$value = implode(",:",array_keys($properties));

$sql = "INSERT INTO ".self::$table."(" . $key . ") ";
$sql.= "VALUES (:" . $value . ")";
$stmt = $db->prepare($sql);
foreach($properties as $key=>$value)
$stmt->bindParam($key,$value);

if($stmt->execute())
print_r($stmt);
$this->id = db::the_insert_id();
return true;
else
print_r($stmt);
return false;

return $stmt;



This is working well: I am getting TRUE as response and it creates a user in database, but I am just not sure about it.







share|improve this question





















  • Just to be clear — what class is this, and what is db::?
    – 200_success
    Jan 25 at 22:39










  • db is database and its calling from another class. it's static method, i have also auto loader.
    – ÐÐ¸ÐºÐ¾Ð»Ð° Р.
    Jan 25 at 23:50










  • I know that db stands for "database", but what is that code? Mysqli?
    – 200_success
    Jan 26 at 0:17










  • it is PDO github.com/anonunder/firstep/blob/master/singleton_db.php
    – ÐÐ¸ÐºÐ¾Ð»Ð° Р.
    Jan 26 at 1:04












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I did a working CRUD abstracted class, and I am not sure that this is a good way for it, so I would like to hear your opinion.



protected function properties()
$properties = array();
foreach (self::$table_fields as $db_field)
if(property_exists($this, $db_field))
$properties[$db_field] = $this->$db_field;


return $properties;


public function create()
$db = db::getConnection()->conn;
$properties = $this->properties();

$key = implode(",",array_keys($properties));
$value = implode(",:",array_keys($properties));

$sql = "INSERT INTO ".self::$table."(" . $key . ") ";
$sql.= "VALUES (:" . $value . ")";
$stmt = $db->prepare($sql);
foreach($properties as $key=>$value)
$stmt->bindParam($key,$value);

if($stmt->execute())
print_r($stmt);
$this->id = db::the_insert_id();
return true;
else
print_r($stmt);
return false;

return $stmt;



This is working well: I am getting TRUE as response and it creates a user in database, but I am just not sure about it.







share|improve this question













I did a working CRUD abstracted class, and I am not sure that this is a good way for it, so I would like to hear your opinion.



protected function properties()
$properties = array();
foreach (self::$table_fields as $db_field)
if(property_exists($this, $db_field))
$properties[$db_field] = $this->$db_field;


return $properties;


public function create()
$db = db::getConnection()->conn;
$properties = $this->properties();

$key = implode(",",array_keys($properties));
$value = implode(",:",array_keys($properties));

$sql = "INSERT INTO ".self::$table."(" . $key . ") ";
$sql.= "VALUES (:" . $value . ")";
$stmt = $db->prepare($sql);
foreach($properties as $key=>$value)
$stmt->bindParam($key,$value);

if($stmt->execute())
print_r($stmt);
$this->id = db::the_insert_id();
return true;
else
print_r($stmt);
return false;

return $stmt;



This is working well: I am getting TRUE as response and it creates a user in database, but I am just not sure about it.









share|improve this question












share|improve this question




share|improve this question








edited Jan 26 at 1:10









200_success

123k14143401




123k14143401









asked Jan 25 at 21:52









Никола Р.

31




31











  • Just to be clear — what class is this, and what is db::?
    – 200_success
    Jan 25 at 22:39










  • db is database and its calling from another class. it's static method, i have also auto loader.
    – ÐÐ¸ÐºÐ¾Ð»Ð° Р.
    Jan 25 at 23:50










  • I know that db stands for "database", but what is that code? Mysqli?
    – 200_success
    Jan 26 at 0:17










  • it is PDO github.com/anonunder/firstep/blob/master/singleton_db.php
    – ÐÐ¸ÐºÐ¾Ð»Ð° Р.
    Jan 26 at 1:04
















  • Just to be clear — what class is this, and what is db::?
    – 200_success
    Jan 25 at 22:39










  • db is database and its calling from another class. it's static method, i have also auto loader.
    – ÐÐ¸ÐºÐ¾Ð»Ð° Р.
    Jan 25 at 23:50










  • I know that db stands for "database", but what is that code? Mysqli?
    – 200_success
    Jan 26 at 0:17










  • it is PDO github.com/anonunder/firstep/blob/master/singleton_db.php
    – ÐÐ¸ÐºÐ¾Ð»Ð° Р.
    Jan 26 at 1:04















Just to be clear — what class is this, and what is db::?
– 200_success
Jan 25 at 22:39




Just to be clear — what class is this, and what is db::?
– 200_success
Jan 25 at 22:39












db is database and its calling from another class. it's static method, i have also auto loader.
– ÐÐ¸ÐºÐ¾Ð»Ð° Р.
Jan 25 at 23:50




db is database and its calling from another class. it's static method, i have also auto loader.
– ÐÐ¸ÐºÐ¾Ð»Ð° Р.
Jan 25 at 23:50












I know that db stands for "database", but what is that code? Mysqli?
– 200_success
Jan 26 at 0:17




I know that db stands for "database", but what is that code? Mysqli?
– 200_success
Jan 26 at 0:17












it is PDO github.com/anonunder/firstep/blob/master/singleton_db.php
– ÐÐ¸ÐºÐ¾Ð»Ð° Р.
Jan 26 at 1:04




it is PDO github.com/anonunder/firstep/blob/master/singleton_db.php
– ÐÐ¸ÐºÐ¾Ð»Ð° Р.
Jan 26 at 1:04










1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted











  1. Format all identifiers in order to avoid a syntax error. Assuming it's mysql, use backticks for the purpose:



    $key = "`".implode("`,`",array_keys($properties))."`";



  2. You are going to repeat the second half of the code in the every crud method. To avoid that, add a query() method to your db class



    public function query($sql, $params = ) 
    $stmt = self::getConnection()->conn->prepare($sql);
    $stmt->execute($params);
    return $stmt;



    so you'll be able to make your crud methods much more concise.



    public function create()
    $properties = $this->properties();

    $key = "`".implode("`,`",array_keys($properties))."`";
    $value = implode(",:",array_keys($properties));

    $sql = "INSERT INTO `".self::$table."` (" . $key . ") ";
    $sql.= "VALUES (:" . $value . ")";
    db::query($sql, $properties);
    $this->id = db::the_insert_id();



  3. There is no point in returning true or false from such a method, the only reason for it to return false is a database error and such an error should be thrown in the form of Exception and handled elsewhere, making all this true false stuff rather useless.


  4. Your current setup is following Active Record pattern, making a data object to contain all the database interaction related code. Consider switching to Data Mapper pattern where you have two classes - your data class and a mapper that is responsible for all the database interactions.





share|improve this answer























  • 2. What about binding params? What is the difference between using query and bindparam? also, I already have that method. Thanks.
    – ÐÐ¸ÐºÐ¾Ð»Ð° Р.
    Jan 25 at 23:52











  • Don't you realize that your method doesn't use a prepared statement?
    – Your Common Sense
    Jan 26 at 8:11










  • It is using, it is just automatic and that is why I asked is that good way to do it. This is output: prntscr.com/i601uc
    – ÐÐ¸ÐºÐ¾Ð»Ð° Р.
    Jan 26 at 17:14










  • From the link you posted above, it is clear that your db:query() does NOT use a prepared statement, so I have no idea what are you talking about.
    – Your Common Sense
    Jan 26 at 21:29










  • $stmt = $db->prepare($sql); foreach($properties as $key=>$value) $stmt->bindParam($key,$value); $stmt->execute() This isn't prepared statement? I'm not executing the script from query method.. look my firstpost.
    – ÐÐ¸ÐºÐ¾Ð»Ð° Р.
    Jan 26 at 21:31











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186007%2fmaking-crud-abstracted-class-in-php%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted











  1. Format all identifiers in order to avoid a syntax error. Assuming it's mysql, use backticks for the purpose:



    $key = "`".implode("`,`",array_keys($properties))."`";



  2. You are going to repeat the second half of the code in the every crud method. To avoid that, add a query() method to your db class



    public function query($sql, $params = ) 
    $stmt = self::getConnection()->conn->prepare($sql);
    $stmt->execute($params);
    return $stmt;



    so you'll be able to make your crud methods much more concise.



    public function create()
    $properties = $this->properties();

    $key = "`".implode("`,`",array_keys($properties))."`";
    $value = implode(",:",array_keys($properties));

    $sql = "INSERT INTO `".self::$table."` (" . $key . ") ";
    $sql.= "VALUES (:" . $value . ")";
    db::query($sql, $properties);
    $this->id = db::the_insert_id();



  3. There is no point in returning true or false from such a method, the only reason for it to return false is a database error and such an error should be thrown in the form of Exception and handled elsewhere, making all this true false stuff rather useless.


  4. Your current setup is following Active Record pattern, making a data object to contain all the database interaction related code. Consider switching to Data Mapper pattern where you have two classes - your data class and a mapper that is responsible for all the database interactions.





share|improve this answer























  • 2. What about binding params? What is the difference between using query and bindparam? also, I already have that method. Thanks.
    – ÐÐ¸ÐºÐ¾Ð»Ð° Р.
    Jan 25 at 23:52











  • Don't you realize that your method doesn't use a prepared statement?
    – Your Common Sense
    Jan 26 at 8:11










  • It is using, it is just automatic and that is why I asked is that good way to do it. This is output: prntscr.com/i601uc
    – ÐÐ¸ÐºÐ¾Ð»Ð° Р.
    Jan 26 at 17:14










  • From the link you posted above, it is clear that your db:query() does NOT use a prepared statement, so I have no idea what are you talking about.
    – Your Common Sense
    Jan 26 at 21:29










  • $stmt = $db->prepare($sql); foreach($properties as $key=>$value) $stmt->bindParam($key,$value); $stmt->execute() This isn't prepared statement? I'm not executing the script from query method.. look my firstpost.
    – ÐÐ¸ÐºÐ¾Ð»Ð° Р.
    Jan 26 at 21:31















up vote
0
down vote



accepted











  1. Format all identifiers in order to avoid a syntax error. Assuming it's mysql, use backticks for the purpose:



    $key = "`".implode("`,`",array_keys($properties))."`";



  2. You are going to repeat the second half of the code in the every crud method. To avoid that, add a query() method to your db class



    public function query($sql, $params = ) 
    $stmt = self::getConnection()->conn->prepare($sql);
    $stmt->execute($params);
    return $stmt;



    so you'll be able to make your crud methods much more concise.



    public function create()
    $properties = $this->properties();

    $key = "`".implode("`,`",array_keys($properties))."`";
    $value = implode(",:",array_keys($properties));

    $sql = "INSERT INTO `".self::$table."` (" . $key . ") ";
    $sql.= "VALUES (:" . $value . ")";
    db::query($sql, $properties);
    $this->id = db::the_insert_id();



  3. There is no point in returning true or false from such a method, the only reason for it to return false is a database error and such an error should be thrown in the form of Exception and handled elsewhere, making all this true false stuff rather useless.


  4. Your current setup is following Active Record pattern, making a data object to contain all the database interaction related code. Consider switching to Data Mapper pattern where you have two classes - your data class and a mapper that is responsible for all the database interactions.





share|improve this answer























  • 2. What about binding params? What is the difference between using query and bindparam? also, I already have that method. Thanks.
    – ÐÐ¸ÐºÐ¾Ð»Ð° Р.
    Jan 25 at 23:52











  • Don't you realize that your method doesn't use a prepared statement?
    – Your Common Sense
    Jan 26 at 8:11










  • It is using, it is just automatic and that is why I asked is that good way to do it. This is output: prntscr.com/i601uc
    – ÐÐ¸ÐºÐ¾Ð»Ð° Р.
    Jan 26 at 17:14










  • From the link you posted above, it is clear that your db:query() does NOT use a prepared statement, so I have no idea what are you talking about.
    – Your Common Sense
    Jan 26 at 21:29










  • $stmt = $db->prepare($sql); foreach($properties as $key=>$value) $stmt->bindParam($key,$value); $stmt->execute() This isn't prepared statement? I'm not executing the script from query method.. look my firstpost.
    – ÐÐ¸ÐºÐ¾Ð»Ð° Р.
    Jan 26 at 21:31













up vote
0
down vote



accepted







up vote
0
down vote



accepted







  1. Format all identifiers in order to avoid a syntax error. Assuming it's mysql, use backticks for the purpose:



    $key = "`".implode("`,`",array_keys($properties))."`";



  2. You are going to repeat the second half of the code in the every crud method. To avoid that, add a query() method to your db class



    public function query($sql, $params = ) 
    $stmt = self::getConnection()->conn->prepare($sql);
    $stmt->execute($params);
    return $stmt;



    so you'll be able to make your crud methods much more concise.



    public function create()
    $properties = $this->properties();

    $key = "`".implode("`,`",array_keys($properties))."`";
    $value = implode(",:",array_keys($properties));

    $sql = "INSERT INTO `".self::$table."` (" . $key . ") ";
    $sql.= "VALUES (:" . $value . ")";
    db::query($sql, $properties);
    $this->id = db::the_insert_id();



  3. There is no point in returning true or false from such a method, the only reason for it to return false is a database error and such an error should be thrown in the form of Exception and handled elsewhere, making all this true false stuff rather useless.


  4. Your current setup is following Active Record pattern, making a data object to contain all the database interaction related code. Consider switching to Data Mapper pattern where you have two classes - your data class and a mapper that is responsible for all the database interactions.





share|improve this answer
















  1. Format all identifiers in order to avoid a syntax error. Assuming it's mysql, use backticks for the purpose:



    $key = "`".implode("`,`",array_keys($properties))."`";



  2. You are going to repeat the second half of the code in the every crud method. To avoid that, add a query() method to your db class



    public function query($sql, $params = ) 
    $stmt = self::getConnection()->conn->prepare($sql);
    $stmt->execute($params);
    return $stmt;



    so you'll be able to make your crud methods much more concise.



    public function create()
    $properties = $this->properties();

    $key = "`".implode("`,`",array_keys($properties))."`";
    $value = implode(",:",array_keys($properties));

    $sql = "INSERT INTO `".self::$table."` (" . $key . ") ";
    $sql.= "VALUES (:" . $value . ")";
    db::query($sql, $properties);
    $this->id = db::the_insert_id();



  3. There is no point in returning true or false from such a method, the only reason for it to return false is a database error and such an error should be thrown in the form of Exception and handled elsewhere, making all this true false stuff rather useless.


  4. Your current setup is following Active Record pattern, making a data object to contain all the database interaction related code. Consider switching to Data Mapper pattern where you have two classes - your data class and a mapper that is responsible for all the database interactions.






share|improve this answer















share|improve this answer



share|improve this answer








edited Jan 26 at 12:01


























answered Jan 25 at 22:30









Your Common Sense

2,435524




2,435524











  • 2. What about binding params? What is the difference between using query and bindparam? also, I already have that method. Thanks.
    – ÐÐ¸ÐºÐ¾Ð»Ð° Р.
    Jan 25 at 23:52











  • Don't you realize that your method doesn't use a prepared statement?
    – Your Common Sense
    Jan 26 at 8:11










  • It is using, it is just automatic and that is why I asked is that good way to do it. This is output: prntscr.com/i601uc
    – ÐÐ¸ÐºÐ¾Ð»Ð° Р.
    Jan 26 at 17:14










  • From the link you posted above, it is clear that your db:query() does NOT use a prepared statement, so I have no idea what are you talking about.
    – Your Common Sense
    Jan 26 at 21:29










  • $stmt = $db->prepare($sql); foreach($properties as $key=>$value) $stmt->bindParam($key,$value); $stmt->execute() This isn't prepared statement? I'm not executing the script from query method.. look my firstpost.
    – ÐÐ¸ÐºÐ¾Ð»Ð° Р.
    Jan 26 at 21:31

















  • 2. What about binding params? What is the difference between using query and bindparam? also, I already have that method. Thanks.
    – ÐÐ¸ÐºÐ¾Ð»Ð° Р.
    Jan 25 at 23:52











  • Don't you realize that your method doesn't use a prepared statement?
    – Your Common Sense
    Jan 26 at 8:11










  • It is using, it is just automatic and that is why I asked is that good way to do it. This is output: prntscr.com/i601uc
    – ÐÐ¸ÐºÐ¾Ð»Ð° Р.
    Jan 26 at 17:14










  • From the link you posted above, it is clear that your db:query() does NOT use a prepared statement, so I have no idea what are you talking about.
    – Your Common Sense
    Jan 26 at 21:29










  • $stmt = $db->prepare($sql); foreach($properties as $key=>$value) $stmt->bindParam($key,$value); $stmt->execute() This isn't prepared statement? I'm not executing the script from query method.. look my firstpost.
    – ÐÐ¸ÐºÐ¾Ð»Ð° Р.
    Jan 26 at 21:31
















2. What about binding params? What is the difference between using query and bindparam? also, I already have that method. Thanks.
– ÐÐ¸ÐºÐ¾Ð»Ð° Р.
Jan 25 at 23:52





2. What about binding params? What is the difference between using query and bindparam? also, I already have that method. Thanks.
– ÐÐ¸ÐºÐ¾Ð»Ð° Р.
Jan 25 at 23:52













Don't you realize that your method doesn't use a prepared statement?
– Your Common Sense
Jan 26 at 8:11




Don't you realize that your method doesn't use a prepared statement?
– Your Common Sense
Jan 26 at 8:11












It is using, it is just automatic and that is why I asked is that good way to do it. This is output: prntscr.com/i601uc
– ÐÐ¸ÐºÐ¾Ð»Ð° Р.
Jan 26 at 17:14




It is using, it is just automatic and that is why I asked is that good way to do it. This is output: prntscr.com/i601uc
– ÐÐ¸ÐºÐ¾Ð»Ð° Р.
Jan 26 at 17:14












From the link you posted above, it is clear that your db:query() does NOT use a prepared statement, so I have no idea what are you talking about.
– Your Common Sense
Jan 26 at 21:29




From the link you posted above, it is clear that your db:query() does NOT use a prepared statement, so I have no idea what are you talking about.
– Your Common Sense
Jan 26 at 21:29












$stmt = $db->prepare($sql); foreach($properties as $key=>$value) $stmt->bindParam($key,$value); $stmt->execute() This isn't prepared statement? I'm not executing the script from query method.. look my firstpost.
– ÐÐ¸ÐºÐ¾Ð»Ð° Р.
Jan 26 at 21:31





$stmt = $db->prepare($sql); foreach($properties as $key=>$value) $stmt->bindParam($key,$value); $stmt->execute() This isn't prepared statement? I'm not executing the script from query method.. look my firstpost.
– ÐÐ¸ÐºÐ¾Ð»Ð° Р.
Jan 26 at 21:31













 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186007%2fmaking-crud-abstracted-class-in-php%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Chat program with C++ and SFML

Function to Return a JSON Like Objects Using VBA Collections and Arrays

Will my employers contract hold up in court?