-
Notifications
You must be signed in to change notification settings - Fork 39
Db Row
Строка в таблице, аналогично принятому в ZFCore
Для корректной работы требует соответствующий класс Table
<?php
namespace Application\Users;
/**
* @property integer $id
* @property string $login
* @property string $created
* @property string $updated
*/
class Row extends \Bluz\Db\Row
{
}
$userRow = new \Application\Users\Row();
$userRow -> login = 'username';
$userRow -> save();
Аннотация в блоке комментариев необходимы лишь для поддержки автодополнения в IDE
Можно вытащить связанный записи в одном SQL запросе следующим образом:
<?php
// sql query with relation one to one
$sql = "
select
users.*
, g.name AS __Groups_name
from users
left join groups g on users.group_id = g.id
";
// use it from controller
$userRows = $db->fetchObjects($sql, array(), '\Application\Users\Row');
// use it from Users\Table
$userRows = self::fetch($sql);
// result
foreach($userRows as $userRow)
$groupRow = $userRow->getRelation('Groups');
Обратите внимание на SELECT, в нём можно увидеть конструкцию - g.name AS __Groups_name, тут Groups - имя связанной Row и его атрибут.
В Row существуют следующие хуки:
- beforeInsert
- afterInsert
- beforeUpdate
- afterUpdate
- beforeDelete
- afterDelete
Пример использования хуков:
<?php
namespace Application\Users;
/**
* @property integer $id
* @property string $login
* @property string $created
* @property string $updated
*/
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');
}
}
- добавить валидацию на уровне модели (а не Crud)
- организация взаимосвязей подобным образом мне не нравится
| users | | posts | | posts_tags | | tags |
| id | | id | | postId | | id |
| name | | title | | tagId | | name |
| userId |
<?php
namespace Application\Users;
/**
* @property $id
* @property $name
*/
class Row extends \Bluz\Db\Row {
}
class Table extends \Bluz\Db\Table {
protected $table = 'users'; // can retrieve from NS
protected $primary = ['id']; // can retrieve from DB info
}
namespace Application\Posts;
/**
* @property $id
* @property $title
* @property $userId
*/
class Row extends \Bluz\Db\Row {
}
class Table extends \Bluz\Db\Table {
/* manyToOne */
public function init()
{
$this->manyToOne('userId', 'Users', 'id');
$this->manyToMany('id', 'PostsTags', 'postId', 'Tags');
}
}
namespace Application\PostsTags;
/**
* @property $postId
* @property $tagId
*/
class Row extends \Bluz\Db\Row {
}
class Table extends \Bluz\Db\Table {
/* manyToOne */
public function init()
{
$this->manyToOne('postId', 'Posts', 'id');
$this->manyToOne('tagId', 'Tags', 'id');
}
}
namespace Application\Tags;
/**
* @property $id
* @property $name
*/
class Row extends \Bluz\Db\Row {
}
class Table extends \Bluz\Db\Table {
/* manyToOne */
public function init()
{
$this->manyToMany('id', 'PostsTags', 'tagId', 'Posts');
}
}
Acl
Application
Auth
Cache
Common
— Exception
— Collection
— Container
— Helper
— Options
— Singleton
Config
Controller
— Data
— Mapper
—— Crud
—— Rest
— Reflection
Crud
— Crud Table
Db
— Row
— Table
— Relations
— Query
Debug
EventManager
Grid
Http
Layout
Logger
Mailer
Messages
Nil
Proxy
Registry
Request
Response
Router
Session
Translator
Validator
View