0

How can use PostgreSQL array_append() and array_remove() in Yii? I am trying to update array type attribute on following table rows-

CREATE  TABLE Books(
id INT NOT NULL PRIMARY KEY,  
name UUID[])

Sample data rows: 
1       NULL
2       NULL

$books=Books::model()->findByPk($id);
$books->name='array_append(name, $Name_key_array)';
$books->save();

I have found following error:

CDbException
CDbCommand failed to execute the SQL statement: SQLSTATE[22P02]: Invalid text representation: 7 ERROR: array value must start with "{" or dimension information

But If I use array directly then its work but not when using variable name i.e.

array_append({1,2,3}, {4})

Also, I already tried few more ways but haven't found any success. I hope will find some great ideas to fix this issue.

1
  • Check the below answer. Commented Dec 6, 2013 at 16:21

1 Answer 1

1

I think you are using the array_append() in other way. it should be like this

SELECT array_append(ARRAY[1,2], 3);
 array_append
--------------
 {1,2,3}
(1 row)

So if i correct yours, it should be

$books->name='array_append($Name_key_array, name)';

for removing elements from an array, check this solution: http://www.youlikeprogramming.com/2013/06/removing-values-from-a-postgresql-array/

to an array in PostgreSQL data must be inserted in the following way,

INSERT INTO table (arr_col) VALUES ('{val1,val2, val3}');

http://www.postgresql.org/docs/9.1/static/arrays.html

Sign up to request clarification or add additional context in comments.

4 Comments

Its not working. Also, I have tried with CDbExpression but it was displaying following error "CDbCommand failed to execute the SQL statement: SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "$" " My Code: $books->name=new CDbExpression('array_append(name,$Name_key_array)');
what happens if you hard code the values like this to see if it works. array_append(columname, value). i.e array_append(name, 5)
If I use array directly then its works but not when using variable name.
Can you please update the question by stating the attempt you made now to resolve without variable name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.