Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

beforeInsert hook throws 500 error with Mongo Mapper #388

Open
jezzball opened this issue Jan 11, 2025 · 0 comments
Open

beforeInsert hook throws 500 error with Mongo Mapper #388

jezzball opened this issue Jan 11, 2025 · 0 comments

Comments

@jezzball
Copy link

Hi,

I originally logged this issue against bcosca/fatfree but later realised it should maybe have gone here...

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();

?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant