This Question is two-fold.
1)
Two models exist in my code with 1-To-many relationship (1 user has many action items). How do I set up this connection in a PHP class?
2)
One of the models, actionitem
, needs a property function to produce a full name, (Last, First) of a actionitem
made up of 4 types of user
s this property method should really come from (depend on) user
determined by id.
How do I accomplish this objective?
<?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 ??
}
}
Problem is I have the property function specified in my user
class
<?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);
}
}