nextras / orm

Orm with clean object design, smart relationship loading and powerful collections.
https://nextras.org/orm
MIT License
310 stars 57 forks source link

'array_combine() expects parameter 2 to be array, int given' when fetching collection with composite primary key #448

Closed Hologos closed 4 years ago

Hologos commented 4 years ago

Describe the bug

I get _TypeError: array_combine() expects parameter 2 to be array, int given_ when fetching collection with composite primary key.

Trace file

To Reproduce

<?php

declare(strict_types=1);

namespace App\Model;

use Nextras\Orm\Entity\Entity;
use Nextras\Orm\Relationships\ManyHasMany;

/**
 * @property int                      $id                    {primary}
 * @property string                   $email
 * @property ManyHasMany|Benefit[]    $benefits              {m:m Benefit::$customers, isMain=true}
 */
class Customer extends Entity
{
}

/**
 * @property int                      $id          {primary-proxy}
 * @property int                      $category    {enum self::CATEGORY_*} {primary}
 * @property int                      $type        {enum self::TYPE_*} {primary}
 * @property string                   $name
 * @property ManyHasMany|Customer[]   $customers   {m:m Customer::$benefits}
 */
abstract class Benefit extends Entity
{
    const CATEGORY_DAILY_MENU_ITEM_PRICE = 2;

    const TYPE_ALL_DISHES_FOR_FREE = 2;
}

foreach($this->customer->benefits as $benefit)
{
    \Tracy\Debugger::dump($benefit->category .' '. $benefit->type);
}

Expected behavior Be able to fetch collection.

Versions::

hrach commented 4 years ago

This is not a bug, but unsupported feature, not currently planned. Orm does not support multi-column foreign keys.

If you need only one combination of category & type, put there an artificial auto-increment id and make category & type an unique key.

Hologos commented 4 years ago

The problem is that I need combination of customer_id + category + type to be unique. I will have to do it using two 1:m relationships as you proposed on slack.

hrach commented 4 years ago

If category+type is unique, than id of this row is unique. Thank the connection table for m:m can be has unique entries to, since customer_id and benefit_id will be part of PK.