Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Fatal error happened when saving related records #2319

Closed
AlloVince opened this issue Apr 10, 2014 · 3 comments
Closed

[BUG] Fatal error happened when saving related records #2319

AlloVince opened this issue Apr 10, 2014 · 3 comments
Labels
bug A bug report status: medium Medium

Comments

@AlloVince
Copy link
Contributor

Hi guys

I would like to save related records for two tables: Tags and Texts, text is belongs to tags by one to one relation. here is test code:

$di = new \Phalcon\DI\FactoryDefault();
$di->set('db', function(){
    return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
        "host" => "localhost",
        "username" => "root",
        "password" => "",
        "dbname" => "scrapy"
    ));
});


class Tag extends Phalcon\Mvc\Model
{
    /**
    *
    * @var integer
    */
    public $id;

    /**
    *
     * @var string
     */
     public $tagName;


    public function getSource()
    {
        return "eva_blog_tags";
    }

    public function initialize()
    {
        $this->hasOne('id', 'Texts', 'post_id', array(
            'alias' => 'Text'
        ));
    }

}

class Text extends Phalcon\Mvc\Model
{
    /**
    *
    * @var integer
    */
    public $post_id;

    /**
    *
     * @var string
     */
     public $content;


    public function getSource()
    {
        return "eva_blog_texts";
    }

}

$tag = new Tag();
$text = new Text();
$tag->Text = $text;

$_POST = array(
    'tagName' => 'foo',
    'Text' => array(
        'content' => 'bar'
    ),
);
/*
//should add below:
$text->assign($_POST['Text']);
unset($_POST['Text']);
*/

$tag->assign($_POST);
$tag->save();

By above code, phalcon will caused a Fatal error:

Fatal error: Phalcon\Mvc\Model::_postSaveRelatedRecords(): Call to method writeattribute() on a non object in /opt/htdocs/phalcon_bugs/model_save_by_sub_array/test.php on line 85

Please notice that I commented few lines, if uncomment these line the demo will be correct. However, I think if input data not correct, phalcon should throw an exception but not caused a Fatal error.

Best regards

@dreamsxin
Copy link
Contributor

The data Text is overwrite model's Relation Text.

$_POST = array(
    'tagName' => 'foo',
    'Text' => array(
        'content' => 'bar'
    ),
);

I fix it.

phalcon pushed a commit that referenced this issue Apr 12, 2014
Fix #2319 In `Model::_postSaveRelatedRecords` add check whether value is object
@dreamsxin
Copy link
Contributor

Can you test it again?

@AlloVince
Copy link
Contributor Author

As my test this issue have been fixed, I hope v1.3.2 could release soon. Thank you.

@niden niden added bug A bug report status: medium Medium and removed Bug - Medium labels Dec 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug report status: medium Medium
Projects
None yet
Development

No branches or pull requests

3 participants