Doctrine ODM EmbedOne to EmbedMany

时间:2015-06-08 13:29:49

标签: mongodb doctrine-orm

我使用Mongodb和Doctrine2来记录有关统计目的的约会操作。我必须从

修改实体
class Appointment {
  /** @ODM\Id */
  protected $id;
  /** @ODM\EmbedOne(targetDocument="Product") */
  private $product;
to
class Appointment {
  /** @ODM\Id */
  protected $id;
  /** @ODM\EmbedMany(targetDocument="Product") */
  private $products;

所以早先的一个约会有一个产品,但现在一个约会可能有多个产品。保存时,每件事情都很好。 我的问题是如何更新旧文档的“产品”以将它们也放入数组中?

问候Andrea

2 个答案:

答案 0 :(得分:0)

您可以使用此处描述的迁移功能:http://doctrine-mongodb-odm.readthedocs.org/en/latest/reference/migrating-schemas.html

在您的情况下,您可以通过以下方式更新您的Doctrine文档:

<?php
class Appointment
{
    /**
     * ODM\EmbedOne(targetDocument="Product")
     * @ODM\NotSaved
     */
    protected $product;

    /**
     * @ODM\EmbedMany(targetDocument="Product")
     */
    protected $products;

    /**
     * @ODM\AlsoLoad({"product"})
     */
    public function populateProducts($product)
    {
        $this->products = [$product];
        $this->product = null;
    }

