Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Multiple Models for EAV Containers?
  • Hi,

    I would like to use two Relations for EAV Containers.

    Having a client and for this client I would like to store Settings and Metadata. Settings are stored in Strings and Metadata in Textareas. Therefore I used two different tables.

    When I try to store Metadata they store themselves in the table of the Settings.

    My Models:
    http://bin.fuelphp.com/snippet/view/xZ

    My Controller:
    http://bin.fuelphp.com/snippet/view/x0

    How can I tell the code in which model I should store the EAV data?
    Or is it possible to decide per key if I would like to store it in a varchar or text field and have two different fields for my values in the database table?

    Thanks for your kind help
    Kay / Maven88

  • HarroHarro
    Accepted Answer
    You can not.

    EAV supports multiple relations for read and update, but will always create in the first one defined. Simply because

    $model->eavkey = $value;

    doesn't allow you to specify something extra.

    But since under the hood EAV are normal relations, you can simply add it the way you would any other related object:

    $model->metadata[] = Model_Metadata::forge($data);
  • Hey Harro,
    thanks for the quick feedback.

    You mean something like this:
    http://bin.fuelphp.com/snippet/view/x1

    If I do that I'll get an error:
    Fuel\Core\Database_Exception [ 23000 ]:
    SQLSTATE[23000]: Integrity
    constraint violation: 1048 Column 'key' cannot be null with query:
    "INSERT INTO `meta` (`client_id`, `key`, `value`, `created_at`,
    `updated_at`) VALUES ('76990864', null, null, 1390425676, 1390425676)"

    How can I set the correctvalues for $key and $value in this scenario?

    Thanks
    Kay / Maven88
  • Hi, thanks that is working.
    But it always saves all entries new into the database.
    How can I only "update" the keys once set?
  • Check if the property exists first? Something like:

    try
    {
        if ($model->key)
        {
            $model->key = $value;
        }
    }
    catch (\OutOfBoundsException $e)
    {
        // forge the new key/value here
    }

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion