Warning: Undefined variable $generator in /var/www/html/prod/vendor/engmohamedamer/yii2-enhanced-gii/model/default/traits.php on line 19
Warning: Attempt to read property "nsTrait" on null in /var/www/html/prod/vendor/engmohamedamer/yii2-enhanced-gii/model/default/traits.php on line 19
;
use Yii;
/**
* This is the trait for all models ?>".
*
Warning: Undefined variable $tableSchema in /var/www/html/prod/vendor/engmohamedamer/yii2-enhanced-gii/model/default/traits.php on line 26
Warning: Attempt to read property "columns" on null in /var/www/html/prod/vendor/engmohamedamer/yii2-enhanced-gii/model/default/traits.php on line 26
Warning: foreach() argument must be of type array|object, null given in /var/www/html/prod/vendor/engmohamedamer/yii2-enhanced-gii/model/default/traits.php on line 26
*/
trait Relation
{
public function loadWithRelation($POST) {
if ($this->load($POST)) {
foreach ($this->relations() as $name => $relation) {
if (isset($POST[$relation[2]])) {
$container = [];
$relationPKAttr = $relation[1]::getTableSchema()->primaryKey[0];
foreach ($POST[$relation[2]] as $rModel) {
$tmp = (!empty($rModel[$relationPKAttr])) ? $relation[1]::findOne($rModel[$relationPKAttr]) : new $relation[1];
$tmp->load([$relation[2] => $rModel]);
$container[] = $tmp;
}
$this->populateRelation($name, $container);
}
}
return true;
}
return false;
}
public function saveWithRelation() {
$db = $this->getDb();
$trans = $db->beginTransaction();
try {
$error = 0;
//save parent
if ($this->save()) {
foreach ($this->relations() as $name => $relation) {
$relationPKAttr = $relation[1]::getTableSchema()->primaryKey[0];
$ids = [];
$deleteString = [];
foreach ($this->$name as $rModel) {
//if hasMany
if ($relation[0]) {
//set child row Foreign Key to parent PK
foreach ($relation[3] as $relFK => $id) {
if (array_key_exists($relFK, $rModel->attributes)) {
$rModel->$relFK = $this->$id;
$deleteString[] = " $relFK = '".$this->$id."'";
}
}
//try to save child row, if not success add error to parent class & set $error to true
if (!$rModel->save()) {
foreach ($rModel->errors as $error) {
foreach ($error as $value) {
$this->addError($relation[2] . " : " . $value);
}
}
$error = 1;
}
//get the deleted child row ID by users
if (!empty($rModel->$relationPKAttr)) {
$ids[] = $rModel->$relationPKAttr;
}
}
}
//delete the deleted child row ID by users
if (!empty($ids)) {
$ids = implode(', ', $ids);
$deleteString = implode("AND ", $deleteString);
print_r($relationPKAttr . " NOT IN('$ids') AND $deleteString");
$relation[1]::deleteAll($relationPKAttr . " NOT IN('$ids') AND $deleteString");
}
}
} else {
$error = 1;
}
if ($error) {
$trans->rollback();
return false;
}
$trans->commit();
return true;
} catch (Exception $e) {
$trans->rollBack();
throw $e;
}
}
}