Example of Users\Row
namespace Application\Users;
class Row extends \Bluz\Db\Row
{
public function beforeInsert()
{
$this->created = gmdate('Y-m-d H:i:s');
}
public function beforeUpdate()
{
$this->updated = gmdate('Y-m-d H:i:s');
}
}
$userRow = new \Application\Users\Row();
$userRow -> login = 'username';
$userRow -> save();
/** * findRelation * * @param Row $row * @param string $relation * @return array * @throws Exception\RelationNotFoundException */ public static function findRelation($row, $relation) { $model = $row->getTable()->getModel(); /** @var \Bluz\Db\Table $relationTable */ $relationTable = Relations::getModelClass($relation); $relationTable::getInstance(); if (!($relations = Relations::getRelations($model, $relation))) { throw new RelationNotFoundException("Relations between model `{$model}` and `{$relation}` is not defined"); } // check many-to-many relations if (sizeof($relations) == 1) { $relations = Relations::getRelations($model, current($relations)); } $field = $relations[$model]; $key = $row->{$field}; return Relations::findRelations($model, $relation, [$key]); }
/** * Set relation * @param Row $row * @return Row */ public function setRelation(Row $row) { $modelName = $row->getTable()->getModel(); $this->relations[$modelName] = [$row]; return $this; }
/** * @expectedException Bluz\Db\Exception\RelationNotFoundException */ public function testGetRelationException() { $this->row->getRelation('wrongRelation'); }