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

michael17887
michael17887

Hi there

i make a plugin with builder. almost everything works, but with relations there goes something wrong. my model

 <?php namespace Michael\Diesel\Models;

use Model;

/**
 * Model
 */
class Eigen extends Model
{
    use \October\Rain\Database\Traits\Validation;

    use \October\Rain\Database\Traits\SoftDelete;

    protected $dates = ['deleted_at'];

    /**
     * @var string The database table used by the model.
     */
    public $table = 'michael_diesel_eigen';
//     public $belongsTo = [
//     'kenteken' => [Acme\Blog\Models\Auto::class, 'key' => 'monteur_id', 'otherKey' => 'monteur_id']
// ];
  public $hasOne  =[
      //   'kenteken' => [\michael\Diesel\Models\Auto::class, 'key' => 'kenteken']
         'monteur_id' => [Monteur::class, 'key' => 'monteur_id']
        ];  
        // 'brand' =>[
        //     'michael\Diesel\Models\Auto',
        //     'table' => 'michael_diesel_autos'
        // ], 

   // ];
    /**
     * @var array Validation rules
     */
    public $rules = [
    ];
}

But i get a error that he want to save it in the wrong table

 "SQLSTATE[42S22]: Column not found: 1054 Unknown column 
'michael_diesel_monteurs.monteur_id' in 
'where clause' (SQL: 
update 

`michael_diesel_monteurs` 
set `monteur_id` = , 
`updated_at` = 2021-07-23 08:33:05 
where 

`michael_diesel_monteurs`.`monteur_id` = 130 and 
`michael_diesel_monteurs`.`monteur_id` is not null and 
`michael_diesel_monteurs`.`deleted_at` is null)"

why he want to save it to the monteurs table. ? with this line : public $table = 'michael_diesel_eigen';

Last updated

daftspunky
daftspunky

Hi Michael!

This is because hasOne will refer to the related table, not the parent table. It sounds like the belongsTo relationship is what you are looking for...

michael17887
michael17887

than i get the error Model 'Michael\Diesel\Models\Eigen' bevat geen definitie voor 'monteur_id'.

public $BelongsTo =[ 'monteur_id' => [Monteur::class, 'table' => 'michael_diesel_monteurs', 'key' => 'monteur_id'] ];

Last updated

michael17887
michael17887

i have now in my Eigen model

public $hasOne =[ 'kenteken' => [\michael\Diesel\Models\Auto::class, 'key' => 'kenteken'], 'monteur' => [Monteur::class, 'table' => 'michael_diesel_monteurs', 'key' => 'id'], 'naam' => [Monteur::class, 'table' => 'michael_diesel_monteurs', 'key' => 'naam'] ];

now i get a list with monteurs in my form field. but when i save he save it in the table monteurs. why?

i have in my Eigen form a relation with name monteur and by relation columname monteur.

what do i wrong?

1-4 of 4

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