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

Non-static method Propel\Runtime\ActiveQuery\ModelCriteria::delete()` cannot be called statically #2009

Open
phpfui opened this issue Jun 21, 2024 · 2 comments

Comments

@phpfui
Copy link

phpfui commented Jun 21, 2024

Getting the following error from the code generated by propel init:

Fatal error: Uncaught Error: Non-static method Propel\Runtime\ActiveQuery\ModelCriteria::delete()` cannot be called statically in C:\websites\php-orm-sql-benchmarks\SOB\Propel2\Base\EmployeeQuery.php on line 222

Indeed, ModelCriteria::delete is not static.

Running PHP 8.2.13 (but this is not a PHP issue, PHP is reported the error correctly)

Composer:

"propel/propel": "2.0.0-beta4",

The generated method in question:

	public function delete(?ConnectionInterface $con = null) : int
	{
		if (null === $con) {
			$con = Propel::getServiceContainer()->getWriteConnection(EmployeeTableMap::DATABASE_NAME);
		}

		$criteria = $this;

		// Set the correct dbName
		$criteria->setDbName(EmployeeTableMap::DATABASE_NAME);

		// use transaction because $criteria could contain info
		// for more than one table or we could emulating ON DELETE CASCADE, etc.
		return $con->transaction(static function() use ($con, $criteria) {
			$affectedRows = 0; // initialize var to track total num of affected rows

			EmployeeTableMap::removeInstanceFromPool($criteria);

			$affectedRows += ModelCriteria::delete($con);  // this is line 222
			EmployeeTableMap::clearRelatedInstancePool();

			return $affectedRows;
		});
	}
@rizwanjiwan
Copy link

Hey, I tired reproducing this in my project (php 8.3.6). Seems to work fine. Since the generated Query base classes inherit from ModelCriteria, ModelCriteria::delete($con) reads to me to be equivalent to parent::delete($con): http://php.adamharvey.name/manual/en/keyword.parent.php

It's not recommended, but still valid PHP.

I'm doing this on my end (on my implementation of an employee model): EmployeeQuery::create()->filterById(2)->delete()

I'm happy to try and help if you let me know if you see it differently or have more information.

@phpfui
Copy link
Author

phpfui commented Sep 13, 2024

So it looks like it should be parent::delete($con) and not ModelCriteria::delete($con).

I think this changed in a recent version of PHP. Because ModelCriteria::delete does not reference $this, it must be static. parent::delete has an implicit $this, so it calls the parent method, which is not static. That should work, but I don't know the code, which is why I asked.

This makes sense to me now. Looks like this changed in PHP 8.0. https://php.watch/versions/8.0/non-static-static-call-fatal-error

This would explain why it worked in the past but does not work now. Not sure why you can't reproduce. Maybe I generated things wrong?

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

2 participants