This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

So, after watch over and over the Extending the User plugin screencast I still not know how to save the new fields I added by extending the User model. Any time I try to save any data I get this error:

"Unexpected type of array, should attribute "profile" be jsonable?" on line 984 of /var/www/html/octproject.dev/vendor/october/rain/src/Database/Model.php

This is the Plugin.php file content:

<?php namespace OctProject\AMCProfile;

use System\Classes\PluginBase;
use RainLab\User\Models\User as UserModel;
use RainLab\User\Controllers\Users as UsersController;
use OctProject\AMCProfile\Models\AMCProfile as ProfileModel;

/**
 * AMCProfile Plugin Information File
 */
class Plugin extends PluginBase
{

    /**
     * Returns information about this plugin.
     *
     * @return array
     */
    public function pluginDetails()
    {
        return [
            'name'        => 'Octproject Profile plugin',
            'description' => 'Add extra fields for OctProject User Profiles by extends RainLab User',
            'author'      => 'DTS',
            'icon'        => 'icon-users'
        ];
    }

    public function boot()
    {
        UserModel::extend(function($model){
                $model->hasOne['amcprofile'] = ['OctProject\AMCProfile\Models\AMCProfile'];

                $model->fillable($model->getFillable() + [
                        "pin",
                        "pinRegeneratedDateTime",
                        "phone2",
                        "phone3",
                        "phone4"
                    ]);
            });

        UsersController::extendListColumns(function($list, $model) {
                if (!$model instanceof UserModel)
                    return;

                $list->addColumns([
                    'pin' => [
                        'label' => 'PIN Code'
                    ],
                    'pinRegeneratedDateTime' => [
                        'label' => 'PIN Generated On'
                    ]
                ]);
        });

        UsersController::extendFormFields(function ($form, $model, $context) {
                if (!$model instanceof UserModel)
                    return;

                if (!$model->exists)
                    return;

                // Ensures that a profile model always exists ...
                ProfileModel::getFromUser($model);

                $form->addTabFields([
                        'profile[pin]' => [
                            'label' => 'User PIN',
                            'tab' => 'Profile',
                            'type' => 'text'
                        ],
                        'profile[phone2]' => [
                            'label' => 'Teléfono(2)',
                            'tab' => 'Profile',
                            'type' => 'text'
                        ],
                        'profile[phone3]' => [
                            'label' => 'Teléfono(3)',
                            'tab' => 'Profile',
                            'type' => 'text'
                        ],
                        'profile[phone4]' => [
                            'label' => 'Teléfono(4)',
                            'tab' => 'Profile',
                            'type' => 'text'
                        ]
                    ]);
            });
    }

}

And this one is the code on the AMCProfile.php model:

<?php namespace OctProject\AMCProfile\Models;

use Model;

/**
 * AMCProfile Model
 */
class AMCProfile extends Model
{

    /**
     * @var string The database table used by the model.
     */
    public $table = 'amc_profiles';

    /**
     * @var array Guarded fields
     */
    protected $guarded = ['*'];

    /**
     * @var array Fillable fields
     */
    protected $fillable = [];

    /**
     * @var array Relations
     */
    public $belongsTo = [
        "user" => ["RainLab\User\Models\User"]
    ];

    public static function getFromUser($user)
    {
        if ($user->amcprofile)
            return $user->amcprofile;

        $amcprofile = new static;
        $amcprofile->user = $user;
        $amcprofile->save();

        $user->amcprofile = $amcprofile;

        return $amcprofile;
    }

}

What could be wrong there? Did I miss something? Any help? Advice? Ideas?

Note: Will be nice if @daftspunk can add more material to this screencast as for example, a fix for this issue, of course if it's a real issue and is not me messing up things in somewhere, since maybe others will make the same mistake as me and also how to show that fields on a User profile page not the update page, that's clear but I'll like to add a Profile page to my site where Users can see their own profiles and possible others profiles.

Your relation is called amcprofile and the code references profile. For example, change profile[pin] to amcprofile[pin].

Thanks @daftspunk did you see any other error on my code? When I save the object I get this error:

"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'amcprofile[pinRegeneratedDateTime]' in 'field list' (SQL: update users set updated_at = 2014-11-09 18:59:40, amcprofile[pinRegeneratedDateTime] = where id = 1)" on line 625 of /var/www/html/octproject.dev/vendor/laravel/framework/src/Illuminate/Database/Connection.php

Last updated

Also, can you write a small example around my note or at least point me in the right direction? How do I display the information stored in, for example, profile page through a component?

1-4 of 4

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.