There is a class that describes a row of the BaseModel table. BaseModel has many heirs for each table in the database (for example: OrderModel ).

The base model has a delete method that looks like this:

  /** * This function should be overriden if needed * @return boolean(true|false) */ public function beforeDelete() { return true; } /** * This function should be overriden if needed * @return boolean(true|false) */ public function afterDelete() { return true; } /** * Remove record from DB * @throws UnexpectedValueException when trying to remove record with unknown id or id < 0 * @throws RuntimeException if afterDelete() didn't work properly * @return boolean(true|false) * * @uses Database::exec() * @uses $this->beforeDelete() * @uses $this->afterDelete() */ public function delete() { if (empty($this->id) || $this->id < 0) { throw new UnexpectedValueException('Can`t delete record with unset ID in '.__CLASS__. ', see file '.__FILE__.' at line '.__LINE__); } if ($this->beforeDelete()) { $sql = 'DELETE FROM `'.static::$tableName.'` WHERE `'.static::$fields['id'].'`=:'.static::$fields['id']; $pdoArgs = array(); $pdoArgs[] = array('param' => ':'.static::$fields['id'], 'arg' => $this->id, 'type' => static::$pdoTypes[static::$fieldTypes['id']]); if (Database::getInstance()->exec($sql, $pdoArgs)) { if ($this->afterDelete()) { return true; } else { throw new RuntimeException('Model has been deleted but afterDelete() don`t work correct, class '.__CLASS__. ', see file '.__FILE__.' at line '.__LINE__); } } } return false; } 

Interested in how to delete a class object in the afterDelete() method, since the model remains available and you can call the save() method for it, etc.

Example: let's say we have a list of orders, I go around it with foreach-ем and delete unacknowledged models (by calling the method ->delete() ), and then I launch another foreach to send orders ->send() . But in the list everything will still be objects that are deleted. How to make for them the right unset.

  • I did not fully understand the question, but if I still understood correctly, then: 1) you cannot remove an object from yourself, 2) I would make the isValid () method in the class returning false if the object is to be deleted and in the second foreache 'e, with send' unset would be invalid objects - rjhdby
  • You delete from the database, unset unnecessary models in the collection - Naumov
  • @Naumov You understood me correctly. Is it possible to do this inside the model, so that it would not be done every time manually? - Makarenko_I_V
  • So you need to remove from the collection, a certain array, and the model is lower than this observer, theoretically, you can try, of course, by passing the link this collection to the model and there already. But it already sounds like that even scary. - Naumov
  • @Naumov How strange it is to me that I cannot do inside my unset($this); method unset($this); and in my collection will appear null . - Makarenko_I_V

1 answer 1

The object is stored in RAM, and what you have is a link to this memory. The object is alive as long as there is at least one link to it. So you can call unset($this) any number, because it will not lead to any result.

It is impossible to delete the pseudo-variable $this .

Conclusion - you need to delete where the references to the object are stored.


PS

PDO :: exec () returns the number of rows, so if the request is successful, it does not delete any of the lines. RuntimeException will RuntimeException

  • Thanks for the PS and conceived. Since if the line is really deleted, it returns true, if there are problems, false, Exeption will only crash if there is a cant in the model that needs to be fixed. - Makarenko_I_V