Hi,
I would like to know if there is any example how to use the ipl/orm PropertyModifiers on insert / update.
}
} else {
$values = $this->getValues();
try {
/** @var User $user */
$user = Auth::getInstance()->getUser();
if ($this->job === null) {
$values['author'] = $user->getUsername();
$values['ctime'] = (new DateTime())->getTimestamp() * 1000.0;
$values['mtime'] = (new DateTime())->getTimestamp() * 1000.0;
$conn->insert('x509_job', $values);
$message = $this->translate('Created job successfully');
} else {
$values['mtime'] = (new DateTime())->getTimestamp() * 1000.0;
$conn->update('x509_job', $values, ['id = ?' => $this->job->id]);
$message = $this->translate('Updated job successfully');
}
Every example of update/insert always circumvents any modifier.
Would that be a vaild approach?
public function save($asTransaction = true)
{
$db = Database::get();
if($asTransaction){
$db->beginTransaction();
}
$this->beforeSave($db);
$behavior = new Behaviors();
$this->createBehaviors($behavior);
$behavior->persist($this);
$values=$this->getValues();
if (!isset ($this->id) || $this->id === null) {
$db->insert($this->getTableName(), $values);
$this->id = $db->lastInsertId();
} else {
$db->update($this->getTableName(), $values, [$this->getKeyName().' = ?' => $this->id]);
}
if($asTransaction){
$db->commitTransaction();
}
}
Thanks in advance and
Best Regards
Nicolas
nilmerg
(Johannes Meyer)
April 26, 2024, 6:35am
2
Yes. Because they’re not based on ipl\Orm, but ipl\Sql. And the latter has no such thing.
I don’t recommend it. We don’t use it ourselves that way either. Typically, data being used during an insert or update, comes from a form, which is then supposed to provide suitable values. No modifiers are needed then, besides some simple transformations if necessary. (like DateTime::format)
but why is there a toDb() if you won’t use it?
if ($value === null) {
return null;
}
$datetime = DateTime::createFromFormat('U.u', sprintf('%F', $value / 1000.0));
$datetime->setTimezone(new DateTimeZone(date_default_timezone_get()));
return $datetime;
}
public function toDb($value, $key, $context)
{
if (is_numeric($value)) {
return (int) ($value * 1000.0);
}
if (! $value instanceof DateTime) {
try {
$value = new DateTime($value);
} catch (Exception $err) {
throw new ValueConversionException(sprintf('Invalid date time format provided: %s', $value));
nilmerg
(Johannes Meyer)
April 26, 2024, 7:45am
4
We use it. Just not for updating or inserting data. The method you linked is at the moment only used to transform values of filter expressions.
ahh so you transform the value for the filter to a Db compatible one in order to compare?
Thanks