We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Problems With Deletion

I am trying to do something similar to the results of this post (https://forum.phalcon.io/discussion/183/insert-and-delete-multiple-related-records), but I am having troubles making this work, as well as just getting deletion to work normally.

```foreach($deletable as $single) { $user = UserProjects::findFirst(array( 'conditions' => 'user_id=?0 and project_id=?1 and is_stakeholder=1', 'bind' => array( $single, $project->id ) ));

var_dump($user->delete());
exit;


This throws me a PDOException. I ran some code to show me the queries being generated, and for some reason I am getting: DELETE FROM `users_projects` WHERE. There is nothing getting appended to the end of the query.

I have ensured that I am getting a result from the initial findFirst, but, after that, nothing happens. Any ideas? I'm on Phalcon 0.9.1.


98.9k

Does UserProjects have a primary key?



5.1k

It does not. It is a pivot table. Sorry. Forgot to mention that.



98.9k

Since the SQL is dinamically generated, the ORM cannot delete a record if a primary key is not available.



5.1k

So, I take there is no way to delete from pivot tables, aside from creating the query yourself? If that's the case, that's fine.



98.9k

You can do something like:

$user->getConnection->delete("user_projects", "user_id=? and project_id=? and is_stakeholder=1", array($singe, $project->id));