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

Model relation with namespace

Hi,

I start a multi module application and I add namespaces. But, since I added namespace, my relation between object doesn't works anymore. When I load an object, children are no longer there. Is this a normal behaviour ?

Parent

<?php
namespace NearbyMe\Backend\Models;

class User extends \Phalcon\Mvc\Model {

    /**
     *
     * @var integer
     */
    public $ID;

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

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

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

    /**
     *
     * @var integer
     */
    public $UserStatusID;

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

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

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

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

    /**
     *
     * @var integer
     */
    public $AddressID;

    /**
     *
     * @var date
     */
    public $CreationDate;

    /**
     *
     * @var date
     */
    public $ModificationDate;

    /**
     * Initialize method for model.
     */
    public function initialize() {

        $this->setSource('User');

        $this->hasMany("ID", "NearbyMe\Backend\Models\UserRole", "UserID");
        $this->hasMany("ID", "NearbyMe\Backend\Models\UserModule", "UserID");
    }

    public function getMessages() {

        $messages = array();

        foreach (parent::getMessages() as $message) {

            switch ($message->getType()) {

                case 'InvalidCreateAttempt':
                    $messages[] = 'The record cannot be created because it already exists';
                    break;
                case 'InvalidUpdateAttempt':
                    $messages[] = 'The record cannot be updated because it already exists';
                    break;
                case 'PresenceOf':
                    $messages[] = 'The field ' . $message->getField() . ' is mandatory';
                    break;
            }
        }

        return $messages;
    }

    public function validation() {

        $this->validate(new Phalcon\Mvc\Model\Validator\Email(

            array(

                "field" => "Email",
                "message" => "InvalidEmailAddress"
            )
        ));

        /*$this->validate(new Uniqueness(
            array(

                "field" => "MainPhoneNumber",
                "message" => "UniquePhoneNumber"
            )
        ));*/

        $isValid = $this->validationHasFailed() != true;
        return $isValid;
    }

    function columnMap() {

        return array(

            'ID' => 'ID',
            'AddressID' => 'AddressID',
            'Email' => 'Email',
            'Firstname' => 'Firstname',
            'Lastname' => 'Lastname',
            'UserStatusID' => 'UserStatusID',
            'Password' => 'Password',
            'UserTypeID' => 'UserTypeID',
            'MainPhone' => 'MainPhone',
            'OtherPhone' => 'OtherPhone',
            'CreationDate' => 'CreationDate',
            'ModificationDate' => 'ModificationDate'
        );
    }

}

Children

<?php
namespace NearbyMe\Backend\Models;

class UserRole extends \Phalcon\Mvc\Model {

    /**
     *
     * @var integer
     */
    public $ID;

    /**
     *
     * @var integer
     */
    public $UserID;

    /**
     *
     * @var integer
     */
    public $RoleID;

    /**
     * Initialize method for model.
     */
    public function initialize() {

        $this->setSource('UserRole');

        $this->belongsTo("UserID", "NearbyMe\Backend\Models\User", "ID");
        $this->belongsTo("RoleID", "NearbyMe\Backend\Models\Role", "ID");
    }

    /**
     * Independent Column Mapping.
     */
    public function columnMap() {

        return array(

            'ID' => 'ID',
            'UserID' => 'UserID',
            'RoleID' => 'RoleID'
        );
    }
}
<?php

namespace NearbyMe\Backend\Models;

class Role extends \Phalcon\Mvc\Model {

    /**
     *
     * @var integer
     */
    public $ID;

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

    /**
     * Initialize method for model.
     */
    public function initialize() {

        $this->setSource('Role');
        $this->hasMany("ID", "NearbyMe\Backend\Models\UserRole", "RoleID");
    }

    /**
     * Independent Column Mapping.
     */
    public function columnMap() {

        return array(

            'ID' => 'ID',
            'RoleName' => 'RoleName'
        );
    }

    /*public function afterFetch() {

        $translate = $this->_dependencyInjector->get('translate');

        //Translate
        $this->RoleName = utf8_encode($translate->_($this->RoleName));
    }*/
}

The line that cause the error : $userRole = $user->userrole;

Thank you !



98.9k

It seems it must be:

$role->userrole; instead of $user->userrole;



31.3k

No, I want the roles that belongs to the user.