Doctrine one to many association, 无法获得关系中的其他对象

Doctrine one to many association, can't get the other object in relation

我正在为 zend framework 2 使用 doctrine 2 模块,我尝试了一对多关联,但在查询主实体时遇到问题。这是实体

<?php
namespace Domain\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Domain\Entity\Abstracts\Entity;
use Doctrine\ORM\Mapping as ORM;
use Domain\Entity\Item;


/**
 * Class Note
 * @package Domain\Entity
 * @ORM\Table(name="notes")
 * @ORM\Entity
 */
class Note extends Entity {

    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer",name="note_id")
     */
    public $id;

    /**
     * @ORM\Column(type="datetime",name="note_due_date")
     */
    public $dueDate;


    /**
     * @ORM\OneToMany(targetEntity="Item",mappedBy="note")
     **/
    public $items;


    /**
     * @ORM\Column(type="datetime",name="note_created_date")
     */
    public $createdDate;


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

}

每个笔记可以有多个项目,但项目只有一个笔记

<?php
namespace Domain\Entity;
use Doctrine\ORM\Mapping as ORM;


/**
 * Class Note
 * @package Domain\Entity
 * @ORM\Table(name="items")
 * @ORM\Entity
 */
class Item {

    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer",name="item_id")
     */
    public $id;

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

    /**
     * @ORM\Column(type="boolean",name="item_is_done")
     */
    public $isDone;

    /**
     * @ORM\Column(type="integer",name="item_order")
     */
    public $order;


    /**
     * @ORM\Column(type="datetime",name="item_created_date")
     */
    public $createdDate;

    /**
     * @ORM\Column(type="integer",name="item_note_id")
     */
    public $noteId;


    /**
     * @ORM\ManyToOne(targetEntity="Note", inversedBy="items")
     * @ORM\JoinColumn(name="item_note_id", referencedColumnName="note_id")
     */
    public $note;


}

我想检索 ID 为 1 的便条,我希望结果是便条及其相关项

$notes = $em->getRepository('\Domain\Entity\Note')
            ->findBy(array('id' => 1));

var_dump($notes );

输出很奇怪

