@TomB How can I fit in an updateWhere() function to work with the save() function. Am writing a program that has to update a particular column in the database and I created the updateWhere() function with this code
From a performance perspective, this makes sense but from an Object-Oriented Programming one, it doesn’t.
In OOP you want to deal with objects, in this case an object that represents a complete single item. An Author, A joke, A category, etc.
Using updateWhere you are no longer dealing in complete objects but making changes to the underlying data directly, you are dealing with a small part of multiple single things. This breaks encapsulation as any validation or processing that the containing object might do is bypassed. For example, you could validate the data in the joke when setting the various properties.
Depending on how many results you get though might run into out of memory exceptions. There are ways around that though. Basically what you’d want is not return all results at once but query several times using OFFSET and LIMIT. Combine this with a PHP Generator and you’re good to go. Let me know if you want an example of that.
public function save($record){
$entity = new $this->className(...$this->constructorARgs);
try{
if($record[$this->primaryKey] == ''){
$record[$this->primaryKey] = null;
}
$insertId = $this->insert($record);
$entity->{$this->primaryKey} = $insertId;
}catch(\PDOException $e){
$this->update($records); // Notice the change in the $record variable. It has changed to $RECORDS
}
$records = $this->tableName->find('column', 'value'); // Knowingly change it to $this->tableName which I think it should be
foreach($records as $record){
$record->lastActivity = NOW(); // lastActivity is the column to be updated while mysql NOW() function is what should saved inside the lastActivity column
$this->tableName->save($record)
}
return $entity;
}
I’m not sure exactly what it is you’re trying to achieve but that apparoch is certainly the wrong direction.
$this->update($records); // Notice the change in the $record variable. It has changed to $RECORDS
The $records variable does not exist so this won’t work
The update method expects an array or object that represents a single record, you’d need to amend the update method to update multiple records.
$record->lastActivity = NOW(); // lastActivity is the column to be updated while mysql NOW() function is what should saved inside the lastActivity column
The DatabaseTable class in the book was designed to work with any table. This line will mean it can only work with tables that have a lastActivity column.
public function findAll() {
$limit = 100;
$offset = 0;
while (true) {
$result = $this->query('SELECT * FROM ' . $this->table.' ORDER BY id OFFSET '.$offset.' LIMIT '.$limit);
$rows = $result->fetchAll();
if (count($rows) === 0) {
return;
}
foreach ($rows as $row) {
yield $row;
}
$offset += 100;
}
This assumes there is an id column that can be sorted on. If you this without an ORDER BY the result is undefined; it may return some rows multiple times and some rows not at all. With an ORDER BY it works.
What the code does instead of querying everything in the entire table at once, it fetches them in batches of 100, which should easily fit in memory.