教义一对多关联,无法获得关系

时间:2015-08-09 03:15:33

标签: doctrine-orm zend-framework2

我正在使用doctrine 2模块用于zend框架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
                  }
                }

实际上文本太长了,我没有粘贴所有结果,

这是表格的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`)
)

这是项目表

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`),
)

1 个答案:

答案 0 :(得分:0)

学说2力求表现。想象一下,你有一个包含100个项目的笔记,你只需要笔记标题;这是教义2所考虑的,实际上除非用户直接询问,否则不会加载100个项目。这种技术称为延迟加载。

因此,请尝试询问您的物品,例如:

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

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

希望这会对你有所帮助。

相关问题