Yii Framework Forum: getAttributes() doesn't return custom attributes - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

getAttributes() doesn't return custom attributes Rate Topic: -----

#1 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 22 January 2009 - 11:13 AM

I've an AR that i also want to use as form model. Therefore i added some custom attributes:

<?php
class Zeit extends CActiveRecord
{
    public customerID;

    public safeAttributes()
    {
        return array('customerID', ...); // list of all db columns
    }
}


I need this customerID as i have a dropdown with customers that are not directly related to this AR. The dropdown value should be kept e.g. on unsuccessful validation. This works. But i also want to store the AR in session when saving, and restore the attributes from this session object, to pre-fill form values on return to the page.

If i use getAttributes(true), customerID is missing from the list of attributes. Am i missing something?
0

#2 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,902
  • Joined: 04-October 08
  • Location:DC, USA

Posted 22 January 2009 - 12:50 PM

It is by design that getAttributes only returns column values.
You probably should write explicitly a for-loop to dump out the attributes.
0

#3 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 23 January 2009 - 05:39 AM

I'm confused :)

So safeAttributes() only defines the attributes that can be set, but they will not be included in getAttributes()? I think i misunderstand the API for the parameter for getAttributes then:

Quote

If this is true (default), then all attribute values will be returned, including those that are not loaded from DB (null will be returned for these attributes).


What are those attributes "not loaded from DB"? And what values should i use to loop?
0

#4 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,902
  • Joined: 04-October 08
  • Location:DC, USA

Posted 23 January 2009 - 06:36 AM

safeAttributes is only used by setAttributes. It has nothing to do with getAttributes.

A column may or may not be loaded into AR, depending on the SELECT option of query criteria (by default, every column is loaded). When an AR is new (to be inserted), only those attributes whose values are set are considered as loaded.

So when the parameter is true, it means the return value will contain every column.

0

#5 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 23 January 2009 - 06:38 AM

I see, thanks for clarification.
0

#6 User is offline   aztech 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 206
  • Joined: 12-December 08
  • Location:Poland

Posted 23 January 2009 - 06:44 AM

AFAIR safeAttibues are removed in SVN version. For more details please see this topic.
0

#7 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 23 January 2009 - 06:47 AM

Yes, i've seen that, thanks.
0

#8 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,902
  • Joined: 04-October 08
  • Location:DC, USA

Posted 23 January 2009 - 06:58 AM

safeAttributes is still there (with enhancements). protectedAttributes is removed.
0

#9 User is offline   aztech 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 206
  • Joined: 12-December 08
  • Location:Poland

Posted 23 January 2009 - 10:36 AM

Oh sorry my bad
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users