Yii Framework Forum: adding manual data for attributes model manualy - Yii Framework Forum

Jump to content

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

adding manual data for attributes model manualy save() model array manual Rate Topic: -----

#1 User is offline   kay 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 22
  • Joined: 14-July 11

Posted 22 July 2011 - 01:42 PM

hello guys . i`m soory i`m asking again ..

i had this trouble since this morning ..

i want to save an array into a model that i get from multiple field

if(isset($_POST['komponent'])){
                    //print_r($_POST['komponent']);    [i]//  [0] => a [1] => b [/i]
                    $mod = array("komponent" => $_POST['komponent']);
                    print_r($mod);   [i]//Array ( [komponent] => Array ( [0] => a [1] => b ) ) [/i]
                    $modelb->attributes= $mod;
                    $modelb->save();
                }


i tried to input this with an array into field named komponent

i had tried to save with more declare in model like this
$modelb->attributes['komponent'] = $mod;

when i see in database my field fulled with "array" not the data i tried to store ..



oh yea . i had tried this one too ..
i thougt when looping it will store manualy one by one

if(isset($_POST['komponent'])){
                    print_r($_POST['komponent']);
                    foreach ($_POST['komponent'] as $newkomponent):
                        
                        $baru = array('komponent'=>$newkomponent);
                     
                        $modelb->attributes = $baru;
                        $modelb->save();
                        
                    endforeach;
                    print_r($baru); 
                    
                

need you`r help :)
0

#2 User is offline   elbek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 134
  • Joined: 22-July 10
  • Location:MA, USA

Posted 22 July 2011 - 02:04 PM

Take a look
public function setAttributes($values,$safeOnly=true)
	{
		if(!is_array($values))
			return;
		$attributes=array_flip($safeOnly ? $this->getSafeAttributeNames() : $this->attributeNames());
		foreach($values as $name=>$value)
		{
			if(isset($attributes[$name]))
				$this->$name=$value;
			else if($safeOnly)
				$this->onUnsafeAttribute($name,$value);
		}
	}

framework checks if the model has such attribute
isset($attributes[$name])

then it sets the value
$this->$name=$value;



then have a look your output
[i]//Array ( [komponent] => Array ( [0] => a [1] => b ) ) [/i]


framework tries to find 0 and 1 attributes from model

then it can not find then does not set value to model.

instead of 0 and 1 here should be attr names.
Elbek
0

#3 User is offline   kay 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 22
  • Joined: 14-July 11

Posted 23 July 2011 - 10:46 AM

View Postelbek, on 22 July 2011 - 02:04 PM, said:

Take a look
public function setAttributes($values,$safeOnly=true)
	{
		if(!is_array($values))
			return;
		$attributes=array_flip($safeOnly ? $this->getSafeAttributeNames() : $this->attributeNames());
		foreach($values as $name=>$value)
		{
			if(isset($attributes[$name]))
				$this->$name=$value;
			else if($safeOnly)
				$this->onUnsafeAttribute($name,$value);
		}
	}

framework checks if the model has such attribute
isset($attributes[$name])

then it sets the value
$this->$name=$value;



then have a look your output
[i]//Array ( [komponent] => Array ( [0] => a [1] => b ) ) [/i]


framework tries to find 0 and 1 attributes from model

then it can not find then does not set value to model.

instead of 0 and 1 here should be attr names.



thanks for your help . i will try to implement this code :)
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