src/PicORM/Model.php
private function _setRelation(array $configRelation, $callArgs)
    {
        $isSaved = false;
        switch ($configRelation['typeRelation']) {
            case self::ONE_TO_ONE:
                if ($callArgs[0] instanceof $configRelation['classRelation']) {
                    // if relation model is new, we need to store it before
                    if ($callArgs[0]->isNew()) {
                        $callArgs[0]->save();
                    }

                    // set model field corresponding to the relation
                    $this->{$configRelation['sourceField']} = $callArgs[0]->{$configRelation['targetField']};

                    // if we have autoget field, grab and store them inside model
                    foreach ($configRelation['autoGetFields'] as $oneField) {
                        $this->{$oneField} = $callArgs[0]->{$oneField};
                    }
                    $isSaved = true;
                }
                break;
            case self::ONE_TO_MANY:
                if (is_array($callArgs[0])) {
                    // foreach models in relation
                    foreach ($callArgs[0] as $oneRelationModel) {
                        // set model field corresponding to the relation
                        $oneRelationModel->{$configRelation['targetField']} = $this->{$configRelation['sourceField']};
                        $oneRelationModel->save();
                    }
                    $isSaved = true;
                }
                break;
            case self::MANY_TO_MANY:
                // assure that we have an array of model
                if (!is_array($callArgs[0])) {
                    $callArgs[0] = array($callArgs[0]);
                }

                // prepare a query for testing if relation already exists
                $testQueryHelper = new InternalQueryHelper();
                $testQueryHelper->select('count(*) as nb')->from("`" . $configRelation['relationTable'] . "`")
                                ->where("`" . $configRelation['sourceField'] . "`", '=', '?')
                                ->where("`" . $configRelation['targetField'] . "`", '=', '?');
                $testQuery = static::$_dataSource->prepare($testQueryHelper->buildQuery());

                // prepare a query to insert relation between models
                $insertQuery = new InternalQueryHelper();
                $insertQuery->insertInto("`" . $configRelation['relationTable'] . "`")
                            ->values($configRelation['sourceField'], "?")
                            ->values($configRelation['targetField'], "?");
                $insertQuery = static::$_dataSource->prepare($insertQuery->buildQuery());

                // for each model in relation
                foreach ($callArgs[0] as $oneRelationModel) {
                    // if model is new, we need to store it before setting relation
                    if ($oneRelationModel->isNew()) {
                        $oneRelationModel->save();
                    } else {
                        // test if relation already exists between the two models
                        $testQuery->execute(
                                  array(
                                      $this->{$configRelation['sourceField']},
                                      $oneRelationModel->{$configRelation['targetField']}
                                  )
                        );
                        $errorcode = $testQuery->errorInfo();
                        if ($errorcode[0] != "00000") {
                            throw new Exception($errorcode[2]);
                        }
                        $testResult = $testQuery->fetch(\PDO::FETCH_ASSOC);

                        // relation already set
                        if ($testResult['nb'] > 0) {
                            continue;
                        }
                    }

                    // create link in relation table
                    $insertQuery->execute(
                                array(
                                    $this->{$configRelation['sourceField']},
                                    $oneRelationModel->{$configRelation['targetField']}
                                )
                    );
                    $errorcode = $insertQuery->errorInfo();

                    // check if data is stored with no error
                    if ($errorcode[0] != "00000") {
                        throw new Exception($errorcode[2]);
                    }

                    $isSaved = true;
                }
                break;
        }

        return $isSaved;
    }
private function _setRelation(array $configRelation, $callArgs)
    {
        $isSaved = false;
        switch ($configRelation['typeRelation']) {
            case self::ONE_TO_ONE:
                if ($callArgs[0] instanceof $configRelation['classRelation']) {
                    // if relation model is new, we need to store it before
                    if ($callArgs[0]->isNew()) {
                        $callArgs[0]->save();
                    }

                    // set model field corresponding to the relation
                    $this->{$configRelation['sourceField']} = $callArgs[0]->{$configRelation['targetField']};

                    // if we have autoget field, grab and store them inside model
                    foreach ($configRelation['autoGetFields'] as $oneField) {
                        $this->{$oneField} = $callArgs[0]->{$oneField};
                    }
                    $isSaved = true;
                }
                break;
            case self::ONE_TO_MANY:
                if (is_array($callArgs[0])) {
                    // foreach models in relation
                    foreach ($callArgs[0] as $oneRelationModel) {
                        // set model field corresponding to the relation
                        $oneRelationModel->{$configRelation['targetField']} = $this->{$configRelation['sourceField']};
                        $oneRelationModel->save();
                    }
                    $isSaved = true;
                }
                break;
            case self::MANY_TO_MANY:
                // assure that we have an array of model
                if (!is_array($callArgs[0])) {
                    $callArgs[0] = array($callArgs[0]);
                }

                // prepare a query for testing if relation already exists
                $testQueryHelper = new InternalQueryHelper();
                $testQueryHelper->select('count(*) as nb')->from("`" . $configRelation['relationTable'] . "`")
                                ->where("`" . $configRelation['sourceField'] . "`", '=', '?')
                                ->where("`" . $configRelation['targetField'] . "`", '=', '?');
                $testQuery = static::$_dataSource->prepare($testQueryHelper->buildQuery());

                // prepare a query to insert relation between models
                $insertQuery = new InternalQueryHelper();
                $insertQuery->insertInto("`" . $configRelation['relationTable'] . "`")
                            ->values($configRelation['sourceField'], "?")
                            ->values($configRelation['targetField'], "?");
                $insertQuery = static::$_dataSource->prepare($insertQuery->buildQuery());

                // for each model in relation
                foreach ($callArgs[0] as $oneRelationModel) {
                    // if model is new, we need to store it before setting relation
                    if ($oneRelationModel->isNew()) {
                        $oneRelationModel->save();
                    } else {
                        // test if relation already exists between the two models
                        $testQuery->execute(
                                  array(
                                      $this->{$configRelation['sourceField']},
                                      $oneRelationModel->{$configRelation['targetField']}
                                  )
                        );
                        $errorcode = $testQuery->errorInfo();
                        if ($errorcode[0] != "00000") {
                            throw new Exception($errorcode[2]);
                        }
                        $testResult = $testQuery->fetch(\PDO::FETCH_ASSOC);

                        // relation already set
                        if ($testResult['nb'] > 0) {
                            continue;
                        }
                    }

                    // create link in relation table
                    $insertQuery->execute(
                                array(
                                    $this->{$configRelation['sourceField']},
                                    $oneRelationModel->{$configRelation['targetField']}
                                )
                    );
                    $errorcode = $insertQuery->errorInfo();

                    // check if data is stored with no error
                    if ($errorcode[0] != "00000") {
                        throw new Exception($errorcode[2]);
                    }

                    $isSaved = true;
                }
                break;
        }

        return $isSaved;
    }
Set a relation value from magic setter

@param array $configRelation - Relation configuration
@param array $callArgs       - Models instance

@throws Exception

@return bool