8.4.0 How to add custom attribute during install and set its value?

Permalink
I install a custom attribute in a category:
$akc = AttributeCategory::getByHandle('car');
        if (!is_object($akc)) {
            $akc = AttributeCategory::add('car', AttributeCategory::ASET_ALLOW_SINGLE, $pkg);
        }
        $set = $this->addCarAttributeSet('car', 'car_options', 'Car Options', $pkg);
        $select = AttributeType::getByHandle('select');
        $this->installCarAttribute('gear_box', $select, $pkg, $set, array(
            'akHandle' => 'gear_box',
            'akName' => t('Gear Box'),
            'akIsSearchable' => true,
        ));
    }

But how can I set the attribute value during its installation?

Thank you.

linuxoid
 
mnakalay replied on at Permalink Reply
mnakalay
If your question is how to add the select attribute options during install, the solution is already given here (specifically for select attribute):http://www.concrete5.org/community/forums/customizing_c5/selectattr...
linuxoid replied on at Permalink Reply
linuxoid
I'm trying this:
// Install category 'Car'
$akc = \Concrete\Core\Attribute\Key\Category::add('car', \Concrete\Core\Attribute\Key\Category::ASET_ALLOW_SINGLE, $pkg);
$service = $this->app->make('Concrete\Core\Attribute\Category\CategoryService');
$category = $service->getByHandle('car');
$key = $category->getByHandle('gear_box'); // <- Exception

where it throws an exception "Call to undefined method Concrete\Core\Entity\Attribute\Category::getByHandle()"

If I do this instead:
$category = AttributeCategory::getByHandle('car');
$key = CarKey::getByHandle('gear_box');

another error:
"Class 'CollectionAttributeKey' not found"

If I change for
$key = new CarKey();

another error: "Unable to retrieve legacy attribute key for method: setAttributeKeyHandle"
mnakalay replied on at Permalink Reply
mnakalay
I'm assuming you're doing this in a package. Make sure the minimum version you indicated in your package is 8.0.0.

Also, it is impossible to troubleshoot attribute related issues without seeing the whole code because C5 changed a lot from 5.7 to 8 concerning attributes and depending on how you write your code C5 might interpret it differently. In your case, it seems C5 thinks you are working with the legacy type attribute.
linuxoid replied on at Permalink Reply
linuxoid
Yes, I'm doing it in a package. I'm running 8.4.1. And I'm using the Community Store as a basis. It does use older style of attributes. That's why I tried to do what that document page showed but had those errors.

I've basically copied everything from the doc page but the exception was thrown right at the beginning there
mnakalay replied on at Permalink Reply
mnakalay
My question was what minimum version does your package require not what version are you using. C5 will use attributes differently depending on whether your package's $appVersionRequired

If your package requires 5.7.x as Community Store des than the above code will fail as you are telling C5 you are using legacy C5 but are then using v8 code.

First make sure your package's $appVersionRequired is set to at least 8.0.0
linuxoid replied on at Permalink Reply
linuxoid
$appVersionRequired = '8.4.0';
mnakalay replied on at Permalink Reply
mnakalay
ok. All I can say is the problem must be somewhere else in your package but since you're dripping code it is impossible to know what's happening.

Also, I use that code in production in a very attribute-heavy package installed on a few dozens of websites and it works flawlessly.

So maybe go back to the full tutorial in the code and find where you made a mistake. I used the exact same tutorial and although I had to adapt it a bit to my needs, it still worked.
linuxoid replied on at Permalink Reply
linuxoid
Is there any free package which uses Entity Attributes?

I need to have object attributes, not page attributes.
mnakalay replied on at Permalink Reply
mnakalay
I know I understand you have a custom "car" attribute category.

I don't know of any free package that does that other than community store
linuxoid replied on at Permalink Reply
linuxoid
I don't get that category thing 100% (there are categories, attributes, sets, trees, topics...).

All I need is to assign a number of attributes (strings, numbers, text etc.) to my object so that I can manipulate the list of my objects (cars) in blocks.

Do I need custom attributes for that? Or there's another way? Can it be done with some other kind of attributes?

Everyone talks about attributes but what's the benefit of them over simply storing values in a DB?
linuxoid replied on at Permalink Reply 1 Attachment
linuxoid
Ok, I've set up a completely new package Calendar and copied everything from this document page:
https://documentation.concrete5.org/developers/attributes/advanced-c...

The package is attached.

And it doesn't work. It throws an exception:

"Doctrine \ Common \ Persistence \ Mapping \ MappingException
The class 'Concrete\Package\Calendar\Entity\Attribute\Key\EventKey' was not found in the chain configured namespaces Concrete\Core\Entity, MyCal"

Exactly the same one I had in my other package (https://www.concrete5.org/community/forums/customizing_c5/8.4.0-class-was-not-found-in-the-chain-configured-namespaces).

What's wrong with it?

Thank you.