Examples
From RedBean
Examples
Extending the RedBeans Domain Object
The following class extends the RedBeans Domain Object to automate some of the common tasks you may use when using the object.
<?php
class RedbeanModel extends RedBean_DomainObject
{
private $entityName;
private $fields;
/**
*
* @param unknown_type $entityName
* @param array $fields
* @param unknown_type $id
* @return unknown_type
*/
public function __construct($entityName, array $fields, $id=0)
{
if (empty($entityName))
throw new InvalidArgumentException('Argument $entityName cannot be null or empty');
if (empty($fields))
throw new InvalidArgumentException('Arugment $fields cannot be null or empty');
// store entitiy
$this->entityName = $entityName;
// store the fields which define this entity
$this->fields = $fields;
parent::__construct($entityName);
if ($id != 0)
{
$this->find($id);
// TODO: Throw exception if id is not found
}
}
public function set(array $input)
{
if (empty($input))
throw new InvalidArgumentException('Argument $input must have at least one data element.');
//if (!Ext_CheckArrayIsAssoc($input))
// throw new InvalidArgumentException('Arugment $input must be an assocative array.');
// save data into entity
foreach ($input as $key => $value)
{
if (in_array($key, $this->fields))
$this->bean->$key = $value;
else
throw new InvalidArgumentException('Argument $input contains invalid field '.$key.' which is not found in fields definition.');
}
$this->save();
}
public function fetchAsAssoc()
{
$output = array();
foreach($this->fields as $field)
{
$output[$field] = $this->bean->$field;
}
return $output;
}
public function loadFromSQL($sql)
{
if (empty($sql))
throw new InvalidInvalidArgumentException('Arugment $sql must not be null');
$rows = $this->tools->getDatabaseAdapter()->get($sql);
// if no rows found, abort
if (empty($rows))
return array();
$found = $this->tools->getRedBean()->convertToBeans($this->entityName, $rows);
return $found;
}
public function setFromSQL($sql)
{
$found = $this->loadFromSQL($sql);
$id = array_shift($found)->id;
$this->find($id);
}
/**
* For use with external controllers
* @param RedBeanModel $model
* @return unknown_type
*/
public function associateOneToMany($entity)
{
// TODO: Verify incoming model to associate
$this->set1toNAssoc($entity);
RedBean_Plugin_Constraint::addConstraint($this->bean, $entity->bean );
}
public function associate($entity)
{
parent::associate($entity);
}
public function findRelatedBeansWithCriteria($entityName, $searchSQL)
{
$keys = $this->findRelatedKeysWithCriteria($entityName, $searchSQL);
$found = $this->redbean->batch($entityName, $keys);
return $found;
}
public function findRelatedKeysWithCriteria($entityName, $searchSQL)
{
$join_table = $this->entityName.'_'.$entityName;
$sql = "SELECT `{$entityName}`.id FROM `{$this->entityName}`,
`{$entityName}`,
`{$join_table}` relate
WHERE
`{$this->entityName}`.id = relate.{$this->entityName}_id AND
`{$entityName}`.id = relate.{$entityName}_id
AND `{$this->entityName}`.id = '{$this->getID()}'
AND $searchSQL
";
$keys = $this->tools->getDatabaseAdapter()->getCol($sql);
return $keys;
}
}
?>

