Skip to content

Commit

Permalink
Use saveRelated() and loadRelated() as method name as written in the …
Browse files Browse the repository at this point in the history
…documentation
  • Loading branch information
thyseus committed Jun 10, 2015
1 parent 1ba8cd5 commit bec2ccd
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions RelationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
use \yii\helpers\StringHelper;

trait RelationTrait{
public function loadAll($POST) {

public function loadRelated($POST) {
if ($this->load($POST)) {
$shortName = StringHelper::basename(get_class($this));
foreach ($POST as $key => $value) {
Expand All @@ -27,20 +27,20 @@ public function loadAll($POST) {
$relPKAttr = $relModelClass::primaryKey();
// $isCompositePK = (count($relPKAttr) > 1);
// if(!$isCompositePK){
if ($isHasMany) {
$container = [];
foreach ($value as $relPost) {
/* @var $relObj ActiveRecord */
$relObj = (empty($relPost[$relPKAttr[0]])) ? new $rel->modelClass : $relModelClass::findOne($relPost[$relPKAttr[0]]);
$relObj->load($relPost, '');
$container[] = $relObj;
}
$this->populateRelation($relName, $container);
} else {
$relObj = new $rel->modelClass;
$relObj->load($value);
$this->populateRelation($relName, $value);
if ($isHasMany) {
$container = [];
foreach ($value as $relPost) {
/* @var $relObj ActiveRecord */
$relObj = (empty($relPost[$relPKAttr[0]])) ? new $rel->modelClass : $relModelClass::findOne($relPost[$relPKAttr[0]]);
$relObj->load($relPost, '');
$container[] = $relObj;
}
$this->populateRelation($relName, $container);
} else {
$relObj = new $rel->modelClass;
$relObj->load($value);
$this->populateRelation($relName, $value);
}
// }
}
}
Expand All @@ -49,8 +49,8 @@ public function loadAll($POST) {
return false;
}
}
public function saveAll() {

public function saveRelated() {
/* @var $this ActiveRecord */
$db = $this->getDb();
$trans = $db->beginTransaction();
Expand Down Expand Up @@ -96,7 +96,7 @@ public function saveAll() {
$compiledNotDeletedPK = [];
foreach($notDeletedPK as $attr => $pks){
$compiledNotDeletedPK[$attr] = "$attr NOT IN(".implode(', ', $pks).")";
// echo "$notDeletedFK AND ".implode(' AND ', $compiledNotDeletedPK);
// echo "$notDeletedFK AND ".implode(' AND ', $compiledNotDeletedPK);
$relModel->deleteAll("$notDeletedFK AND ".implode(' AND ', $compiledNotDeletedPK));
}
}else{
Expand All @@ -119,7 +119,7 @@ public function saveAll() {
throw $exc;
}
}

/* this function is deprecated*/
public function getAttributesWithRelatedAsPost(){
$return = [];
Expand All @@ -133,7 +133,7 @@ public function getAttributesWithRelatedAsPost(){
}
return $return;
}

public function getAttributesWithRelated(){
$return = $this->attributes;
foreach($this->relatedRecords as $name => $records){
Expand Down

0 comments on commit bec2ccd

Please sign in to comment.