    /**
     * @ODM\AlsoLoad({"product"})
     */
    public function populateProducts($product)
    {
        $this->products = [$product];
    }
}

这样,旧的单品就会被投射到一系列产品上。

答案 1 :(得分:0)

<?php

命名空间Application \ Entity \ Documents;

将Doctrine \ ODM \ MongoDB \ Mapping \ Annotations用作ODM;

/ **  * @ODM \ Document(收集=&#34;约会&#34;)  * / class Appointment {

/** @ODM\Id */
protected $id;

/** @ODM\String @ODM\Index */
protected $department;

/** @ODM\String @ODM\Index */
protected $action;

/** @ODM\Int @ODM\Index */
protected $idAppointment;

/** @ODM\String */
protected $service_order;

/** @ODM\Int */
protected $sales_order;

/** @ODM\String */
protected $access_number;

/** @ODM\String */
protected $so_type;

/** @ODM\String */
protected $stage;

/** @ODM\String */
protected $se_items;

/** @ODM\String @ODM\Index */
protected $id_va;

/** @ODM\String */
protected $id_icms;

/** @ODM\String */
protected $company;

/** @ODM\String */
protected $customer_name;

/** @ODM\String */
protected $street;

/** @ODM\String */
protected $housenumber;

/** @ODM\String */
protected $building;

/** @ODM\String */
protected $appartment;

/** @ODM\String */
protected $postal_code;

/** @ODM\String */
protected $city;

/** @ODM\String */
protected $email;

/** @ODM\String */
protected $contact_number;

/** @ODM\String */
protected $ref_isp;

/** @ODM\String */
protected $comment;

/** @ODM\Date */
protected $first_available_schedule;

/** @ODM\Date */
protected $request_cancellation;

/** @ODM\Boolean */
protected $asap;

/** @ODM\String */
protected $allday;

/** @ODM\String @ODM\Index */
protected $operator;

/** @ODM\String */
protected $createdBy;

/** @ODM\String */
protected $movedBy;

/** @ODM\String */
protected $network;

/** @ODM\Date @ODM\Index */
private $created;

/** @ODM\Date @ODM\Index */
private $posted;

/** @ODM\String */
protected $actionBy;

/** @ODM\EmbedOne(targetDocument="Schedule") */
private $schedule;

/**
 * @ODM\EmbedOne(targetDocument="Product")
 * @ODM\NotSaved
 */
protected $product;

/** @ODM\EmbedMany(targetDocument="Product") */
private $products = array();

/**
 * @ODM\AlsoLoad({"product"})
 */
public function populateProducts($product) {
    $this->products = [$product];
    $this->product = null;
}

/** @ODM\String */
protected $mqMsg;

public function __construct() {
    $this->products = new \Doctrine\Common\Collections\ArrayCollection();
}

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

public function getDepartment() {
    return $this->department;
}

public function getAction() {
    return $this->action;
}

public function getIdAppointment() {
    return $this->idAppointment;
}

public function getService_order() {
    return $this->service_order;
}

public function getSales_order() {
    return $this->sales_order;
}

public function getAccess_number() {
    return $this->access_number;
}

public function getSo_type() {
    return $this->so_type;
}

public function getStage() {
    return $this->stage;
}

public function getSe_items() {
    return $this->se_items;
}

public function getId_va() {
    return $this->id_va;
}

public function getId_icms() {
    return $this->id_icms;
}

public function getCompany() {
    return $this->company;
}

public function getCustomer_name() {
    return $this->customer_name;
}

public function getStreet() {
    return $this->street;
}

public function getHousenumber() {
    return $this->housenumber;
}

public function getBuilding() {
    return $this->building;
}

public function getAppartment() {
    return $this->appartment;
}

public function getPostal_code() {
    return $this->postal_code;
}

public function getCity() {
    return $this->city;
}

public function getEmail() {
    return $this->email;
}

public function getContact_number() {
    return $this->contact_number;
}

public function getRef_isp() {
    return $this->ref_isp;
}

public function getComment() {
    return $this->comment;
}

public function getFirst_available_schedule() {
    return $this->first_available_schedule;
}

public function getRequest_cancellation() {
    return $this->request_cancellation;
}

public function getAsap() {
    return $this->asap;
}

public function getAllday() {
    return $this->allday;
}

public function getOperator() {
    return $this->operator;
}

public function getCreatedBy() {
    return $this->createdBy;
}

public function getMovedBy() {
    return $this->movedBy;
}

public function getNetwork() {
    return $this->network;
}

public function getCreated() {
    return $this->created;
}

public function getPosted() {
    return $this->posted;
}

public function getActionBy() {
    return $this->actionBy;
}

public function getSchedule() {
    return $this->schedule;
}

public function getProduct() {
    return $this->product;
}

public function getProducts() {
    return $this->products;
}

public function getMqMsg() {
    return $this->mqMsg;
}

public function setId($id) {
    $this->id = $id;
}

public function setDepartment($department) {
    $this->department = $department;
}

public function setAction($action) {
    $this->action = $action;
}

public function setIdAppointment($idAppointment) {
    $this->idAppointment = $idAppointment;
}

public function setService_order($service_order) {
    $this->service_order = $service_order;
}

public function setSales_order($sales_order) {
    $this->sales_order = $sales_order;
}

public function setAccess_number($access_number) {
    $this->access_number = $access_number;
}

public function setSo_type($so_type) {
    $this->so_type = $so_type;
}

public function setStage($stage) {
    $this->stage = $stage;
}

public function setSe_items($se_items) {
    $this->se_items = $se_items;
}

public function setId_va($id_va) {
    $this->id_va = $id_va;
}

public function setId_icms($id_icms) {
    $this->id_icms = $id_icms;
}

public function setCompany($company) {
    $this->company = $company;
}

public function setCustomer_name($customer_name) {
    $this->customer_name = $customer_name;
}

public function setStreet($street) {
    $this->street = $street;
}

public function setHousenumber($housenumber) {
    $this->housenumber = $housenumber;
}

public function setBuilding($building) {
    $this->building = $building;
}

public function setAppartment($appartment) {
    $this->appartment = $appartment;
}

public function setPostal_code($postal_code) {
    $this->postal_code = $postal_code;
}

public function setCity($city) {
    $this->city = $city;
}

public function setEmail($email) {
    $this->email = $email;
}

public function setContact_number($contact_number) {
    $this->contact_number = $contact_number;
}

public function setRef_isp($ref_isp) {
    $this->ref_isp = $ref_isp;
}

public function setComment($comment) {
    $this->comment = $comment;
}

public function setFirst_available_schedule($first_available_schedule) {
    $this->first_available_schedule = $first_available_schedule;
}

public function setRequest_cancellation($request_cancellation) {
    $this->request_cancellation = $request_cancellation;
}

public function setAsap($asap) {
    $this->asap = $asap;
}

public function setAllday($allday) {
    $this->allday = $allday;
}

public function setOperator($operator) {
    $this->operator = $operator;
}

public function setCreatedBy($createdBy) {
    $this->createdBy = $createdBy;
}

public function setMovedBy($movedBy) {
    $this->movedBy = $movedBy;
}

public function setNetwork($network) {
    $this->network = $network;
}

public function setCreated($created) {
    $this->created = $created;
}

public function setPosted($posted) {
    $this->posted = $posted;
}

public function setActionBy($actionBy) {
    $this->actionBy = $actionBy;
}

public function setSchedule($schedule) {
    $this->schedule = $schedule;
}

public function setProduct($product) {
    $this->product = $product;
}

public function setProducts($products) {
    $this->products = $products;
}

public function setMqMsg($mqMsg) {
    $this->mqMsg = $mqMsg;
}

public function addProduct($product) {
    $this->products[] = $product;
}

}

/ ** @ODM \ EmbeddedDocument * / 课程安排{

/** @ODM\Id */
protected $id;

/** @ODM\String */
protected $layer;

/** @ODM\String */
protected $team;

/** @ODM\String */
protected $disponibility;

/** @ODM\Date @ODM\Index */
protected $duedate;

/** @ODM\Date @ODM\Index */
protected $dueperiod;

/** @ODM\Boolean */
protected $is_task;

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

function getLayer() {
    return $this->layer;
}

function getTeam() {
    return $this->team;
}

function getDisponibility() {
    return $this->disponibility;
}

function getDuedate() {
    return $this->duedate;
}

function getDueperiod() {
    return $this->dueperiod;
}

function getIs_task() {
    return $this->is_task;
}

function setId($id) {
    $this->id = $id;
}

function setLayer($layer) {
    $this->layer = $layer;
}

function setTeam($team) {
    $this->team = $team;
}

function setDisponibility($disponibility) {
    $this->disponibility = $disponibility;
}

function setDuedate($duedate) {
    $this->duedate = $duedate;
}

function setDueperiod($dueperiod) {
    $this->dueperiod = $dueperiod;
}

function setIs_task($is_task) {
    $this->is_task = $is_task;
}

}

/ ** @ODM \ EmbeddedDocument * / class Product {

/** @ODM\Id */
protected $id;

/** @ODM\String */
protected $category;

/** @ODM\String */
protected $disponibility;

/** @ODM\String */
protected $abbr;

/** @ODM\Float */
protected $freeze_day;

/** @ODM\Boolean */
protected $regulated;

/** @ODM\Int */
protected $sla;

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

function getCategory() {
    return $this->category;
}

function getDisponibility() {
    return $this->disponibility;
}

function getAbbr() {
    return $this->abbr;
}

function getFreeze_day() {
    return $this->freeze_day;
}

function getSenumber() {
    return $this->senumber;
}

function getRegulated() {
    return $this->regulated;
}

public function getSla() {
    return $this->sla;
}

function setId($id) {
    $this->id = $id;
}

function setCategory($category) {
    $this->category = $category;
}

function setDisponibility($disponibility) {
    $this->disponibility = $disponibility;
}

function setAbbr($abbr) {
    $this->abbr = $abbr;
}

function setFreeze_day($freeze_day) {
    $this->freeze_day = $freeze_day;
}

function setSenumber($senumber) {
    $this->senumber = $senumber;
}

function setRegulated($regulated) {
    $this->regulated = $regulated;
}

public function setSla($sla) {
    $this->sla = $sla;
}

}