Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Can't access fields through relationship

Hello everybody !

It is quite possible that I got something completely wrong, but I spent some time on google and didn't find any answer to my problem.

I have two models (User, Text). One User has many Text. I'm trying to access the User pseudonym through the Text. Here is my code :

<?php
// User model

use Phalcon\Mvc\Model;

class User extends Model
{
    /**
     * @var integer
     */
    protected $id;

    /**
     * @var string
     */
    protected $pseudonym;

    public function initialize()
    {

        $this->hasMany(
            "id",
            "Text",
            "user_id"
        );
    }

    // Here some accessors (including getPseudonym() )
}

// Text model

use Phalcon\Mvc\Model;

class Text extends Model
{
    /**
     * @var integer
     */
    protected $id;

    public function initialize()
    {
        $this->belongsTo(
            "user_id",
            "User",
            "id"
        );
    }

    // Here some accessors
}

// In the controller

public function indexAction()
{
    $blogArticle = Text::findFirst();
    $pseudonym = $blogArticle->user->pseudonym;
}

The 'pseudonym' variable on the last line is NULL whereas it should be a string.

Thank you for your time and help, and please forgive my English, for I am French...

edited Jul '17

You forgot alias:

$this->belongsTo(
            "user_id",
            "User",
            "id",
            ["alias" => "user"]
        );

Though you should have exception here already, so not sure what's going on and why you have null, maybe it's really null value?

edited Jul '17

Thank you for your answer. I just tried to add the alias, but it didn't work... Well in my database, the Text I'm calling is supposedly linked to the first User, whose pseudonym is the string "Richard"

Then maybe try to clear your metadata if you use any cache adapter for it?

I'm sorry, I have no idea what you are talking about. I don't know how to clear my metadata, and I don't know what a cache adapter is.

Then idk, without more information can't help you right now. I have similar code in my app, works without any problem.

Well thank you for your help. I'll continue to search ;)

Hi @richardalmee can you check this

var_dump(Text::findFirst());
var_dump(Text::findFirst()->user);
var_dump(Text::findFirst()->user->id);

What is show?

Aouch ! I can't copy-paste it all here but the last line shows :

Notice: Undefined property: Phalcon\Mvc\Model\Resultset\Simple::$id in /var/www/html/phalcon/encresnoires/app/controllers/BlogController.php on line 27 NULL



99.6k
Accepted
answer
edited Jul '17

You must have wrong relations or you access it somehow wrong. You didn't post correct code from your models. You have hasMany relation in your Text model(which is causing to return Phalcon\Mvc\Model\Resultset\Simple), or you tried to access Text from User model.

edited Jul '17

Thank you !!! I found the problem. I did have some other relationships involving those two models, (manyToMany) which I didn't think were relevant to copy paste for you in the first place. Commenting them solved my problem. You guys are awesome ! :D

Most likely you just need aliases, just use aliases always for relations, it will save you time next time.