We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Save other data to model

I wanna write some data to a model and wanna read it later at some other position. The data isn't related to the table of the model but I'll need the data in a template.

Is there an existing way to do that? Like "$model->set('name', 'value')" and later "{{ model.name }}"

I hope you understand what I'm writing.

You can use:

$model->writeAttribute('name', value');


7.0k

Works fine, thank you!



7.0k
edited Jul '15

Meh, sorry for doublepost but atm I have the following problem: When I iterate through the model-objects and save some data (using Andres' method) and try to use the data later it doesn't work.

$users = \User::find();

foreach($users as $user) {
    $postCount = \Post::count(array(
        'user_id = ?0',
        'bind' => array($user->id)
    ));

    $user->writeAttribute('post_count', $postCount);
}

return $users;

When I'm now trying to read the attribute after the foreach like the following, I'll just get an error that "post_count" isn't known.

var_dump($users[0]->post_count);

What I am doing wrong?