开发者

Embedded document and php assiociative array, how to in mongodb with doctrine?

开发者 https://www.devze.com 2023-02-19 09:23 出处:网络
Hi after some hours of searching i finally give up, I cant find any way of getting this thing done, I want to know if it is possible to create an EmbedMany relation using an php assiociative array.

Hi after some hours of searching i finally give up, I cant find any way of getting this thing done, I want to know if it is possible to create an EmbedMany relation using an php assiociative array.

I can use the @field(type="hash") but then I cant add a Embedded document to that key!

Here is the code I have now:

<?php

namespace Entity;

/**
 * @Document(collection="object_types")
 */
class OType
{
    /**
     * @Id
     * @var integer
     */
    private $id;

    /**
     * @Field(type="string")
     * @var string
     */
    private $name;

    /** 
     * @EmbedMany(targetDocument="Property")
     */
    private $properties = array();

    /**
     * Node Type
     *
     * @param string $name
     */
    public function __construct($name)
    {
        $this->name = $name;


    }

    /**
     * Set name
     *
     * @param string $name
     * @return void
     */
    public function setName($name)
    {
        $this->name = $name;
    }

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

    /**
     * Add property
     *
     * @param Property $property
     */
    public function addProperty(Property $property)
    {
        $this->properties[$property->getName()] = $property;
    }

    /**
     * Get properties
     *
     * @return Property[]开发者_如何转开发
     */
    public function getProperties()
    {
        return $this->properties;
    }
}

I want to do something like this:

$property = new Property();
$property->setName('memberof');
$property->setType('string');

$type = new OType();
$type->setName('user');
$type->addProperty($property);

And get this result:

{
  "_id": {
    "$oid": "4d89ae2c2eea0f8406000004"
  },
  "name": "user",
  "properties": [
    "memberof": {
      "name": "memberof",
      "type": "string"
    }
  ]
}

Thanks for you help.

Best regards Flamur


Actually, you CAN, use strategy="set" E.g:

@EmbedMany(targetDocument="Field", strategy="set")

Make sure to read THROUGH doctrine's documentation on collections:

http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/reference-mapping.html


I'm afraid you can't do that.

Make sure to read doctrine's documentation on collections: http://www.doctrine-project.org/docs/mongodb_odm/1.0/en/reference/reference-mapping.html


You shouldn't do that (just stick to one way of doing this: or you use always the associative array, or you use embedded documents)

However, this trick can help:

Just transform your the values in your properties array so that they all be Property objects.

//Add to your document:
/** @PostLoad */
public function postLoad()
{
    foreach($this->properties as $k => $p) {
        if(is_array($p)) {
            $newP = new Property();
            $newP = $p['name'];
            //..
            $this->properties[$k] = $newP;
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消