我在Symfony 3.4中拥有这些实体:
class Status {
/*
* @var int
*/
public $id;
/*
* @var string
*/
public $name;
/*
* @var StatusLabel $label
*
* @ManyToOne(targetEntity="StatusLabel")
* @JoinColumn(name="statusLabelId", referencedColumnName="id")
*/
public $label;
/*
* @return StatusLabel
*/
public function getLabel($activity) // Needs an activity as an argument
{
return $this->label;
}
}
class Activity {
/*
* @var int
*/
public $id;
/*
* @var string
*/
public $name;
}
class StatusLabel {
/*
* @var int
*/
public $id;
/*
* @var int
*/
public $statusId;
/*
* @var int
*/
public $activityId;
/*
* @return string
*/
public $label;
}
允许我们为状态活动设置状态标签的新实体称为StatusLabel。这些表具有以下属性:
Status
--------
| id | name |
StatusLabel
--------
| id | activityId | statusId | label |
Activity
--------
| id | name |
尝试使用批注来声明连接,以便在有活动的情况下获取StatusLabel.label,例如:
$activity = $this->getDoctrine()->getRepository('Entity\Activity')->findOneBy(['slug' => 'outdoors-activity']);
/** @var StatusLabel $activityStatusLabel */
$activityStatusLabel = $userActivity->getStatus()->getLabel($activity);
echo $activityStatusLabel->getLabel();
自动传递活动会更好,但是我无法弄清楚如何使用ORM批注进行操作。我不想从实体查询!