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

Utility
Utility

Same context as http://octobercms.com/forum/post/eloquent-orm-question

Extended Rainlab\Blog\Models\Post to ExtendPost model, using post_id as primary key.

Extended Rainlab\User\Models\User to ExtendUser mode, using user_id as primary key.

There is ManyToMany relation between ExtendPost and ExtendUser (yes, not hasMany in my case).

The 1st problem is:

I render extendposts of a user a ExtendUser's form view by $this->relationRender('extendposts'). Then when I click Add ExtendPost button, it shows

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'field list' (SQL: select id from ut_ut_extendposts inner join ut_ut_extendusers_extendposts on ut_ut_extendposts.post_id = ut_ut_extendusers_extendposts.post_id where ut_ut_extendusers_extendposts.user_id = 1)"

It seems that even if I add $primaryKey = 'post_id' to ExtendPost model and all the relation declaration, it still generates SELECT 'id'.

The 2nd problem is:

In columns.yaml of ExtendUser, I can no long use 'select'

 extendposts:
     label: posts
     relation: extendposts
     select: @title

Last updated

Flynsarmy
Flynsarmy

For your second problem: As per the docs select: title should be nameColumn: title for relation fields.

Last updated

Utility
Utility

But nameColumn: title is for Form (fields.yaml), not List (columns.yaml)

Flynsarmy
Flynsarmy

Oh, if you're using lists try select: @title.

Last updated

Utility
Utility

Yes I did. But it did not work for an extended model. Sorry I missed @ in the post.

Utility
Utility

The 2nd problem is solved using the same method in http://octobercms.com/forum/post/how-can-i-show-image-attachment-on-list

That is, adding to Extendpost model this method:

getExtendpostsAttribute()

The 1st problem still exists ...

Last updated

Utility
Utility

I think the 1st problem is because in modules/backend/behaviors/RelationController.php, it assumes 'id' as the primary. For example, changing the following solved my problem:

 /**
 * Returns the existing record IDs for the relation.
 */
private function findExistingRelationIds($checkIds = null)
{
    $results = $this->relationObject
        ->getBaseQuery()
        ->select($this->relationModel->table . '.' . $this->relationModel->primaryKey); 
        // ->select('id');

    if ($checkIds !== null && is_array($checkIds) && count($checkIds))
        $results = $results->whereIn($this->relationModel->table . '.' . $this->relationModel->primaryKey, $checkIds);
        // $results = $results->whereIn('id', $checkIds);

    return $results->lists($this->relationModel->primaryKey);
    // return $results->lists('id');
}

It lists something, at least ... But:

if there is no attached models, the list gives all candidated models for selecting to attach.

If there is already an attached model, the list gives only that attached model. So there is no way to attach other models ...

And onRelationManageAdd() does not work ...

Last updated

Utility
Utility

I have added this as #issue500

1-8 of 8

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