【发布时间】:2014-08-23 11:47:13
【问题描述】:
我使用many-to-one 映射并关联了这三个实体,我需要从模板中的关联中获取驱动程序名称,因此我在模板上执行此操作:
{% for devices in pagination %}
{{ devices.getDriver.name }}
{% endfor %}
但我收到此错误:
对象“Device\DeviceBundle\Entity\Device”的方法“getDriver”确实 不存在于 /var/www/html/src/Device/DeviceBundle/Resources/views/List/listDevices.html.twig 在第 47 行
这是实体和映射信息:
Device.php
class Device
{
protected $id;
protected $description;
protected $imei;
protected $created;
protected $modified;
protected $deletedAt;
public function getId()
{
return $this->id;
}
public function getDescription()
{
return $this->description;
}
public function setDescription($description)
{
$this->description = $description;
}
public function getImei()
{
return $this->imei;
}
public function setImei($imei)
{
$this->imei = $imei;
}
}
设备映射
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:gedmo="http://gediminasm.org/schemas/orm/doctrine-extensions-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="Device\DeviceBundle\Entity\Device" table="device" repository-class="Device\DeviceBundle\Entity\Repository\DeviceRepository">
<id name="id" type="integer" column="id">
<generator strategy="AUTO" />
</id>
<field name="description" column="description" type="string" length="255" unique="true" nullable="false" />
<field name="imei" column="imei" type="string" length="17" unique="true" nullable="false" />
<field name="created" type="datetime">
<gedmo:timestampable on="create"/>
</field>
<field name="modified" type="datetime">
<gedmo:timestampable on="update"/>
</field>
<field name="deletedAt" type="datetime" nullable="true" />
<gedmo:soft-deleteable field-name="deletedAt" time-aware="false" />
</entity>
</doctrine-mapping>
DriverHasDevice.php(关系实体)
class DriverHasDevice
{
protected $driver;
protected $device;
public function setDriver($driver)
{
$this->driver = $driver;
}
public function getDriver()
{
return $this->driver;
}
public function setDevice($device)
{
$this->device = $device;
}
public function getDevice()
{
return $this->device;
}
}
DriverHasDevice 的映射
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="Device\DeviceBundle\Entity\DriverHasDevice" table="driver_has_device">
<id name="driver" column="driver" association-key="true" />
<id name="device" column="device" association-key="true" />
<many-to-one field="driver" target-entity="Driver\DriverBundle\Entity\Driver">
<join-column name="driver" referenced-column-name="id"/>
</many-to-one>
<many-to-one field="device" target-entity="Device\DeviceBundle\Entity\Device">
<join-column name="device" referenced-column-name="id"/>
</many-to-one>
</entity>
</doctrine-mapping>
第三个实体不相关,所以我不显示代码,现在,我如何获取驱动程序名称?
添加方法
我把这个方法加到Device.php:
public function getDriver()
{
return $this->driver;
}
但现在的错误是这个:
无法访问 NULL 变量 ("") 中的属性 ("name") /var/www/html/src/Device/DeviceBundle/Resources/views/List/listDevices.html.twig 在第 49 行
为什么?
【问题讨论】:
标签: php symfony doctrine-orm