Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8669971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:36:18+00:00 2026-06-12T18:36:18+00:00

This is a simple problem, but after going through all similar threads in Stackoverflow

  • 0

This is a simple problem, but after going through all similar threads in Stackoverflow and elsewhere without success, the problem remains.

I can’t get bidirectional One-To-Many relationship working. Biomass.field is accessible (owning side), but Field.biomasses (inverse side) is not.


Tables (removed expendable fields)

CREATE TABLE `field` (
  `field_id` varchar(255) NOT NULL,
  PRIMARY KEY (`field_id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `biomass` (
  `biomass_id` int(11) NOT NULL AUTO_INCREMENT,
  `field_id` varchar(255) NOT NULL,
  PRIMARY KEY (`biomass_id`),
  KEY `FK_field_has_0m_biomasses_idx` (`field_id`)  
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;


ALTER TABLE `biomass`
  ADD CONSTRAINT `FK_field_has_0m_biomasses` FOREIGN KEY (`field_id`) REFERENCES 
`field` (`field_id`) ON DELETE CASCADE ON UPDATE CASCADE;

Field.php

<?php

namespace Acme\MainBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Acme\MainBundle\Entity\Field
 *
 * @ORM\Table(name="field")
 * @ORM\Entity
 */
class Field {

    public function __construct() {
        $this->biomasses = new ArrayCollection();
    }

    /**
     * @var string $id
     *
     * @ORM\Column(name="field_id", type="string", length=255, nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /*
     * var ArrayCollection $biomasses
     *
     * @ORM\OneToMany(targetEntity="Biomass", mappedBy="field", cascade={"all"})
     */
    private $biomasses;

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

    /**
     * Get biomasses
     *
     * @return ArrayCollection
     */
    public function getBiomasses() {
        return $this->biomasses;
    }

    /**
     * Set biomasses
     *
     * @param ArrayCollection $biomasses
     * @return ArrayCollection
     */
    public function setBiomasses(ArrayCollection $biomasses) {
        $this->biomasses = $biomasses;
        return $this->biomasses;
    }

    /**
     * Add Biomass
     *
     * @param Biomass $biomass
     * @return ArrayCollection
     */
    public function addBiomass(Biomass $biomass) {
        $biomass->setField($this);
        return $this->biomasses[] = $biomass;
    }

    /**
     * Set biomass
     *
     * @param Biomass $biomass
     * @return ArrayCollection
     */
    public function removeBiomass(Biomass $biomass) {
        return $this->biomasses->removeElement($biomass);
    }       

    function __toString() {
        return $this->getId();
    }
}

Biomass.php

<?php

namespace Acme\MainBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Acme\MainBundle\Entity\Biomass
 *
 * @ORM\Table(name="biomass")
 * @ORM\Entity
 */
class Biomass {
    /**
     * @var integer $id
     *
     * @ORM\Column(name="biomass_id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var Field
     *
     * @ManyToOne(targetEntity="Field", inversedBy="biomasses")
     * @JoinColumn(name="field_id", referencedColumnName="field_id")
     */
    private $field;

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

    /**
     * Set field
     *
     * @param Field $field
     * @return Biomass
     */
    public function setField(Field $field = null) {
        //$field->addBiomass($this);
        $this->field = $field;

        return $this;
    }

    /**
     * Get field
     *
     * @return Field
     */
    public function getField() {
        return $this->field;
    }

    function __toString() {
        return $this->id;
    }
}

FieldController.php

$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository("AcmeMainBundle:Field")->findAll();

foreach($entities as $field) {
    foreach($field->getBiomasses() as $biomass) {
        print_r($biomass->getId());
    }
}

This results with :Warning: Invalid argument supplied for foreach() in C:[…]\MainBundle\Controller\FieldController.php line 39

ArrayCollecion is not being created, because Doctrine does not call constructors, it uses a serialize/unserialize trick to instantiate classes.

I have dummy data in those tables and the data is accessible, so it seems that doctrine is not creating biomasses object at all.


Environment
Symfony-2.1.2, Apache/2.4.3 (Win64), PHP/5.4.6-Win64, MySQL Community Server 5.5.28

EDIT: fixed Biomass.field referencing column name typo.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:36:19+00:00Added an answer on June 12, 2026 at 6:36 pm

    In BioMass.php the @JoinColumn should point to field_id not id in the BioMass table.

     /**
     * @var Field
     *
     * @ManyToOne(targetEntity="Field", inversedBy="biomasses")
     * @JoinColumn(name="field_id", referencedColumnName="field_id")
     */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All, this seems like a simple problem but it's giving me fits. Lets say
hey guys having this really simple problem but cant seem to figure out have
this may seem like a simple problem but I couldn't find it in the
this must be such a simple problem but can someone tell me why this
This is a seemingly simple problem but I am having trouble doing it in
Maybe this is a simple problem, but I am just not seeing it :)
I'm guessing this is a simple problem, but I'm just learning... I have this:
This is a really simple problem, but I couldn't find a solution anywhere. I'm
This is such a simple problem, but I can't find an answer anywhere... I
There must be a simple solution to this problem but for the love of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.