ArrayCollections在引用实体

时间:2016-02-03 22:32:27

标签: php doctrine-orm doctrine

我正在使用doctrine 2从预先存在的db模式创建实体。我有一些关系,但是当我访问Quote集合时,以下ArrayCollections返回null $screenprint $screenPrint

Quote.php

<?php
namespace BlankStyle\Entity;

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

/**
 * Class Quote
 * @ORM\Entity
 * @ORM\Table(name="uc_decoration_quotes")
 */

class Quote
{
  /** @ORM\Id @ORM\GeneratedValue @ORM\Column(type="integer", name="quote_id") */
  private $id;

  /** @ORM\Column(type="integer", name="order_id") */
  private $order_id;

  /** @ORM\Column(type="integer", name="uid") */
  private $uid;

  /** @ORM\Column(type="integer", name="customer_name") */
  private $c_name;

  /** @ORM\Column(type="integer", name="customer_email") */
  private $c_email;

  /** @ORM\Column(type="integer", name="customer_phone") */
  private $c_phone;

  /** @ORM\Column(type="integer", name="title") */
  private $title;

  /** @ORM\Column(type="integer", name="need_date") */
  private $need_date;

  /** @ORM\Column(type="integer", name="ship_date") */
  private $ship_date;

  /** @ORM\Column(type="integer", name="delivery_date") */
  private $d_date;

  /** @ORM\Column(type="integer", name="price_tier") */
  private $price_tier;

  /** @ORM\Column(type="integer", name="print_vendor") */
  private $p_vendor;

  /** @ORM\Column(type="integer", name="status") */
  private $status;

  /** @ORM\Column(type="integer", name="created") */
  private $created;

  /** @ORM\Column(type="string", name="data") */
  private $data;

  /**
   * @var integer
   * @ORM\ManyToOne(targetEntity="User", inversedBy="quote")
   * @ORM\JoinColumn(name="uid", referencedColumnName="uid")
   */
  private $user;

  /**
   * @var ArrayCollection $screenPrint
   * @ORM\OneToMany(targetEntity="ScreenPrint", mappedBy="quote")
   */
  private $screenPrint;

  /**
   * @var ArrayCollection $screenPrint
   * @ORM\OneToMany(targetEntity="Sublimation", mappedBy="quote")
   */
  private $sublimation;

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

Sublimation.php

<?php
namespace Blankstyle\Entity;

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

/**
 * Sublimation
 * @ORM\Entity
 * @ORM\Table(name="uc_decoration_sublimation")
 */
class Sublimation
{
  /** @ORM\Id @ORM\GeneratedValue @ORM\Column(type="integer", name="sublimation_id") */
  private $sublimation_id;

  /** @ORM\Id @ORM\Column(type="integer", name="quote_id") */
  private $quote_id;

  /** @ORM\Column(type="string", name="description") */
  private $description;

  /** @ORM\Column(type="string", name="location") */
  private $location;

  /** @ORM\Column(type="integer", name="qty") */
  private $qty;

  /** @ORM\Column(type="integer", name="width") */
  private $width;

  /** @ORM\Column(type="integer", name="height") */
  private $height;

  /** @ORM\Column(type="float", name="cost") */
  private $cost;

  /** @ORM\Column(type="float", name="price") */
  private $price;

  /**
   * @var integer
   * @ORM\ManyToOne(targetEntity="Quote", inversedBy="sublimation")
   * @ORM\JoinColumn(name="quote_id", referencedColumnName="quote_id")
   */
  private $quote;
}

Screenprint.php

<?php
namespace Blankstyle\Entity\Decoration;

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

/**
 * ScreenPrint
 * @ORM\Entity
 * @ORM\Table(name="uc_decoration_screenprint")
 */
class ScreenPrint
{
  /** @ORM\Id @ORM\GeneratedValue @ORM\Column(type="integer", name="loc_id") */
  private $loc_id;

  /** @ORM\Id @ORM\Column(type="integer", name="quote_id") */
  private $id;

  /** @ORM\Column(type="string", name="description") */
  private $description;

  /** @ORM\Column(type="string", name="location") */
  private $location;

  /** @ORM\Column(type="integer", name="qty") */
  private $qty;

  /** @ORM\Column(type="integer", name="color") */
  private $color;

  /** @ORM\Column(type="integer", name="width") */
  private $width;

  /** @ORM\Column(type="integer", name="height") */
  private $height;

  /** @ORM\Column(type="float", name="cost") */
  private $cost;

  /** @ORM\Column(type="float", name="price") */
  private $price;

  /**
   * @var integer
   * @ORM\ManyToOne(targetEntity="Quote", inversedBy="screenPrint")
   * @ORM\JoinColumn(name="id", referencedColumnName="quote_id")
   */
  private $quote;
}

uc_decoration_screenprint.sql

CREATE TABLE IF NOT EXISTS `uc_decoration_screenprint` (
  `loc_id` int(9) NOT NULL,
  `quote_id` int(9) NOT NULL,
  `location` varchar(120) NOT NULL,
  `description` varchar(120) NOT NULL,
  `colors` int(2) NOT NULL,
  `qty` int(3) NOT NULL,
  `ink_changes` int(3) NOT NULL,
  `print_cost` float(10,2) NOT NULL,
  `print_price` float(10,2) NOT NULL,
  `ink_cost` float(10,2) NOT NULL,
  `ink_price` float(10,2) NOT NULL,
  `setup_cost` float(10,2) NOT NULL,
  `setup_price` float(10,2) NOT NULL,
  `height` varchar(100) DEFAULT NULL,
  `width` varchar(100) DEFAULT NULL,
  `pantones` varchar(255) DEFAULT NULL,
  `under_base` smallint(1) DEFAULT NULL,
   PRIMARY KEY (loc_id, quote_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

uc_decoration_sublimation.sql 同样的事情是后者有主键约束

1 个答案:

答案 0 :(得分:0)

问题出在我的实体内的注释中,只将@ORM \ Id添加到您想要关联的键中。你不能有两个。

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

/**
 * ScreenPrint
 * @ORM\Entity
 * @ORM\Table(name="uc_decoration_screenprint")
 */
class ScreenPrint
{
  /** @ORM\GeneratedValue @ORM\Column(type="integer", name="loc_id") */
  private $loc_id;

  /** @ORM\Id @ORM\Column(type="integer", name="quote_id") */
  private $id;

  /** @ORM\Column(type="string", name="description") */
  private $description;

  /** @ORM\Column(type="string", name="location") */
  private $location;

  /** @ORM\Column(type="integer", name="qty") */
  private $qty;

  /** @ORM\Column(type="integer", name="color") */
  private $color;

  /** @ORM\Column(type="integer", name="width") */
  private $width;

  /** @ORM\Column(type="integer", name="height") */
  private $height;

  /** @ORM\Column(type="float", name="cost") */
  private $cost;

  /** @ORM\Column(type="float", name="price") */
  private $price;

  /**
   * @var integer
   * @ORM\ManyToOne(targetEntity="Quote", inversedBy="screenPrint")
   * @ORM\JoinColumn(name="id", referencedColumnName="quote_id")
   */
  private $quote;
}

是对的。 欢呼声。