array(1) {
  [0] => object(Domain\Entity\Note)#362 (6) {
    ["id"] => int(1)
    ["dueDate"] => object(DateTime)#358 (3) {
      ["date"] => string(26) "2015-08-19 00:00:00.000000"
      ["timezone_type"] => int(3)
      ["timezone"] => string(3) "UTC"
    }
    ["items"] => object(Doctrine\ORM\PersistentCollection)#366 (9) {
      ["snapshot":"Doctrine\ORM\PersistentCollection":private] => array(0) {
      }
      ["owner":"Doctrine\ORM\PersistentCollection":private] => *RECURSION*
      ["association":"Doctrine\ORM\PersistentCollection":private] => array(15) {
        ["fieldName"] => string(5) "items"
        ["mappedBy"] => string(4) "note"
        ["targetEntity"] => string(18) "Domain\Entity\Item"
        ["cascade"] => array(0) {
        }
        ["orphanRemoval"] => bool(false)
        ["fetch"] => int(2)
        ["type"] => int(4)
        ["inversedBy"] => NULL
        ["isOwningSide"] => bool(false)
        ["sourceEntity"] => string(18) "Domain\Entity\Note"
        ["isCascadeRemove"] => bool(false)
        ["isCascadePersist"] => bool(false)
        ["isCascadeRefresh"] => bool(false)
        ["isCascadeMerge"] => bool(false)
        ["isCascadeDetach"] => bool(false)
      }
      ["em":"Doctrine\ORM\PersistentCollection":private] => object(Doctrine\ORM\EntityManager)#280 (11) {
        ["config":"Doctrine\ORM\EntityManager":private] => object(Doctrine\ORM\Configuration)#285 (1) {
          ["_attributes":protected] => array(14) {
            ["autoGenerateProxyClasses"] => int(0)
            ["proxyDir"] => string(28) "data/DoctrineORMModule/Proxy"
            ["proxyNamespace"] => string(23) "DoctrineORMModule\Proxy"
            ["entityNamespaces"] => array(0) {
            }
            ["classMetadataFactoryName"] => string(41) "Doctrine\ORM\Mapping\ClassMetadataFactory"
            ["metadataCacheImpl"] => object(Doctrine\Common\Cache\ArrayCache)#288 (3) {
              ["data":"Doctrine\Common\Cache\ArrayCache":private] => array(20) {
                ["DoctrineNamespaceCacheKey[DoctrineModule]"] => int(1)
                ["DoctrineModule[Domain\Entity\Abstracts\Entity@[Annot]][1]"] => array(0) {
                }
                ["DoctrineModule[Domain\Entity\Note@[Annot]][1]"] => array(2) {
                  ["Doctrine\ORM\Mapping\Table"] => object(Doctrine\ORM\Mapping\Table)#323 (5) {
                    ["name"] => string(5) "notes"
                    ["schema"] => NULL
                    ["indexes"] => NULL
                    ["uniqueConstraints"] => NULL
                    ["options"] => array(0) {
                    }
                  }
                  ["Doctrine\ORM\Mapping\Entity"] => object(Doctrine\ORM\Mapping\Entity)#322 (2) {
                    ["repositoryClass"] => NULL
                    ["readOnly"] => bool(false)
                  }
                }
                ["DoctrineModule[Domain\Entity\Note$id@[Annot]][1]"] => array(3) {
                  ["Doctrine\ORM\Mapping\Id"] => object(Doctrine\ORM\Mapping\Id)#328 (0) {
                  }
                  ["Doctrine\ORM\Mapping\GeneratedValue"] => object(Doctrine\ORM\Mapping\GeneratedValue)#330 (1) {
                    ["strategy"] => string(4) "AUTO"
                  }
                  ["Doctrine\ORM\Mapping\Column"] => object(Doctrine\ORM\Mapping\Column)#340 (9) {
                    ["name"] => string(7) "note_id"
                    ["type"] => string(7) "integer"
                    ["length"] => NULL
                    ["precision"] => int(0)
                    ["scale"] => int(0)
                    ["unique"] => bool(false)
                    ["nullable"] => bool(false)
                    ["options"] => array(0) {
                    }
                    ["columnDefinition"] => NULL
                  }
                }
                ["DoctrineModule[Domain\Entity\Note$dueDate@[Annot]][1]"] => array(1) {
                  ["Doctrine\ORM\Mapping\Column"] => object(Doctrine\ORM\Mapping\Column)#331 (9) {
                    ["name"] => string(13) "note_due_date"
                    ["type"] => string(8) "datetime"
                    ["length"] => NULL
                    ["precision"] => int(0)
                    ["scale"] => int(0)
                    ["unique"] => bool(false)
                    ["nullable"] => bool(false)
                    ["options"] => array(0) {
                    }
                    ["columnDefinition"] => NULL
                  }
                }
                ["DoctrineModule[Domain\Entity\Note$funDegree@[Annot]][1]"] => array(1) {
                  ["Doctrine\ORM\Mapping\Column"] => object(Doctrine\ORM\Mapping\Column)#337 (9) {
                    ["name"] => string(15) "note_fun_degree"
                    ["type"] => string(6) "string"
                    ["length"] => NULL
                    ["precision"] => int(0)
                    ["scale"] => int(0)
                    ["unique"] => bool(false)
                    ["nullable"] => bool(false)
                    ["options"] => array(0) {
                    }
                    ["columnDefinition"] => NULL
                  }
                }

其实文字太长了,我没有把结果全部贴出来,

这是 table 的 SQL

CREATE TABLE `notes` (
 `note_id` int(11) NOT NULL AUTO_INCREMENT,
 `note_due_date` datetime NOT NULL,
 `note_created_date` datetime NOT NULL,
 PRIMARY KEY (`note_id`)
)

这是项目 table

CREATE TABLE `items` (
 `item_id` int(11) NOT NULL AUTO_INCREMENT,
 `item_note_id` int(11) NOT NULL,
 `item_name` varchar(250) NOT NULL,
 `item_is_done` tinyint(4) NOT NULL DEFAULT '0',
 `item_order` int(11) NOT NULL,
 `item_created_date` date NOT NULL,
 PRIMARY KEY (`item_id`),
)

原则 2 力求表现。假设您有一个包含 100 个项目的笔记,而您只需要笔记标题;这就是学说 2 的想法,实际上这 100 个项目是不会加载的,除非用户直接请求它们,这种技术称为延迟加载。

所以尝试索取您的物品,例如:

foreach (note->getItems() as item)
      // do your stuff here

或者将它们发送到树枝模板并打印结果。

希望对您有所帮助。