0

我有 2 个实体

/**
 * @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;
}

这是我的表单处理程序中的代码:

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;
}

我遇到的问题是它没有在拥有方(fuel_line)上设置外键(carrier_fuel_id)。运行上述处理方法时出现此错误。

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...

编辑)我更新了 setFuelLine 设置器以设置carrier_fuel id,但现在它似乎试图在实际插入carrier_fuel 行之前保存fuel_line。

使用参数 [26、“4”、“4”、“4”、 29]:

SQLSTATE [42703]:未定义列:7 错误:关系“fuel_line”的列“carrier_fuel_id”不存在第 1 行:...SERT INTOfuel_line(id、min、max、ltl_surcharge、carrier_fu ...

4

2 回答 2

2

那么你必须从 CarrierFuel 实体中定义 referencedColumnName。查看文档,您发现了以下示例:

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

所以在你的情况下,你对 FuelLine 实体的注释应该是这样的:

/**
     * @ORM\ManyToOne(targetEntity="CarrierFuel", inversedBy="fuel_lines")
     * @ORM\JoinColumn(name="carrier_fuel_id", referencedColumnName="id")
     */
protected $carrier_fuel;
于 2013-07-25T22:44:19.463 回答
-2

您没有在 FuelLine 类中定义 $carrier_fuel_id 属性。这是在数据库中保存相关 FuelLine 实体的外键所必需的。

于 2013-07-25T12:51:04.147 回答