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

don't understand Relationship Models

Hi,

PS : how can i add code block ? this view is terrible for you i know....

i try to use Relationship models, i haven't error but i don't know how it's work. (sorry for my bad english.

here my models

class Composant extends Model
{
    protected $ID_Composant;
    protected $ID_Section;
    protected $ID_Montant;
    protected $ID_Remplissage;

    public function initialize()
    {       
        $this->belongsTo(
            'ID_Section',
            'Section',
            'ID_Section'
        );

        $this->belongsTo(
            'ID_Montant',
            'Montant',
            'ID_Montant'
        );

        $this->belongsTo(
            'ID_Remplissage',
            'Remplissage',
            'ID_Remplissage'
        );
    }

    public function __get($name)
    {
        if (property_exists($this,$name))
        {
            return $this->$name;
        }
    }

    public function __set($name, $value)
    {
        if (property_exists($this,$name))
        {
            $this->$name = $value;
        }
    }
}
class Remplissage extends Model
{
    protected $ID_Remplissage;
    protected $Remplissage;
    protected $Visserie;
    protected $Panneaux;
    protected $PrixHT;
    protected $PrixTTC;

    public function initialize()
    {
        $this->hasMany(
            'ID_Remplissage',
            'Composant',
            'ID_Remplissage'
        );
    }

    public function __get($name)
    {
        if (property_exists($this,$name))
        {
            return $this->$name;
        }
    }

    public function __set($name, $value)
    {
        if (property_exists($this,$name))
        {
            $this->$name = $value;
        }
    }
}
class Montant extends Model
{
    protected $ID_Montant;
    protected $Hauteur;
    protected $Largeur;
    protected $Epaisseur;
    protected $PrixHT;
    protected $PrixTTC;

    public function initialize()
    {
        $this->hasMany(
            'ID_Montant',
            'Composant',
            'ID_Montant'
        );
    }

    public function __get($name)
    {
        if (property_exists($this,$name))
        {
            return $this->$name;
        }
    }

    public function __set($name, $value)
    {
        if (property_exists($this,$name))
        {
            $this->$name = $value;
        }
    }
}
class Section extends Model
{
    protected $ID_Section;
    protected $Lisses;
    protected $Contrefort;
    protected $SabotAssemblage;
    protected $Goujons;
    protected $Supports;
    protected $PrixHT;
    protected $PrixTTC;

    public function initialize()
    {
        $this->hasMany(
            'ID_Section',
            'Composant',
            'ID_Section'
        );
    }

    public function __get($name)
    {
        if (property_exists($this,$name))
        {
            return $this->$name;
        }
    }

    public function __set($name, $value)
    {
        if (property_exists($this,$name))
        {
            $this->$name = $value;
        }
    }
}

And my controller :

$composant = new Composant();

        $result = $composant->findFirst();

        $montant = $result->Montant;

$result has all ID to 1 (it's normal) but $montant is equal to NULL, don't know why. Can you explain me ?



10.1k

Can you remove $ in the has many definition so

   $this->hasMany(
            '$ID_Section',
            'Composant',
            '$ID_Section'
     );

should be

   $this->hasMany(
            'ID_Section',
            'Composant',
            'ID_Section'
     );

Besides that. How does you DB look? Is there data in there that matches the join?



4.2k
edited Jan '19

Thx i changed.

My BD looks like at :

https://i.goopics.net/Py2eQ.png

Same order than models. So data matches.

But $result->Montant is NULL



4.2k

It's work with add alias in relationship

like this :

$this->belongsTo(
            'ID_Section',
            'Models\Base\Section',
            'ID_Section',
            ['alias' => 'section']
        );

and call with

$montant = $result->getRelated('montant');

but why i don't use $result->montant ? like doc ? need loader or service ?



145.0k
Accepted
answer
edited Jan '19

Because you added your own magic properties and getters.



10.1k
edited Jan '19

Grrr.... that I missed that... Thnx @Jurigag