public function getAccountProjects($accountId, $query = null)
{
$collection = $this->findByKey($query, ['accountId' => $accountId], true);
//Select identifiers of all projects assigned to farms from the account
$assignedProjects = [];
$rs = $this->db->Execute("\n SELECT DISTINCT fs.value\n FROM farms f\n JOIN farm_settings fs ON f.id = fs.farmid\n WHERE fs.name = ?\n AND f.clientid = ?\n ", [Entity\FarmSetting::PROJECT_ID, $accountId]);
while ($rec = $rs->fetchRow()) {
$assignedProjects[$rec['value']] = true;
}
//Adjusts missing projects.
//This is going to be very rare event.
foreach ($collection as $projectEntity) {
if (isset($assignedProjects[$projectEntity->projectId])) {
unset($assignedProjects[$projectEntity->projectId]);
}
}
foreach ($assignedProjects as $projectId => $v) {
$project = ProjectEntity::findPk($projectId);
/* @var $project ProjectEntity */
$projectBillingCode = $project->getProperty(ProjectPropertyEntity::NAME_BILLING_CODE);
if (empty($query) || (stripos($project->name, $query) !== false || stripos($projectBillingCode, $query) !== false)) {
$collection->append($project);
}
}
return $collection;
}