/**
* Finds projects by key
* It searches by name or billing number
*
* @param string $key optional Search key
* @return ArrayCollection Returns collection of the ProjectEntity objects
*/
public function findByKey($key = null)
{
if (is_null($key) || $key === '') {
return $this->all();
}
$collection = new ArrayCollection();
$projectEntity = new ProjectEntity();
//Includes archived projects
$projectPropertyEntity = new ProjectPropertyEntity();
//Cost center entity
$ccEntity = new CostCentreEntity();
$rs = $this->db->Execute("\n SELECT " . $projectEntity->fields('p') . ", " . $ccEntity->fields('c', true) . "\n FROM " . $projectEntity->table('p') . "\n LEFT JOIN " . $ccEntity->table('c') . " ON c.`cc_id` = p.`cc_id`\n WHERE p.`name` LIKE ?\n OR EXISTS (\n SELECT 1 FROM " . $projectPropertyEntity->table('pp') . "\n WHERE `pp`.project_id = `p`.`project_id`\n AND `pp`.`name` = ? AND `pp`.`value` LIKE ?\n )\n ", ['%' . $key . '%', ProjectPropertyEntity::NAME_BILLING_CODE, '%' . $key . '%']);
while ($rec = $rs->FetchRow()) {
$item = new ProjectEntity();
$item->load($rec);
if ($rec['c_cc_id']) {
$cc = new CostCentreEntity();
$cc->load($rec, 'c');
$item->setCostCenter($cc);
}
$collection->append($item);
}
return $collection;
}