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

Not save model relation

Model User:

<?php
use Phalcon\Mvc\Model;

class Users extends Model
{  
      public function initialize()
     {
          $this->hasOne('id','Profil','owner');
    }   
}

Model Profil:

<?php
 use Phalcon\Mvc\Model;

 class Profil extends Model
 {  
      public function initialize()
      {
          $this->hasOne('owner','Users','id');
      }   
 }

and

 $user = Users::findFirst(1);
 $profil = $user->getProfil();
 $proifl->text = 'blablalbla';
 $profil->save();
$profil->save(); OR $user->save();

Not saving...

What does this mean?

$profil->$user->getProfil();


1.3k

Misspelling

edited Jan '15

Misspelling

<?php

class Session extends \Phalcon\Mvc\Model
{
    protected $id;
    protected $user_id;
    protected $user_agent;

    public function initialize()
    {
        $this->belongsTo('user_id', __NAMESPACE__ . '\User', 'id', [
            'alias'      => 'User',
            'foreignKey' => true
        ]);
    }

    public function getId()
    {
        return $this->id;
    }

    public function getUserAgent()
    {
        return $this->user_agent;
    }

    public function setUserAgent($user_agent)
    {
        $this->user_agent = $user_agent;

        return $this;
    }
}

class User extends \Phalcon\Mvc\Model
{
    protected $id;

    public function initialize()
    {
        $this->hasMany('id', __NAMESPACE__ . '\Session', 'user_id', [
            'alias' => 'Sessions',
            'foreignKey' => ['action' => Relation::ACTION_CASCADE]
        ]);
    }

    public function getId()
    {
        return $this->id;
    }
}

class DevController extends \Phalcon\Mvc\Controller
{
    public function indexAction()
    {
        $model = User::findFirst(1);

        // \Phalcon\Mvc\Model\Resultset\Simple
        $sessions  = $model->Sessions;

        // Session model
        $session = $sessions->getLast();

        //  "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:36.0) Gecko/20100101 Firefox/36.0"
        echo $session->getUserAgent();

        $session->setUserAgent('foo bar');

        $session->save();

        // "foo bar"
        echo $session->getUserAgent();
    }
}

You can see what exactly $user->getProfil() returned directly in controller

echo(print_r($user->getProfil(), true));
die;


1.3k

when

hasMany

throws error

Undefined property: Phalcon\Mvc\Model\Resultset\Simple
edited Jan '15

I showed you an working example that just checked in 1.3.4 and 2.0.0 BETA3



1.3k

I have versions 1,3,4

Your DB tables are named profil and users ?

Can you try please get related model with alias, like in my example?

yes



1.3k

When I do so:

$profil = Profil::findFirst('owner=1');
$profil->text = 'blabla';
$profil->save();

not save...

Structur table profil

owner = unique_key text = text

edited Jan '15
<?php

use Phalcon\Mvc\Model;

class Users extends Model
{  
    public function initialize()
    {
        $this->hasOne('id', __NAMESPACE__.'\Profil', 'owner');
    }
}
<?php
use Phalcon\Mvc\Model;

class Profil extends Model
{  
    public function initialize()
    {
        $this->belongsTo('owner', __NAMESPACE__ . '\Users', 'id');
    }
}

Note, Profil belongs to, and Users has one.

Also try with __NAMESPACE__, and be sure that Profil has owner field for public acces and Users has id for public access

edited Jan '15

Profil have public or protected text ?

When I do so:

$profil = Profil::findFirst('owner=1');
$profil->text = 'blabla';
$profil->save();

not save...

Structur table profil

owner = unique_key text = text

Can you try this?

        $profil = Profil::findFirst('owner=1');
        $profil->text = 'blabla';

        if (!$profil->save()) {
            foreach ($profil->getMessages() as $message) {
                echo(print_r($message, 1));
            }

            die;
        }


1.3k

helped to remove and re-create the table.

Thanks Serghei !



39.2k
Accepted
answer

So, if issue is resolved, can you mark it as solved please?