Skip to content

beforeInsert hook throws 500 error with Mongo Mapper #1292

@jezzball

Description

@jezzball

Hi,

I think I've found a problem with the beforeInsert hook when used with the Mongo Mapper. Running the script below on the CLI produces the error below. Why would it need _id before inserting? The error appears even if the function is completely empty. The beforeUpdate hook works fine without error.

In case it matters:

  • PHP 8.0.30
  • MongoDB 7.0.11
  • PHP MongoDB driver library 1.20.0 (going back as far as 1.16.0 doesn't help, same error)
  • Fat-Free Framework 3.9.0 (going back as far as 3.7.0 doesn't help, same error)

Thanks!

Undefined array key "_id"
[vendor/bcosca/fatfree-core/db/mongo/mapper.php:260] 
[vendor/bcosca/fatfree-core/db/mongo/mapper.php:260] Base->{closure}()
[test:30] DB\Mongo\Mapper->insert()
#!/usr/bin/env php
<?php

require(__DIR__.'/vendor/autoload.php');

$f3=\Base::instance();
$f3->config('config.ini');

$db_uri="mongodb+srv://{$f3->get('DB_USER')}:{$f3->get('DB_PASS')}@{$f3->get('DB_SRV')}/?tls=false";
$f3->set('DB', new DB\Mongo($db_uri, $f3->get('DB_NAME')));

class SomeThing extends \DB\Mongo\Mapper {

	function __construct() {
		parent::__construct(\Base::instance()->get('DB'), 'myCollection');

		$this->beforeInsert(function($self) {
			$self->created_at=new \MongoDB\BSON\UTCDateTime();
			$self->updated_at=new \MongoDB\BSON\UTCDateTime();
		});
		$this->beforeUpdate(function($self) {
			$self->updated_at=new \MongoDB\BSON\UTCDateTime();
		});
	}

}

$something=new SomeThing;
$something->blah='blah';
$something->insert();

?>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions