Hello everyone on the Yii forum,
I’d like your help with something. I’m new to Yii and was surprised to
see there was no build-in way to save an object and all of its related objects.
And then I stumbled on the WithRelatedBehavior extension and thought I had found my solution,
but for some reason I can’t get it to work.
Let me explain what I have to do first:
I have three models:
-
a Match
-
a Set
-
a Participant
A Match can conist of multiple Sets, and each set has multiple Participants. A participant has as its primary key a pair consisting of (setId, userId). The matchId and setId need to be created on the fly in the database by a sequence. Because the keys need to be generated I’d like to save the whole packet in one go.
This is the code I have in the models:
class Match extends CActiveRecord
{
...
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'sets'=>array(self::HAS_MANY, 'Set', 'matchId')
);
}
...
}
class Set extends CActiveRecord
{
...
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'participants'=>array(self::HAS_ONE, 'PlayerSetParticipation', 'setId')
);
}
...
}
This is the actual call to save I make:
$pp1=new PlayerSetParticipation;
$pp1->userId=$this->userId1;
$pp2=new PlayerSetParticipation;
$pp2->userId=$this->userId2;
$set1=new Set;
$set1->participants=array($pp1,$pp2);
$newMatch=new Match;
$newMatch->matchDate = date('Y-m-d H:i:s');
$newMatch->sets=array($set1);
$newMatch->withRelated->save(true,array('sets'=>array('participants',)));
It works fine when I do not add the participants, i.e. when I make the call:
$newMatch->withRelated->save(true,array('sets'));
But when I add another level of depth by adding participants, I keep getting the following error:
Array to string conversion
[b]
\…\extensions\wr\WithRelatedBehavior.php(39)[/b]
27 if($owner===null)
28 $owner=$this->getOwner();
29
30 if($data===null)
31 {
32 $attributes=null;
33 $newData=array();
34 }
35 else
36 {
37
38 $attributeNames=$owner->attributeNames();
39 $attributes=array_intersect($data,$attributeNames);
40
41 if($attributes===array())
42 $attributes=null;
43
44 $newData=array_diff($data,$attributeNames);
45 }
46
47 $valid=$owner->validate($attributes,$clearErrors);
48
49 foreach($newData as $name=>$data)
50 {
51 if(!is_array($data))
This makes me wonder if what I wanted to do is actually supported by the extension, but the advanced usage example on yiiext.github.io/extensions/with-related-behavior/index.html seems to imply something along these lines should be possible. I tried copying that example , but I also couldn’t get that to work. I kept getting the same error. I sincerely hope I’ve explained my problem well enough, and that someone might know why this error keeps showing up.