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

that0n3guy
that0n3guy

Anyone able to get these to work? If so, do you have some example code?

that0n3guy
that0n3guy

Just an FYI, at the time of posting, there was no polymorphic support in october... but now there is.

You set this up like so in your models:

Quick explanation of the code below

I have 2 models, contact and ocalist. This allows me to create lists and relate contacts to those lists.

Contact model:

<?php namespace OCA\Blasts\Models;

use Model;

/**
 * Contact Model
 */
class Contact extends Model
{

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

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

    /**
     * @var array Fillable fields
     */
    protected $fillable = ['firstname','lastname','name','email','phone'];

    /**
     * @var array Validation rules
     */
    public $rules = ['phone'=>'required'];

    /**
     * @var array Relations
     */
    public $morphToMany = [
        'lists' => ['OCA\Blasts\Models\Ocalist', 'table' => 'oca_blasts_ocalistables', 'name'=>'ocalistable'],  // @todo need timestamps
    ];
}

Lists model

<?php namespace OCA\Blasts\Models;

use Model;

/**
 * Ocalist Model
 */
class Ocalist extends Model
{

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

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

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

    /**
     * @var array Validation rules
     */
    public $rules = ['name'=>'required'];

    /**
     * @var array Relations
     */
    public $morphedByMany = [
        'contacts' => ['OCA\Blasts\Models\Contact', 'table' => 'oca_blasts_ocalistables', 'name'=>'ocalistable'],
    ];
}

Database "updates" files

Contacts:

<?php namespace OCA\Blasts\Updates;

use Schema;
use October\Rain\Database\Updates\Migration;

class CreateContactsTable extends Migration
{

    public function up()
    {
        Schema::create('oca_blasts_contacts', function($table)
        {
            $table->engine = 'InnoDB';
            $table->increments('id');
            $table->string('firstname');
            $table->string('lastname');
            $table->string('name');
            $table->string('email');
            $table->string('phone');
            $table->boolean('active');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('oca_blasts_contacts');
    }

}

lists:

<?php namespace OCA\Blasts\Updates;

use Schema;
use October\Rain\Database\Updates\Migration;

class CreateOcalistsTable extends Migration
{

    public function up()
    {
        Schema::create('oca_blasts_ocalists', function($table)
        {
            $table->engine = 'InnoDB';
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        });

        Schema::create('oca_blasts_ocalistables', function($table)
        {
            $table->engine = 'InnoDB';
            $table->integer('ocalist_id');
            $table->integer('ocalistable_id');
            $table->string('ocalistable_type');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('oca_blasts_ocalists');
        Schema::drop('oca_blasts_ocalistables');
    }

}
deroccha
deroccha

I just try to make the same thing, building a many to many relation and render this datas with config_relation.yaml here is an image about the relations between tables

and I try to get all those platforms where the shop is realted to theme and show the folloing columns platform_name,domain, status. I've been trying from your example like

Shops Model

public $morphToMany = [
        'platforms' => ['Kakuki\TdTracker\Models\Platform', 'table' => 'kakuki_tdtracker_shop_wf', 'name'=>'platfroms'],  // @todo need timestamps
    ];

and in my Platform Model

public $morphedByMany = [
        'shops' => ['Kakuki\TdTracker\Models\Shops', 'table' => 'kakuki_tdtracker_shop_wf', 'name'=>'platfroms'],
    ];

but I get Undefined variable: mode

Last updated

1-3 of 3

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