title: OneToMany mapping from Table to Foreign Table [Symfony 2 / Doctrine]
body: <p>From what I've gathered, Symfony 2 / Doctrine uses the database definitions (foreign key constraints in my case) to map relations between entities. I have two tables in particular that I want to be able to relate from both sides, but I do not want to create redundant foreign keys in each table. In this case, I have an <code>Account</code> table, and a <code>Transaction</code> table.</p>
  
  <p><strong>Account Table</strong></p>
  
  <pre><code>CREATE TABLE "account" (
      "account_id"            BIGSERIAL NOT NULL,
      "name"                  VARCHAR (100) NOT NULL,
      "date_created"          TIMESTAMP (6) WITH TIME ZONE NOT NULL,
      "date_modified"         TIMESTAMP (6) WITH TIME ZONE,
  
      CONSTRAINT "pk-account-account_id"
          PRIMARY KEY ("account_id"),
  );
  </code></pre>
  
  <p><strong>Transaction Table</strong></p>
  
  <pre><code>CREATE TABLE "transaction" (
      "transaction_id"        BIGSERIAL NOT NULL,
      "account_id"            BIGINT NOT NULL,
      "amount"                MONEY NOT NULL,
      "date_created"          TIMESTAMP (6) WITH TIME ZONE NOT NULL,
      "date_modified"         TIMESTAMP (6) WITH TIME ZONE,
  
      CONSTRAINT "pk-transaction-transaction_id"
          PRIMARY KEY ("transaction_id"),
  
      CONSTRAINT "fk-transaction-account_id-account-account_id"
          FOREIGN KEY ("account_id")
          REFERENCES "account" ("account_id")
              ON DELETE RESTRICT
              ON UPDATE CASCADE,
  );
  </code></pre>
  
  <p>When I generate the entities using <code>php bin/console doctrine:generate:entities</code> I see that the transaction entity has an $account property, but my account entity does not have a $transaction entity. I assume this is because I do not define a foreign key constraint in my account table.</p>
  
  <p>In my code, I create my account object by the following:</p>
  
  <pre><code>$accounts = $this-&gt;getDoctrine()
      -&gt;getRepository('BalancesBundle:Account')
          -&gt;findAll();
  </code></pre>
  
  <p>I then would want to iterate over the array to get the total balance for each account. In the long-term, I'd like to create a helper method inside my account entity that would call <code>getTransactions()</code> to add up all of the transactions into one sum total. </p>
  
  <p>Is this possible? I feel like I'm missing something, and that my only recourse would be to do this from within the transaction entity. I would like to avoid doing this from the transaction entity if possible.</p>