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

Can't delete record using schema in Phalcon v4.0 #14080

Closed
Enzo-life opened this issue May 15, 2019 · 7 comments
Closed

Can't delete record using schema in Phalcon v4.0 #14080

Enzo-life opened this issue May 15, 2019 · 7 comments
Labels
bug A bug report status: medium Medium

Comments

@Enzo-life
Copy link

Just tried to delete a record:

         /** @var Model $className */
         $model = $className::findFirst([
            'id=?0',
            'bind' => [$id],
         ]);
         $model->delete();

Getting error:

Array to string conversion
#0 [internal function]: PDO->prepare('DELETE FROM `Ar...')
#1 [internal function]: Phalcon\Db\Adapter\Pdo->execute('DELETE FROM `Ar...', Array, Array)
#2 [internal function]: Phalcon\Db\Adapter->delete(Array, '`id` = ?', Array, Array)

Checking sources.

/phalcon/mvc/model::delete() has such code:

		let schema = this->getSchema(),
			source = this->getSource();

		if schema {
			let table = [schema, source];
		} else {
			let table = source;
		}

		/**
		 * Join the conditions in the array using an AND operator
		 * Do the deletion
		 */
		let success = writeConnection->delete(table, join(" AND ", conditions), values, bindTypes);

/phalcon/db/adapter::delete() has such code:

public function delete(string table, var whereCondition = null, var placeholders = null, var dataTypes = null) -> bool
	{
		var sql, escapedTable;

		let escapedTable = this->escapeIdentifier(table);

		if !empty whereCondition {
			let sql = "DELETE FROM " . escapedTable . " WHERE " . whereCondition;
		} else {
			let sql = "DELETE FROM " . escapedTable;
		}

Сonclusion: framework doesn't support delete when schema is used.

@JABirchall
Copy link

JABirchall commented May 16, 2019

What is $id set too?

@Enzo-life
Copy link
Author

Enzo-life commented May 16, 2019

$id is integer, the record was fetched from DB successfully.

I'm new in Phalcon and i didn't try to update records yet, but as i see in sources, table value as array with schema and source doesn't process, because table parameter is string in declaration of method delete().

@JABirchall
Copy link

JABirchall commented May 16, 2019

jus noticed but might be wrong, but it looks like its the table is Array possibly.

@Enzo-life
Copy link
Author

Do you see public function delete(string table, in /phalcon/db/adapter.zep? How can be possible an array there? :)

@JABirchall
Copy link

Array to string conversion

@Enzo-life
Copy link
Author

Enzo-life commented May 16, 2019

Yes, this is a problem.

let table = [schema, source];

////////////////// $table is array
let success = writeConnection->delete(table, join(" AND ", conditions), values, bindTypes);

..............................................

////////////////// $table should be STRING, so it tries to convert given array to string
public function delete(string table, var whereCondition = null, var placeholders = null, var dataTypes = null) -> bool
	{
		var sql, escapedTable;

                ////////////////// $table is already string with value 'Array', as converted, so $escapedTable is string 'Array' too
		let escapedTable = this->escapeIdentifier(table);

                ////////////////// Here we get an SQL 'DELETE FROM `Array` WHERE `id` = ?'
		if !empty whereCondition {
			let sql = "DELETE FROM " . escapedTable . " WHERE " . whereCondition;
		} else {
			let sql = "DELETE FROM " . escapedTable;
		}

P.S. This code was taken from master branch, but in branch 4.0 the same situation.

@niden niden added this to To do in 4.1 Release via automation May 26, 2019
@niden niden added 4.0 and removed 4.1 labels Jun 21, 2019
@niden niden added 4.0 and removed 4.1 labels Jul 2, 2019
@niden niden added this to To do in 4.0.0 Release via automation Jul 2, 2019
@niden niden removed this from To do in 4.1 Release Jul 2, 2019
@niden niden mentioned this issue Jul 6, 2019
5 tasks
@niden niden moved this from To do to In progress in 4.0.0 Release Jul 6, 2019
niden added a commit that referenced this issue Jul 6, 2019
niden added a commit that referenced this issue Jul 6, 2019
niden added a commit that referenced this issue Jul 6, 2019
@niden
Copy link
Sponsor Member

niden commented Jul 6, 2019

Resolved in #14235

@niden niden closed this as completed Jul 6, 2019
4.0.0 Release automation moved this from In progress to Done Jul 6, 2019
@niden niden added the bug A bug report label Dec 23, 2019
@niden niden added status: medium Medium and removed Bug - Medium labels Dec 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug report status: medium Medium
Projects
No open projects
4.0.0 Release
  
Done
Development

No branches or pull requests

3 participants