Этот вопрос двоякий.
1)
В моем коде существуют две модели с отношением 1-ко-многим (1 пользователь имеет много элементов действий).Как настроить это соединение в классе PHP?
2)
Одной из моделей, actionitem
, нужна функция свойства для получения полного имени (Last, First)из actionitem
, составленного из 4 типов user
s, этот метод свойства должен действительно происходить (зависеть от) user
, определяемого идентификатором.
Как мне достичь этой цели?
<?php
namespace data\model;
class actionitem {
//Users
public $assignorid;
public $ownerid;
public $altownerid;
public $approverid;
//Additional Attributes
public $actionitemtitle;
public $actionitemstatement;
//Property Function 1
//How do I create the property function for $assignorid?
public function getAssignor(){
return ??
}
}
Проблема в том, что у меня есть функция свойства, указанная в моем user
классе
<?php
namespace data\model;
class user {
public $userid;
public $userlastname;
public $userfirstname;
//Property Function 2
public function getUserLastFirst(){
return trim($this->userlastname, ', ', $this->userfirstname);
}
}
Mapper class
<?php
class actionitems {
private $db = null;
public function __construct(PDO $db){
$this->db = $db;
}
protected function __populateFromCollection($results){
$return = array();
foreach($results as $result){
$return = $this->mapFromArray($result);
}
return $return;
}
public function mapFromArray($array, \data\model\actionitem $actionitem = null){
if (is_null($actionitem)) $actiontiem = new actionitem();
//Mapping code
if (is_null($array['assignor.lastname'])) $actionitem->assignorlastname = $array['assignor.lastname'];
if (is_null($array['assignor.firstname'])) $actionitem->assignorfirstname = $array['assignor.firstname'];
if (is_null($array['owner.lastname'])) $actionitem->ownerlastname = $array['owner.lastname'];
if (is_null($array['owner.firstname'])) $actionitem->ownerfirstname = $array['owner.firstname'];
if (is_null($array['altowner.lastname'])) $actionitem->altownerlastname = $array['altowner.lastname'];
if (is_null($array['altowner.firstname'])) $actionitem->altownerfirstname = $array['altowner.firstname'];
if (is_null($array['approver.lastname'])) $actionitem->altownerlastname = $array['approver.lastname'];
if (is_null($array['approver.firstname'])) $actionitem->approverfirstname = $array['approver.firstname'];
}
public function get($id){
}
public function getAll($params = [])
{
$whereStrings = $whereParams = array();
if (isset($params['Keyword'])){
$searchCols =
[
'assignor.lastname',
'assignor.firstname',
'owner.lastname',
'owner.firstname',
'altowner.lastname',
'altowner.firstname',
'approver.lastname',
'approver.firstname',
'actionitemtitle',
'criticality',
'actionitemstatement',
'closurecriteria',
'closurestatement',
'rejectionjustification',
'ownernotes',
'approvercomments',
'notes'
];
foreach ($searchCols as $col){
$whereStrings[] = "$col like ?";
$whereParams[] = $params['Keyword'];
}
}
$sql = "select
assignor.lastname AS assignorlastname,
assignor.firstname AS assignorfirstname,
owner.lastname AS ownerlastname,
owner.firstname AS ownerfirstname,
altowner.lastname AS altownerlastname,
altowner.firstname AS altownerfirstname,
approver.lastname AS approverlastname,
approver.firstname AS approverfistname,
a.*
from actionitems a
inner join users assignor
ON a.assignorid = assignor.userid
inner join users owner
ON a.approverid = owner.userid
inner join users altowner
ON a.altownerid = altowner.userid
inner join users approver
ON a.approverid = approver.userid";
if (!empty($whereStrings)){
$sql .= " where " . implode(' AND ' . $whereStrings);
}
if (isset($params['limit'])){
$sql .= " limit " . intval($params['limit']);
}
$statement = $this->db->prepare($sql);
$statement->execute($whereParams);
$resuls = $statement->fetchAll();
return $this->__populateFromCollection($results);
}
}