0

I have 2 Entities

/**
 * @ORM\Entity(repositoryClass="FLI\ContractBundle\Repository\CarrierFuelRepository")
 * @ORM\Table(name="carrier_fuel")
 */
class CarrierFuel
{
    /**
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string")
     */
    protected $title;

    /**
     * @ORM\ManyToOne(targetEntity="FuelIndex")
     */
    protected $index;

    /**
     * @ORM\OneToMany(targetEntity="FuelLine", mappedBy="carrier_fuel",  cascade={"persist", "remove"})
     */
    protected $fuel_lines;
    }


/**
 * @ORM\Entity(repositoryClass="FLI\ContractBundle\Repository\FuelLineRepository")
 * @ORM\Table(name="fuel_line")
 */
class FuelLine
{
    /**
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="CarrierFuel", inversedBy="fuel_lines")
     */
    protected $carrier_fuel;
}

And this is code in my form handler:

public function process(CarrierFuel $fuel)
{
    $this->form->setData($fuel);
    if ('POST' === $this->request->getMethod())
    {
        $this->form->bind($this->request);
        if ($this->form->isValid())
        {
            $this->em->persist($fuel);
            $this->em->flush();
            return true;
        }
    }
    return false;
}

The issue im having is its not setting the foreign key (carrier_fuel_id) on the owning side (fuel_line). I get this error when running the process method above.

An exception occurred while executing 'INSERT INTO fuel_line (id, min, max, ltl_surcharge, carrier_fuel_id) VALUES (?, ?, ?, ?, ?)' with params [14, "4", "4", "4", null]:

SQLSTATE[42703]: Undefined column: 7 ERROR: column "carrier_fuel_id" of relation "fuel_line" does not exist
LINE 1: ...SERT INTO fuel_line (id, min, max, ltl_surcharge, carrier_fu...

(EDIT) I updated the setFuelLine setter to set the carrier_fuel id, but now it seems like it tries to save the fuel_line before actually inserting the carrier_fuel row.

An exception occurred while executing 'INSERT INTO fuel_line (id, min, max, ltl_surcharge, carrier_fuel_id) VALUES (?, ?, ?, ?, ?)' with params [26, "4", "4", "4", 29]:

SQLSTATE[42703]: Undefined column: 7 ERROR: column "carrier_fuel_id" of relation "fuel_line" does not exist LINE 1: ...SERT INTO fuel_line (id, min, max, ltl_surcharge, carrier_fu...

Trololololol
  • 200
  • 4
  • 18
  • 1
    Everything looks correct at first glance, maybe do an `app/console doctrine:schema:validate` to find out if any of your mapping is wrong or your db is out of sync with your mapping. – Ken Hannel Jul 24 '13 at 23:59
  • Fixed the original issue, but now it seems to try to insert the owning side first, so I get a constraint error. – Trololololol Jul 25 '13 at 03:47
  • Can you update your code to show the modified setter? Also, undefined column means your schema is not up to date with your model. – Ezequiel Muns Jul 25 '13 at 05:06
  • Yup, you are correct, should of noticed that :S. Thanks for the help guys. – Trololololol Jul 25 '13 at 21:07

2 Answers2

2

Well you have to define the referencedColumnName from CarrierFuel Entity. Looking into the docs, you found this example:

/**
     * @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
     */

so in your case your annotation on FuelLine entity should be something like this:

/**
     * @ORM\ManyToOne(targetEntity="CarrierFuel", inversedBy="fuel_lines")
     * @ORM\JoinColumn(name="carrier_fuel_id", referencedColumnName="id")
     */
protected $carrier_fuel;
João Alves
  • 1,931
  • 17
  • 25
-2

You don't define a $carrier_fuel_id property in your FuelLine class. This is needed to hold the foreign key of the related FuelLine entity in the database.

Tocacar
  • 475
  • 3
  • 12