Doctrine模型数组数据类型

时间:2017-01-13 07:23:37

标签: php doctrine-orm orm doctrine

我是Doctrine的新手,我正在尝试制作一个MongoDb模型/集合。我有一个字段languages,应该是数组。像这样的数组:

$var = [
        array("fname"=>"test","lname"=>"test"),
        array("fname"=>"test","lname"=>"test"),
        array("fname"=>"test","lname"=>"test"),
    ];

但似乎我的类型声明无效。我有:

<?php


namespace RemoteStaff\Documents;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

/**
 * Class CandidateAsl
 * @package RemoteStaff\Documents
 * @ODM\Document(db="prod", collection="candidates_asl")
 */
class CandidateAsl
{
    /**
     * Constructor class
     */
    public function __construct(){
        $this->languages = new ArrayCollection();
    }

    /**
     * @var String
     * @ODM\Id(strategy="AUTO", type="string")
     */
    protected $_id;

    /**
     * @var string
     * @ODM\Field(type="string", name="gender")
     */
    private $gender;

    /**
     * @var \DateTime
     * @ODM\Field(type="date", name="birthdate")
     */
    private $birthdate;

    /**
     * @var string
     * @ODM\Field(type="string", name="nationality")
     */
    private $nationality;

    /**
     * @var string
     * @ODM\Field(type="string", name="permanentAddress")
     */
    private $permanentAddress;

    /**
     * @var \Doctrine\Common\Collections\ArrayCollection
     */
    private $languages = [];
}

如何在doctrine模型中声明或设置数组字段?

我已阅读文档http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/basic-mapping.html并尝试了

   /**
     * @var array
     * @ODM\Field(type="array", name="languages")
     */
    private $languages = [];

    /**
     * @return mixed
     */
    public function getLanguages()
    {
        return $this->languages;
    }

    /**
     * @param mixed $languages
     */
    public function setLanguages($languages)
    {
        $this->languages = $languages;
    }

我得到了Invalid type specified "array".

1 个答案:

答案 0 :(得分:1)

MongoDB ODM不支持array字段类型。请改用hashcollection

来自documentation

  
      
  • collection:数字索引数组到MongoDB数组

  •   
  • hash:关联数组到MongoDB对象

  •