Answer the question
In order to leave comments, you need to log in
What are objects in php?
I'm trying to make a simple website using the ActiveRecord pattern.
In the ActiveRecord and ORM pattern, we create a class and properties that correspond to a table, we get one table - one class. Plus, each class has 4 methods - insert, delete, edit, update.
When we get all the records from the database using the FETCH_CLASS method, we create an object of the desired class for each row and add this object to the array. It turns out an array of objects.
I do not quite understand what the objects in this array are? Something like an associative array, where the names of the variables in the class are the keys, and the values of the variables, the values of these keys?
Are they different from arrays in that certain methods can be applied to them? for example update, find the object where id = 1 and assign other values to the variables.
in some example I saw that with the insert method they check if there is an object where id is already set or something like that. Why check the id if in the database the id type is SERIAL and when a new record is added it will be unique, but we do not write the id in the sql query itself?
public function insert() {
// Есть у объекта статьи ID?
if ( !is_null( $this->id ) ) trigger_error ( "Article::insert(): Attempt to insert an Article object that already has its ID property set (to $this->id).", E_USER_ERROR );
// Вставляем статью
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sql = "INSERT INTO articles ( publicationDate, title, summary, content ) VALUES ( FROM_UNIXTIME(:publicationDate), :title, :summary, :content )";
$st = $conn->prepare ( $sql );
$st->bindValue( ":publicationDate", $this->publicationDate, PDO::PARAM_INT );
$st->bindValue( ":title", $this->title, PDO::PARAM_STR );
$st->bindValue( ":summary", $this->summary, PDO::PARAM_STR );
$st->bindValue( ":content", $this->content, PDO::PARAM_STR );
$st->execute();
$this->id = $conn->lastInsertId();
$conn = null;
echo ' news addes';
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question