# Hilfe bei ManyToOne und Associations

**URL:** <https://forum.shopware.com/t/hilfe-bei-manytoone-und-associations/54211>\
**Category:** Programmierung\
**Created:** [22. Juli 2018 um 18:35 UTC](https://forum.shopware.com/t/hilfe-bei-manytoone-und-associations/54211 "2018-07-22T18:35:07Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![backtext](https://avatars.discourse-cdn.com/v4/letter/b/e19b73/32.png) [@backtext](https://forum.shopware.com/u/backtext)\
**Post date:** [22. Juli 2018 um 18:35 UTC](https://forum.shopware.com/t/hilfe-bei-manytoone-und-associations/54211/1 "2018-07-22T18:35:07Z")

</div>

Hallo Comunity,

ich habe ein&nbsp;Plugin erstellt welches Fragen und Antworten hat/anzeigt. Das Plugin besteht aus (min.) zwei DB Tabellen: Questions und Answers. Ein Question kann dabei mehrere Answer haben.  
Zuerst einmal, es funktioniert!&nbsp;Die Fragen könnten bearbeiter werden und Antworten auch&nbsp;:). Jetzt versuche ich es&nbsp;weiter anzupassen.

* * *

So jetzt habe ich in&nbsp;meiner Logik, dass **jede Antwor** t zur **nächsten Frage** führt. Somit müsste bei der Datailansicht der Antwort&nbsp; eine **Auswähl (Select) von alle Fragen** kommen!

[Backend Components - Associations](https://developers.shopware.com/developers-guide/backend-components/associations/)  
Eine Auswahl aller Fragen habe ich als&nbsp;Beispiel so realisiert:&nbsp;Link products to tax rates (ManyToOne) - vom&nbsp;dem oben Link&nbsp;  
Leider werden keine Fragen angezeigt und es gib keine Auswahlbox!

PHP Model/Antwort

```
namespace KreageQuiztool\Models;

use Shopware\Components\Model\ModelEntity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="kreagequiztool_answers")
 */
class Answer extends ModelEntity
{
    /**
     * @var integer $id
     *
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var integer $question_id
     *
     * @ORM\Column(type="integer")
     */
    private $question_id;

    /**
     * @var integer $active
     *
     * @ORM\Column(type="integer")
     */
    private $active;

    /**
     * @var string $image
     *
     * @ORM\Column(type="text", nullable=true)
     */
    private $image;

    /**
     * @var string $text
     *
     * @ORM\Column(type="text", nullable=true)
     */
    private $text;

    /**
     * @var string $next_question_id
     *
     * @ORM\Column(type="integer")
     */
    private $next_question_id;

    /**
     * @var
     * @ORM\ManyToOne(targetEntity="KreageQuiztool\Models\Question", inversedBy="answers")
     * @ORM\JoinColumn(name="question_id", referencedColumnName="id")
     */
    protected $question;

    /**
     * @var
     * @ORM\ManyToOne(targetEntity="KreageQuiztool\Models\Question")
     * @ORM\JoinColumn(name="next_question_id", referencedColumnName="id")
     */
    protected $next_question;

    public function __construct()
    {
    }

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param int $question_id
     */
    public function setQuestionId($question_id)
    {
        $this->question_id = $question_id;
    }

    /**
     * @return int
     */
    public function getQuestionId()
    {
        return $this->question_id;
    }

    /**
     * @param int $active
     */
    public function setActive($active)
    {
        $this->active = $active;
    }

    /**
     * @return int
     */
    public function getActive()
    {
        return $this->active;
    }

    /**
     * @param string $image_path
     */
    public function setImage($image_path)
    {
        $this->image = $image_path;
    }

    /**
     * @return string
     */
    public function getImage()
    {
        return $this->image;
    }

    /**
     * @param string $text
     */
    public function setText($text)
    {
        $this->text = $text;
    }

    /**
     * @return string
     */
    public function getText()
    {
        return $this->text;
    }

    /**
     * @param int $next_question_id
     */
    public function setNextQuestionId($next_question_id)
    {
        $this->next_question_id = $next_question_id;
    }

    /**
     * @return string
     */
    public function getNextQuestionId()
    {
        return $this->next_question_id;
    }

    /**
     * @param mixed $question
     */
    public function setQuestion($question)
    {
        $this->question = $question;
    }

    /**
     * @return mixed
     */
    public function getQuestion()
    {
        return $this->question;
    }

    /**
     * @return mixed
     */
    public function getNextQuestion()
    {
        return $this->next_question;
    }

    /**
     * @param mixed $next_question
     */
    public function setNextQuestion($next_question)
    {
        $this->next_question = $next_question;
    }

}

```

JS Model/Antwort

```
Ext.define('Shopware.apps.KreageQuiztool.model.Answer', {
    extend: 'Shopware.data.Model',

    configure: function() {
        return {
            controller: 'KreageQuiztool',
            // controller: 'KreageQuiztoolAnswer',
            listing: 'Shopware.apps.KreageQuiztool.view.list.Answer',
            detail: 'Shopware.apps.KreageQuiztool.view.detail.Answer'
        };
    },

    fields: [
        { name : 'id', type: 'int', useNull: true },
        { name : 'question_id', type: 'int', useNull: true },
        { name : 'active', type: 'boolean', useNull: true },
        { name : 'image', type: 'string', useNull: false },
        { name : 'text', type: 'string', useNull: false },
        { name : 'next_question_id', type: 'int', useNull: true }
    ],

    associations: [{
        relation: 'ManyToOne',
        field: 'image',
        type: 'hasMany',
        model: 'Shopware.apps.Base.model.Media',
        name: 'getMedia',
        associationKey: 'media'
    }, {
        relation: 'ManyToOne',
        field: 'question_id',
        type: 'hasMany',
        model: 'Shopware.apps.KreageQuiztool.model.Question',
        name: 'getNextQuestion',
        associationKey: 'next_question'
    }]
});

```

Die View/Antwort hat eine default ausgabe (alle Felder werden ausgegeben). Die&nbsp;next\_question sehe ich aber nicht und&nbsp;next\_question steht nur ein Zahlenfeld. Ich würde aber gerne eine Auswal haben wo ich eine Frage (ID) auswählen kann.

Soll ich noch etws posten, um es besser nachzuvollziehen.&nbsp;

Ich danke erst mal für die Hilfe 🙂
