我处于这样一种情况,即我已经创建了2个子模块,我试图实现的是使用doctrine映射它们。我的结构如下:
我有2个模块,名为1)帐户 2)公司
在公司I下有2个子模块,名为a)公司 b)货币,同样两者都有独立的控制器和实体。我在公司下有一个名为Voucher的实体。现在问题来了当我尝试使用名为Company' s Entity Currency的子模块映射帐户时出现此错误。
[Doctrine\ORM\Mapping\MappingException]
The target-entity Currency\Entity\Currency cannot be found in 'Account\Enti
ty\Voucher#currency'.
以下是我的实体类
**Voucher Entity**
<?php
namespace Account\Entity;
use Doctrine\ORM\Mapping as ORM;
use Zend\Form\Annotation;
/**
* A Main Voucher entity.
*
* @ORM\Entity
* @ORM\Table(name="vouchers")
*
* @property int $id
* @property date $voucher_date
* @property int $voucher_number
*
* @Annotation\Name("Voucher")
*/
class Voucher {
/**
* @ORM\Id
* @ORM\Column(type="integer");
* @ORM\GeneratedValue(strategy="AUTO")
*
* @Annotation\Required(false)
*/
protected $id;
/**
* @ORM\Column(type="integer")
*
* @Annotation\Required(false)
*/
protected $voucher_number;
/**
* @ORM\ManyToOne(targetEntity="VoucherType", inversedBy="vouchers")
*/
protected $voucher_type;
/**
* @ORM\ManyToOne(targetEntity="Currency\Entity\Currency", inversedBy="vouchers")
*/
protected $currency;
/*
* Constructor
*/
public function __construct() {
$now = new \DateTime("now");
}
/**
* Magic getter to retrieve protected properties.
*
* @param string $property
*/
public function __get($property) {
if ($property == 'name') {
return $this->name();
} else {
return $this->$property;
}
}
/**
* Magic setter to save protected properties.
*
* @param string $property
* @param mixed $value
*/
public function __set($property, $value) {
$this->$property = $value;
}
public function getArrayCopy() {
}
public function exchangeArray($data) {
$this->id = (isset($data['id'])) ? $data['id'] : null;
$this->voucher_date = (isset($data['voucher_date'])) ? $data['voucher_date'] : null;
$this->voucher_number = (isset($data['voucher_number'])) ? $data['voucher_number'] : null;
}
public function populate($data) {
$this->id = isset($data['id']) ? $data['id'] : $this->id;
$this->voucher_date = isset($data['voucher_date']) ? $data['voucher_date'] : $this->category_name;
$this->voucher_number = isset($data['voucher_number']) ? $data['voucher_number'] : $this->voucher_number;
}
}
以下是公司模块
下的货币实体 <?php
namespace Company\Currency\Entity;
use Doctrine\ORM\Mapping as ORM;
use Zend\Form\Annotation;
/**
* A Main Voucher entity.
*
* @ORM\Entity
* @ORM\Table(name="currencies")
*
* @property int $id
* @property string $name
* @property string $code
*
* @Annotation\Name("Currency")
*/
class Currency {
/**
* @ORM\id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
protected $id;
/** @ORM\Column(type="string")
*/
protected $name;
/** @ORM\Column(type="string")
*/
protected $code;
/**
* @ORM\OneToMany(targetEntity="Account\Entity\Voucher", mappedBy="currency", orphanRemoval=true)
*
* @Annotation\Required(false)
*/
protected $vouchers;
/**
* Magic getter to retrieve protected properties.
*
* @param string $property
*/
public function __get($property) {
if ($property == 'name') {
return $this->name();
} else {
return $this->$property;
}
}
/**
* Magic setter to save protected properties.
*
* @param string $property
* @param mixed $value
*/
public function __set($property, $value) {
$this->$property = $value;
}
public function getArrayCopy() {
}
public function exchangeArray($data) {
$this->id = (isset($data['id'])) ? $data['id'] : null;
$this->name = (isset($data['name'])) ? $data['name'] : null;
$this->code = (isset($data['code'])) ? $data['code'] : null;
}
public function populate($data) {
$this->id = isset($data['id']) ? $data['id'] : $this->id;
$this->name = isset($data['name']) ? $data['name'] : $this->name;
$this->code = isset($data['code']) ? $data['code'] : $this->code;
}
}
Module.Config.php
<?php
namespace Company;
return array(
'controllers' => array(
'invokables' => array(
'Company\Controller\Company' => 'Company\Controller\CompanyController',
),
),
'router' => array(
'routes' => array(
'company' => array(
'type' => 'segment',
'options' => array(
'route' => '/company[/][:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Company\Controller\Company',
'action' => 'index',
),
),
),
),
),
// --------- Doctrine Settings For the Module
'doctrine' => array(
'driver' => array(
__NAMESPACE__ . '_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity')
),
'orm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
)
)
)
),
'view_manager' => array(
'template_path_stack' => array(
'company' => __DIR__ . '/../view',
),
),
);
如果我错了,请纠正我。从我得到的问题是,当我有2个子模块在1父模块下,就像在我的情况下。我遇到了这个问题,如果我将两个子模块合并到1个文件夹下,我想我不会得到这个错误。
答案 0 :(得分:3)
类名与命名空间一起(因此FQCN或完全限定类名)为Company\Currency\Entity\Currency
。您可以通过Currency\Entity\Currency
从凭证链接到货币。所以你在这里缺少命名空间的第一个Company
部分。
链接到正确的FQCN,它将被修复。
-
根据您发布的配置进行更新:
您有一个名为Company\Currency\Entity\Currency
的实体。在您的配置中,您指定src/Company/Entity
中包含名称空间Company\Entity
的实体。因此,您的“子模块”中的货币实体永远不会被发现,Doctrine不会知道它,因此无法将其映射到您的凭证。
专业提示:删除子模块并将其作为单个模块或使用两个普通模块。如果您对ZF2模块生态系统不熟悉,那么很难理解这些情况。拥有两个“普通”模块可以简化这一过程。