XF 2.1 Guide on adding entities to database

Matt C.

Well-known member
Would someone be kind enough to post a general guide on how to add entities to the database? I'm creating a Matchmaking add-on for my site where users can post matches/groups and are looking for players.

189122

This is about as far as I've gotten. Any help would be greatly appreciated, thank you!
 
Have you made your entity file and created the pertinent table in your setup file and local database?

If so, you can create entities like this (assuming you're in a controller): $entity = $this->em()->create('Vendor\AddOn:Entity');
Then you can assign the values from your form input: $entity->column = $value;
And finally save the entity to the database: $entity->save();

It wouldn't be a bad idea to encapsulate the creation process in a service if there is anything more complex than that going on, or maybe a FormAction at the very least, but the above will work for starters. If you need help with more fundamental aspects (fetching input from the controller, creating entities and tables, etc) I'd recommend having a look at the tutorial. Otherwise if you have specific questions feel free to ask.
 
Have you made your entity file and created the pertinent table in your setup file and local database?

If so, you can create entities like this (assuming you're in a controller): $entity = $this->em()->create('Vendor\AddOn:Entity');
Then you can assign the values from your form input: $entity->column = $value;
And finally save the entity to the database: $entity->save();

It wouldn't be a bad idea to encapsulate the creation process in a service if there is anything more complex than that going on, or maybe a FormAction at the very least, but the above will work for starters. If you need help with more fundamental aspects (fetching input from the controller, creating entities and tables, etc) I'd recommend having a look at the tutorial. Otherwise if you have specific questions feel free to ask.

Thank you Jeremy. I did as you said, but it seems to no effect, obviously I'm doing something wrong.

PHP:
public function actionSave(ParameterBag $params)
{
    $this->assertPostOnly();

    $visitor = \XF::visitor();

    if (!$visitor->canAddMatch())
    {
        throw $this->exception($this->noPermission());
    }

    if ($params->match_id)
    {
        $match = $this->assertMatchExists($params->match_id);
    }
    else
    {
        $match = $this->em()->create('AH\Matchmaking:Match');
    }

    $title = $this->filter('title', 'str');
    $description = $this->filter('description', 'str');
    $platform = $this->filter('platform', 'str');
    $voice = $this->filter('voice', 'bool');

    $match->title = $title;
    $match->description = $description;
    $match->platform = $platform;
    $match->voice = $voice;
    $match->username = $visitor;

    $match->save();

    $reply = $this->view('AH\Matchmaking:Index', 'ah_mm_overview');
    return $reply;
}
 
Nothing stands out to me. Do you get any errors or exceptions? You can check your server error log to be sure.
 
Actually, it does work. My apologies.

189260

There's just a few problems. The title field doesn't save, and the voice field should be 1 if it's checked, and shouldn't the username field contain the actual username?

Edit: I fixed the username issue by using $visitor->username
 
Make sure your form input for the title is named title and the voice input is named voice. You can check the filtered values using a debugger, or by simply die(var_dump()ing the variables if you don't have a debugger available.
 
Have you made your entity file and created the pertinent table in your setup file and local database?

If so, you can create entities like this (assuming you're in a controller): $entity = $this->em()->create('Vendor\AddOn:Entity');
Then you can assign the values from your form input: $entity->column = $value;
And finally save the entity to the database: $entity->save();

It wouldn't be a bad idea to encapsulate the creation process in a service if there is anything more complex than that going on, or maybe a FormAction at the very least, but the above will work for starters. If you need help with more fundamental aspects (fetching input from the controller, creating entities and tables, etc) I'd recommend having a look at the tutorial. Otherwise if you have specific questions feel free to ask.

What kind of controller should I edit to add this entity?
Thanks.
 
Make sure your form input for the title is named title and the voice input is named voice. You can check the filtered values using a debugger, or by simply die(var_dump()ing the variables if you don't have a debugger available.

Here is the the macro:
Code:
<xf:macro template="ah_mm_match_edit_macros" name="title"
    arg-match="{$match}" />

Code:
<xf:macro name="title" arg-match="!">
    <xf:textboxrow
        label="{{ phrase('title') }}"
        type="match"
        textbox-value="{$match.title}"
        maxlength="{{ max_length($match, 'title') }}"
        placeholder="{{ phrase('title...') }}" />
</xf:macro>

They look good to me, I'm not sure what the problem is
 
Back
Top Bottom