CActiveRecord and array type of attribute

Hi everybody!

I use PostgreSQL database in my project.

In example below $mydata->subscriber_sms is integer[] type

public function actionSubscriber()


{


	$mydata=Mydata::model()->findbyPk($_POST['id']);


	


	if (isset($_POST['Mydata']))


	{


	   $mydata->attributes=$_POST['Mydata'];


               &#036;mydata-&gt;subscriber_sms=<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />?


	   if (&#036;mydata-&gt;save())


		//redirection


	}


	&#036;this-&gt;render('subscriber',array(


   		    'user'=&gt;&#036;mydata,


	));


}

Which way can I assign value for attribute of array type?

I try $mydata->subscriber_sms=’{1,2,3}’;

It does not work :(

Thanks

Hi dude,

I think that you need separate this datas in another table and

make a relation between this tables.

Like this:

on table Mydata will have a field sms_id, and on table sms will have three fields id, mydata_id and sms_number.

The relations look alike this:

Table Mydata has_many sms table.

Then, the data will be more normalized on database. What do you think about?

To create additional table is classic way, but why array types exist?

If I use direct query like

update Mydata set subscribers_sms=’{1,2,3}’ where …

it will be done

Maybe Active Record can not do it.

Ok , I understood right now.

When you try to store a the array on subscribers_sms field, the application show any error?

The application can store some thing on database? what?

when $mydada->save()

i accept this error

"subscriber_sms" is of type integer[] but expression is of type integer

now i simply form direct query like update mydata set subscriber_sms=’{1,2…}’ where …

Other words - problem’s over

Thanks for support

What query (include bind parameters) is generated by Yii when you save data with ActiveRecord?

in log I have this:

UPDATE "base"."mydata" SET "id"=:yp0, "login"=:yp1, "name"=:yp2, "password"=:yp3, … "subscriber_sms"=:yp41, … WHERE "base"."userlist"."id"=-1

I do it without AR

try


		{


			&#036;attr=&#036;_POST['Mydata'];


	   		&#036;sql='update base.userlist set';


	   		


	   		&#036;v='';


			for(&#036;i=1; &#036;i&lt;=Mydata::SUBSCRIBER_SMS_COUNT; &#036;i++)


			{


				if(&#036;attr['sms'.&#036;i]==1) &#036;v.=(&#036;v==''?&quot;'{&quot;:',').&#036;i;


			}


			&#036;v=&#036;v==''?'null':&#036;v.&quot;}'&quot;;


			&#036;sql.=' subscriber_sms='.&#036;v;		


			


			


			&#036;sql.=' where id=:id_userlist';


			&#036;command=Yii::app()-&gt;db-&gt;createCommand(&#036;sql);


    		        &#036;command-&gt;bindValue(&quot;:id_userlist&quot;, Yii::app()-&gt;user-&gt;id);


    		        &#036;command-&gt;execute();


			Yii::app()-&gt;user-&gt;setFlash('EVENT','Data saved');


    		        &#036;this-&gt;refresh();


		}	


		catch (Exception &#036;e)


		{


			...


		}

You can enable CDbConnection::enableParamLogging to see what value is set for yp41. I didn’t work with PDO_PGSQL, may be it support array binding and you can try to write $mydata->subscriber_sms = array(1,2,3). But may be PDO_PGSQL can not support array type for fields. If it’s true, you can override CActiveRecord::insert() and CactiveRecord::update() methods for model or continue to use direct queries.

$mydata->subscriber_sms = array(1,2,3) does dot work

it is realy PDO_PGSQL bug :(

Parameter :yp41 for attribute $mydata->subscriber_sms always is integer datatype, and tail of the bug from PDO