使用MongoDB和Doctrine更新symfony2中的EmbedMany文档

时间:2015-04-16 18:37:13

标签: php mongodb symfony doctrine-orm

我的Order文档中嵌入了Table个文档,我无法弄清楚如何更新它们。例如,我如何更改" pizza"的价值?到" pollo"

我的MongoDB是这样的:

表格文件

    <?php
namespace Acme\StoreBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/** @MongoDB\Document */
class Tables
{
    /**
     * @MongoDB\Id
     */
    protected $id;

    /**
     * @MongoDB\Int
     */
    protected $number;

    /**
     * @MongoDB\Int
     */
    protected $numordini;

    /**
     * @MongoDB\String
     */
    protected $state;

    /**
     * @MongoDB\String
     */
    protected $left;

    /**
     * @MongoDB\String
     */
    protected $top;


    /**
     * @MongoDB\String
     */
    protected $qrcode;

    /**
     * @MongoDB\String
     */
    protected $idristorante;

    /**
     * @MongoDB\String
     */
    protected $idadmin;

    /**
     * @MongoDB\EmbedMany(targetDocument="Order")
     */
    private $order;





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

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

    /**
     * Set number
     *
     * @param int $number
     * @return self
     */
    public function setNumber($number)
    {
        $this->number = $number;
        return $this;
    }

    /**
     * Get number
     *
     * @return int $number
     */
    public function getNumber()
    {
        return $this->number;
    }

    /**
     * Set state
     *
     * @param string $state
     * @return self
     */
    public function setState($state)
    {
        $this->state = $state;
        return $this;
    }

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

    /**
     * Set qrcode
     *
     * @param string $qrcode
     * @return self
     */
    public function setQrcode($qrcode)
    {
        $this->qrcode = $qrcode;
        return $this;
    }

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

    /**
     * Set idristorante
     *
     * @param string $idristorante
     * @return self
     */
    public function setIdristorante($idristorante)
    {
        $this->idristorante = $idristorante;
        return $this;
    }

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

    /**
     * Set idadmin
     *
     * @param string $idadmin
     * @return self
     */
    public function setIdadmin($idadmin)
    {
        $this->idadmin = $idadmin;
        return $this;
    }

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

    /**
     * Add order
     *
     * @param Acme\StoreBundle\Document\Order $order
     */
    public function addOrder(\Acme\StoreBundle\Document\Order $order)
    {
        $this->order[] = $order;
    }

    /**
     * Remove order
     *
     * @param Acme\StoreBundle\Document\Order $order
     */
    public function removeOrder(\Acme\StoreBundle\Document\Order $order)
    {
        $this->order->removeElement($order);
    }

    /**
     * Get order
     *
     * @return Doctrine\Common\Collections\Collection $order
     */
    public function getOrder()
    {
        return $this->order;
    }

    /**
     * Set left
     *
     * @param string $left
     * @return self
     */
    public function setLeft($left)
    {
        $this->left = $left;
        return $this;
    }

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

    /**
     * Set top
     *
     * @param string $top
     * @return self
     */
    public function setTop($top)
    {
        $this->top = $top;
        return $this;
    }

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

    /**
     * Set numordini
     *
     * @param int $numordini
     * @return self
     */
    public function setNumordini($numordini)
    {
        $this->numordini = $numordini;
        return $this;
    }

    /**
     * Get numordini
     *
     * @return int $numordini
     */
    public function getNumordini()
    {
        return $this->numordini;
    }
}

订单文件

<?php
namespace Acme\StoreBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/** @MongoDB\EmbeddedDocument */
class Order
{
    /**
     * @MongoDB\Id
     */
    protected $id;

    /**
     * @MongoDB\String
     */
    protected $nomepiatto;


    /**
     * @MongoDB\String
     */
    protected $variazione;

    /**
     * @MongoDB\String
     */
    protected $prezzo;

    /**
     * @MongoDB\String
     */
    protected $riferimento;

    /**
     * @MongoDB\String
     */
    protected $status;


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

    /**
     * Set nomepiatto
     *
     * @param string $nomepiatto
     * @return self
     */
    public function setNomepiatto($nomepiatto)
    {
        $this->nomepiatto = $nomepiatto;
        return $this;
    }

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

    /**
     * Set variazione
     *
     * @param string $variazione
     * @return self
     */
    public function setVariazione($variazione)
    {
        $this->variazione = $variazione;
        return $this;
    }

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

    /**
     * Set prezzo
     *
     * @param string $prezzo
     * @return self
     */
    public function setPrezzo($prezzo)
    {
        $this->prezzo = $prezzo;
        return $this;
    }

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

    /**
     * Set riferimento
     *
     * @param string $riferimento
     * @return self
     */
    public function setRiferimento($riferimento)
    {
        $this->riferimento = $riferimento;
        return $this;
    }

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

    /**
     * Set status
     *
     * @param string $status
     * @return self
     */
    public function setStatus($status)
    {
        $this->status = $status;
        return $this;
    }

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

数据库表输出:

{
    "_id" : ObjectId("54ad1e08622ef2b6541fc775"),
    "number" : NumberLong(1),
    "state" : "ciao",
    "idristorante" : "54a8834f622ef2e8688b4572",
    "idadmin" : "luca",
    "order" : [
        {
            "_id" : ObjectId("54f71508622ef2df618b4567"),
            "nomepiatto" : "opopop",
            "prezzo" : "12"
        },
        {
            "_id" : ObjectId("54f71ffb622ef2f47f8b4567"),
            "nomepiatto" : "pizza",
            "prezzo" : "12"
        }
    ],
    "left" : "476px",
    "top" : "420px"
}

1 个答案:

答案 0 :(得分:0)

这会将所有 Table.order.nomepiatta字段设置为pizzapollo。未经测试但应该可以使用。

$repo = $odm->getRepository("Table");
 $repo->createQueryBuilder()->findAndUpdate()->field('order.nomepiatto')
->set('pollo')->field("order.nomepiatto")->equals('pizza')->getQuery()->execute();